package cmd import ( "fmt" "os" "git.bloy.org/mike/hasshelper/web" "github.com/spf13/cobra" "github.com/spf13/viper" "go.uber.org/zap" ) var rootCmd = &cobra.Command{ Use: "hasshelper", Short: "Helper for Home Assistant installations.", Run: func(cmd *cobra.Command, args []string) { var logger *zap.Logger var err error if viper.GetString("deployment") == "prod" { logger, err = zap.NewProduction(zap.WithCaller(false)) } else { logger, err = zap.NewDevelopment(zap.WithCaller(false)) } if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } logger.Sugar().With("version", viper.GetString("version")).Info("HASSHelper startup") exitchan := make(chan bool) web.Run(logger, exitchan) <-exitchan // run the main command until one of the goroutines is done }, } // Execute will kick off cobra's processing of the root command func Execute() { viper.SetDefault("Version", "1.2.3") if err := rootCmd.Execute(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } } func init() { cobra.OnInitialize(initConfig) viper.SetEnvPrefix("hasshelper") viper.Set("version", gitVersion) viper.SetDefault("deployment", "prod") } func initConfig() { viper.AutomaticEnv() expected_config := []string{ "deployment", "image_dir", "version", "webserver_port", } for _, key := range expected_config { if !viper.IsSet(key) { fmt.Fprintf(os.Stderr, "Missing configuration value: %s\n", key) os.Exit(1) } } }