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

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