github.com/keikoproj/manny@v0.0.0-20210726112440-8571e4c99ced/cmd/root.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/spf13/cobra"
     8  	"go.uber.org/zap"
     9  	"go.uber.org/zap/zapcore"
    10  )
    11  
    12  const (
    13  	ErrMissingArg  = "missing argument"
    14  	ConfigDebug    = "debug"
    15  	ConfigFormat   = "format"
    16  	OutputFormat   = "output"
    17  	ConfigValidate = "validate"
    18  	ConfigDryRun   = "dry-run"
    19  )
    20  
    21  var (
    22  	Logger *zap.Logger
    23  
    24  	// flag storage
    25  	debug    bool
    26  	format   string
    27  	output   string
    28  	dryRun   bool
    29  	validate bool
    30  
    31  	// Commands
    32  	rootCmd = &cobra.Command{
    33  		Use:   "manny",
    34  		Short: "Argo CD tool to generate K8s manifests from GitOps repo",
    35  		Long:  `Argo CD tool to generate K8s manifests from GitOps repo`,
    36  	}
    37  )
    38  
    39  func Execute() {
    40  	if err := rootCmd.Execute(); err != nil {
    41  		os.Exit(1)
    42  	}
    43  }
    44  
    45  func init() {
    46  	cobra.OnInitialize(initLogger)
    47  
    48  	rootCmd.AddCommand(buildCmd)
    49  
    50  	rootCmd.PersistentFlags().BoolVarP(&debug, ConfigDebug, "D", false, "sets debug mode")
    51  	buildCmd.PersistentFlags().StringVarP(&format, ConfigFormat, "f", "yaml", "sets output format")
    52  	buildCmd.PersistentFlags().StringVarP(&output, OutputFormat, "o", "stdout", "sets file location")
    53  	buildCmd.PersistentFlags().BoolVarP(&validate, ConfigValidate, "", true, "validates the CloudFormation output")
    54  	buildCmd.PersistentFlags().BoolVarP(&dryRun, ConfigDryRun, "", false, "does not output a CloudResource")
    55  }
    56  
    57  // initLogger reads in config file and ENV variables if set.
    58  func initLogger() {
    59  	cfg := zap.Config{
    60  		Encoding:         "console",
    61  		OutputPaths:      []string{"stderr"},
    62  		ErrorOutputPaths: []string{"stderr"},
    63  		Level:            zap.NewAtomicLevelAt(zapcore.InfoLevel),
    64  		EncoderConfig: zapcore.EncoderConfig{
    65  			MessageKey: "message",
    66  
    67  			LevelKey:    "level",
    68  			EncodeLevel: zapcore.CapitalLevelEncoder,
    69  
    70  			TimeKey:    "time",
    71  			EncodeTime: zapcore.ISO8601TimeEncoder,
    72  
    73  			CallerKey:    "caller",
    74  			EncodeCaller: zapcore.ShortCallerEncoder,
    75  		},
    76  	}
    77  
    78  	if debug {
    79  		cfg.Level = zap.NewAtomicLevelAt(zapcore.DebugLevel)
    80  	}
    81  
    82  	l, err := cfg.Build()
    83  	if err != nil {
    84  		fmt.Printf("Error setting up logger: %s", err)
    85  	}
    86  
    87  	Logger = l
    88  }