diff --git a/cmd/config/generate.go b/cmd/config/generate.go index 768749d..b4f4df9 100644 --- a/cmd/config/generate.go +++ b/cmd/config/generate.go @@ -1,10 +1,10 @@ package config import ( - "log" "os" "github.com/chia-network/go-chia-libs/pkg/config" + "github.com/chia-network/go-modules/pkg/slogs" "github.com/spf13/cobra" "github.com/spf13/viper" "gopkg.in/yaml.v3" @@ -17,12 +17,12 @@ var generateCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { cfg, err := config.LoadDefaultConfig() if err != nil { - log.Fatalln(err.Error()) + slogs.Logr.Fatal("error loading default config", "error", err) } err = cfg.FillValuesFromEnvironment() if err != nil { - log.Fatalln(err.Error()) + slogs.Logr.Fatal("error filling values from environment", "error", err) } valuesToSet := viper.GetStringMapString("set") @@ -35,30 +35,25 @@ var generateCmd = &cobra.Command{ } err = cfg.SetFieldByPath(pathSlice, value) if err != nil { - log.Fatalf("Error setting path `%s` to `%s`: %s\n", key, value, err.Error()) + slogs.Logr.Fatal("error setting path in config", "key", key, "value", value, "error", err) } } out, err := yaml.Marshal(cfg) if err != nil { - log.Fatalf("Error marshalling config: %s\n", err.Error()) + slogs.Logr.Fatal("error marshalling config", "error", err) } err = os.WriteFile(viper.GetString("output"), out, 0655) if err != nil { - log.Fatalln(err.Error()) + slogs.Logr.Fatal("error writing output file", "error", err) } }, } func init() { - var ( - outputFile string - setValues map[string]string - ) - - generateCmd.PersistentFlags().StringVarP(&outputFile, "output", "o", "config.yml", "Output file for config") - generateCmd.PersistentFlags().StringToStringVarP(&setValues, "set", "s", nil, "Paths and values to set in the config") + generateCmd.PersistentFlags().StringP("output", "o", "config.yml", "Output file for config") + generateCmd.PersistentFlags().StringToStringP("set", "s", nil, "Paths and values to set in the config") cobra.CheckErr(viper.BindPFlag("output", generateCmd.PersistentFlags().Lookup("output"))) cobra.CheckErr(viper.BindPFlag("set", generateCmd.PersistentFlags().Lookup("set"))) diff --git a/cmd/root.go b/cmd/root.go index d83e9de..9bc1f91 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,6 +7,8 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + + "github.com/chia-network/go-modules/pkg/slogs" ) var cfgFile string @@ -28,8 +30,11 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) + cobra.OnInitialize(initLogs) RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.chia-tools.yaml)") + RootCmd.PersistentFlags().String("log-level", "info", "The log-level for the application, can be one of info, warn, error, debug.") + cobra.CheckErr(viper.BindPFlag("log-level", RootCmd.PersistentFlags().Lookup("log-level"))) } // initConfig reads in config file and ENV variables if set. @@ -57,3 +62,7 @@ func initConfig() { fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) } } + +func initLogs() { + slogs.Init(viper.GetString("log-level")) +} diff --git a/go.mod b/go.mod index cf9429f..0fdad60 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.22.4 require ( github.com/chia-network/go-chia-libs v0.8.4 + github.com/chia-network/go-modules v0.0.5 github.com/spf13/cobra v1.8.1 github.com/spf13/viper v1.19.0 gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index 80cb407..3fbe089 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/chia-network/go-chia-libs v0.8.4 h1:8t9qXQljM8vuHTpkUi6JxUJz0Ne0TWE7KDZ/ELnn/KY= github.com/chia-network/go-chia-libs v0.8.4/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I= +github.com/chia-network/go-modules v0.0.5 h1:5luTVlP6RgBXodnFcWFBk2sLdJn+6vQ4wObim683C7c= +github.com/chia-network/go-modules v0.0.5/go.mod h1:5AiYBxQSvf2aFSOizTqFXXSeb9AucZWrWmRCVwUMO3A= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=