github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/inspect/context/context.go (about)

     1  package inspectcontext
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/hashicorp/errwrap"
    10  	"github.com/henvic/wedeploycli/inspector"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  // ContextCmd for "lcp inspect context"
    15  var ContextCmd = &cobra.Command{
    16  	Use:   "context",
    17  	Short: "Get context info",
    18  	Args:  cobra.NoArgs,
    19  	RunE:  runE,
    20  }
    21  
    22  var (
    23  	directory      string
    24  	format         string
    25  	showTypeFields bool
    26  )
    27  
    28  func init() {
    29  	ContextCmd.Flags().StringVar(&directory, "directory", "", "Run the command on another directory")
    30  	ContextCmd.Flags().StringVarP(&format, "format", "f", "", "Format the output using the given go template")
    31  	ContextCmd.Flags().BoolVar(&showTypeFields, "fields", false, "Show type field names")
    32  }
    33  
    34  func runE(cmd *cobra.Command, args []string) (err error) {
    35  	if directory == "" {
    36  		directory = "."
    37  	}
    38  
    39  	if directory, err = filepath.Abs(directory); err != nil {
    40  		return errwrap.Wrapf("can't resolve directory: {{err}}", err)
    41  	}
    42  
    43  	if showTypeFields && format != "" {
    44  		return errors.New("incompatible use: --fields and --format cannot be used together")
    45  	}
    46  
    47  	if showTypeFields {
    48  		var i = inspector.ContextOverview{}
    49  		fmt.Println(strings.Join(inspector.GetSpec(i), "\n"))
    50  		return nil
    51  	}
    52  
    53  	var inspectMsg, inspectErr = inspector.InspectContext(format, directory)
    54  
    55  	if inspectErr != nil {
    56  		return inspectErr
    57  	}
    58  
    59  	fmt.Printf("%v\n", inspectMsg)
    60  	return nil
    61  }