Skip to content

Commit

Permalink
Add flag --set that allows passing (potentially multiple) key/value p…
Browse files Browse the repository at this point in the history
…airs to set in the config via --set of -s flags
  • Loading branch information
cmmarslender committed Jul 30, 2024
1 parent b9271d7 commit 4f8cd2b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
18 changes: 18 additions & 0 deletions cmd/config/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ var generateCmd = &cobra.Command{
log.Fatalln(err.Error())
}

valuesToSet := viper.GetStringMapString("set")
for path, value := range valuesToSet {
pathMap := config.ParsePathsFromStrings([]string{path}, false)
var key string
var pathSlice []string
for key, pathSlice = range pathMap {
break
}
err = cfg.SetFieldByPath(pathSlice, value)
if err != nil {
log.Fatalf("Error setting path `%s` to `%s`: %s\n", key, value, err.Error())
}
}

out, err := yaml.Marshal(cfg)
if err != nil {
fmt.Printf("Error marshalling config: %s\n", err.Error())
Expand All @@ -42,10 +56,14 @@ var generateCmd = &cobra.Command{
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")

cobra.CheckErr(viper.BindPFlag("output", generateCmd.PersistentFlags().Lookup("output")))
cobra.CheckErr(viper.BindPFlag("set", generateCmd.PersistentFlags().Lookup("set")))

configCmd.AddCommand(generateCmd)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/chia-network/chia-tools
go 1.22.4

require (
github.com/chia-network/go-chia-libs v0.8.3
github.com/chia-network/go-chia-libs v0.8.3-0.20240730222152-3b0bcd6a6205
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
gopkg.in/yaml.v3 v3.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/chia-network/go-chia-libs v0.8.3 h1:B/Ho8OUD9qr5duVuUeULvbnZ8iYRFzKoB45LJJ7XS60=
github.com/chia-network/go-chia-libs v0.8.3/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I=
github.com/chia-network/go-chia-libs v0.8.3-0.20240730222152-3b0bcd6a6205 h1:mDxjsfzxOa/voDTvNQz8aORpPl66SbbKpxSn7KFPOLY=
github.com/chia-network/go-chia-libs v0.8.3-0.20240730222152-3b0bcd6a6205/go.mod h1:imCkBPLUOjqyvYV2XdYnef8EfeiDdZ8bfZCGvG4R1o0=
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 4f8cd2b

Please sign in to comment.