Skip to content

Commit

Permalink
log -> slog (#7)
Browse files Browse the repository at this point in the history
* Add slogs package

* Fix my flags while I'm at it...

* log -> slog
  • Loading branch information
cmmarslender authored Jul 31, 2024
1 parent d459108 commit 7aff487
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
21 changes: 8 additions & 13 deletions cmd/config/generate.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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")
Expand All @@ -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")))
Expand Down
9 changes: 9 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/chia-network/go-modules/pkg/slogs"
)

var cfgFile string
Expand All @@ -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.
Expand Down Expand Up @@ -57,3 +62,7 @@ func initConfig() {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}

func initLogs() {
slogs.Init(viper.GetString("log-level"))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down

0 comments on commit 7aff487

Please sign in to comment.