github.com/flanksource/konfigadm@v0.12.0/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/flanksource/konfigadm/cmd"
     8  	log "github.com/sirupsen/logrus"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var (
    13  	version = "dev"
    14  	commit  = "none"
    15  	date    = "unknown"
    16  )
    17  
    18  func init() {
    19  	log.SetOutput(os.Stderr)
    20  }
    21  
    22  func main() {
    23  	var root = &cobra.Command{
    24  		Use: "konfigadm",
    25  		PersistentPreRun: func(cmd *cobra.Command, args []string) {
    26  			level, _ := cmd.Flags().GetCount("loglevel")
    27  			switch {
    28  			case level > 2:
    29  				log.SetLevel(log.TraceLevel)
    30  			case level > 1:
    31  				log.SetLevel(log.DebugLevel)
    32  			case level > 0:
    33  				log.SetLevel(log.InfoLevel)
    34  			default:
    35  				log.SetLevel(log.WarnLevel)
    36  			}
    37  			log.SetOutput(os.Stderr)
    38  		},
    39  	}
    40  
    41  	root.PersistentFlags().StringSliceP("config", "c", []string{}, "Config file path in YAML or JSON format, Use - to read YAML from stdin")
    42  	root.PersistentFlags().StringSliceP("var", "e", []string{}, "Extra Variables to in key=value format ")
    43  	root.PersistentFlags().StringSliceP("tag", "t", []string{}, "Runtime tags to use, valid tags:  debian,ubuntu,redhat,rhel,centos,aws,vmware,photon")
    44  	root.PersistentFlags().BoolP("detect-tags", "d", true, "Detect tags to use")
    45  
    46  	root.AddCommand(&cmd.CloudInit, &cmd.Minify, &cmd.Apply, &cmd.Verify, &cmd.Images)
    47  	if len(commit) > 8 {
    48  		version = fmt.Sprintf("%v, commit %v, built at %v", version, commit[0:8], date)
    49  	}
    50  	root.AddCommand(&cobra.Command{
    51  		Use:   "version",
    52  		Short: "Print the version of konfigadm",
    53  		Args:  cobra.MinimumNArgs(0),
    54  		Run: func(cmd *cobra.Command, args []string) {
    55  			fmt.Println(version)
    56  		},
    57  	})
    58  
    59  	root.PersistentFlags().CountP("loglevel", "v", "Increase logging level")
    60  	root.SetUsageTemplate(root.UsageTemplate() + fmt.Sprintf("\nversion: %s\n ", version))
    61  
    62  	if err := root.Execute(); err != nil {
    63  		os.Exit(1)
    64  	}
    65  
    66  }