From f88e7b2b5451f1edcefa2e7de492e9d20df6ba54 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Tue, 10 Sep 2024 11:53:32 -0500 Subject: [PATCH 1/4] Add edit config command --- cmd/config/edit.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 +-- 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 cmd/config/edit.go diff --git a/cmd/config/edit.go b/cmd/config/edit.go new file mode 100644 index 0000000..37d44e7 --- /dev/null +++ b/cmd/config/edit.go @@ -0,0 +1,82 @@ +package config + +import ( + "os" + "path" + + "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" +) + +// editCmd generates a new chia config +var editCmd = &cobra.Command{ + Use: "edit", + Short: "Edit an existing chia configuration file", + Example: `chia-tools config edit ~/.chia/mainnet/config/config.yaml --set full_node.port=58444 --set full_node.target_peer_count=10 + +# The following version will discover the config file by inspecting CHIA_ROOT or using the default CHIA_ROOT +chia-tools config edit --set full_node.port=58444 --set full_node.target_peer_count=10`, + Run: func(cmd *cobra.Command, args []string) { + var cfgPath string + + chiaRoot, err := config.GetChiaRootPath() + if err != nil { + slogs.Logr.Fatal("Unable to determine CHIA_ROOT", "error", err) + } + + if len(args) > 1 { + slogs.Logr.Fatal("Unexpected number of arguments provided") + } else if len(args) == 1 { + // Use the provided config path + cfgPath = args[0] + } else { + // Use default chia root + cfgPath = path.Join(chiaRoot, "config", "config.yaml") + } + + cfg, err := config.LoadConfigAtRoot(cfgPath, chiaRoot) + if err != nil { + slogs.Logr.Fatal("error loading chia config", "error", err) + } + + err = cfg.FillValuesFromEnvironment() + if err != nil { + slogs.Logr.Fatal("error filling values from environment", "error", err) + } + + 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 { + slogs.Logr.Fatal("error setting path in config", "key", key, "value", value, "error", err) + } + } + + out, err := yaml.Marshal(cfg) + if err != nil { + slogs.Logr.Fatal("error marshalling config", "error", err) + } + + err = os.WriteFile(cfgPath, out, 0655) + if err != nil { + slogs.Logr.Fatal("error writing output file", "error", err) + } + }, +} + +func init() { + editCmd.PersistentFlags().StringToStringP("set", "s", nil, "Paths and values to set in the config") + + cobra.CheckErr(viper.BindPFlag("set", editCmd.PersistentFlags().Lookup("set"))) + + configCmd.AddCommand(editCmd) +} diff --git a/go.mod b/go.mod index 38b5d25..60df227 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/chia-network/chia-tools go 1.22.4 require ( - github.com/chia-network/go-chia-libs v0.9.1 + github.com/chia-network/go-chia-libs v0.9.2-0.20240910165058-9298d82521aa github.com/chia-network/go-modules v0.0.5 github.com/spf13/cobra v1.8.1 github.com/spf13/viper v1.19.0 diff --git a/go.sum b/go.sum index 45237b3..fd561a7 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/chia-network/go-chia-libs v0.9.1 h1:RAoCnnAl0hccUw6ue/9OsoYxFLlUiXpNy5OPDUDqUQQ= -github.com/chia-network/go-chia-libs v0.9.1/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I= +github.com/chia-network/go-chia-libs v0.9.2-0.20240910165058-9298d82521aa h1:/ae2R5LlLgKZMkmKszBZ1ggasyzxoZyZ0F8M3sq+E70= +github.com/chia-network/go-chia-libs v0.9.2-0.20240910165058-9298d82521aa/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= From b09a7498960f73121614cf814912471d44e5bc28 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Tue, 10 Sep 2024 11:58:32 -0500 Subject: [PATCH 2/4] Fix viper binding + update go chia libs to released version --- cmd/config/edit.go | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/config/edit.go b/cmd/config/edit.go index 37d44e7..eddb203 100644 --- a/cmd/config/edit.go +++ b/cmd/config/edit.go @@ -47,7 +47,7 @@ chia-tools config edit --set full_node.port=58444 --set full_node.target_peer_co slogs.Logr.Fatal("error filling values from environment", "error", err) } - valuesToSet := viper.GetStringMapString("set") + valuesToSet := viper.GetStringMapString("edit-set") for path, value := range valuesToSet { pathMap := config.ParsePathsFromStrings([]string{path}, false) var key string @@ -76,7 +76,7 @@ chia-tools config edit --set full_node.port=58444 --set full_node.target_peer_co func init() { editCmd.PersistentFlags().StringToStringP("set", "s", nil, "Paths and values to set in the config") - cobra.CheckErr(viper.BindPFlag("set", editCmd.PersistentFlags().Lookup("set"))) + cobra.CheckErr(viper.BindPFlag("edit-set", editCmd.PersistentFlags().Lookup("set"))) configCmd.AddCommand(editCmd) } diff --git a/go.mod b/go.mod index 60df227..c1001aa 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/chia-network/chia-tools go 1.22.4 require ( - github.com/chia-network/go-chia-libs v0.9.2-0.20240910165058-9298d82521aa + github.com/chia-network/go-chia-libs v0.9.2 github.com/chia-network/go-modules v0.0.5 github.com/spf13/cobra v1.8.1 github.com/spf13/viper v1.19.0 diff --git a/go.sum b/go.sum index fd561a7..121fe4b 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/chia-network/go-chia-libs v0.9.2-0.20240910165058-9298d82521aa h1:/ae2R5LlLgKZMkmKszBZ1ggasyzxoZyZ0F8M3sq+E70= -github.com/chia-network/go-chia-libs v0.9.2-0.20240910165058-9298d82521aa/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I= +github.com/chia-network/go-chia-libs v0.9.2 h1:WWfiKoCA9zpoq4JgbX/l9cCcdJ4wQeWdc1iG29Fa8/E= +github.com/chia-network/go-chia-libs v0.9.2/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= From 9de3e62b5b28dbced14985db66cf61a44747ec37 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Tue, 10 Sep 2024 11:59:24 -0500 Subject: [PATCH 3/4] Lint --- cmd/certs/generate.go | 4 ++-- cmd/config/edit.go | 4 ++-- cmd/datalayer/deletemirrors.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/certs/generate.go b/cmd/certs/generate.go index b6060a1..031a55e 100644 --- a/cmd/certs/generate.go +++ b/cmd/certs/generate.go @@ -9,8 +9,8 @@ import ( // generateCmd represents the generate command var generateCmd = &cobra.Command{ - Use: "generate", - Short: "Generates a full set of certificates for chia-blockchain", + Use: "generate", + Short: "Generates a full set of certificates for chia-blockchain", Example: "chia-tools certs generate --output ~/.chia/mainnet/config/ssl", Run: func(cmd *cobra.Command, args []string) { err := tls.GenerateAllCerts(viper.GetString("cert-output")) diff --git a/cmd/config/edit.go b/cmd/config/edit.go index eddb203..382c4fe 100644 --- a/cmd/config/edit.go +++ b/cmd/config/edit.go @@ -13,8 +13,8 @@ import ( // editCmd generates a new chia config var editCmd = &cobra.Command{ - Use: "edit", - Short: "Edit an existing chia configuration file", + Use: "edit", + Short: "Edit an existing chia configuration file", Example: `chia-tools config edit ~/.chia/mainnet/config/config.yaml --set full_node.port=58444 --set full_node.target_peer_count=10 # The following version will discover the config file by inspecting CHIA_ROOT or using the default CHIA_ROOT diff --git a/cmd/datalayer/deletemirrors.go b/cmd/datalayer/deletemirrors.go index 5573c7c..4eced40 100644 --- a/cmd/datalayer/deletemirrors.go +++ b/cmd/datalayer/deletemirrors.go @@ -12,8 +12,8 @@ import ( // deleteMirrorsCmd Deletes all owned mirrors for all datalayer subscriptions var deleteMirrorsCmd = &cobra.Command{ - Use: "delete-mirrors", - Short: "Deletes all owned mirrors for all datalayer subscriptions", + Use: "delete-mirrors", + Short: "Deletes all owned mirrors for all datalayer subscriptions", Example: "chia-tools data delete-mirrors --all\nchia-tools data delete-mirrors --id abcd1234", PreRunE: func(cmd *cobra.Command, args []string) error { all := viper.GetBool("delete-mirror-all") From 3d9d6df2eab4b4c04265b93d38f198d4deb9a7de Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Tue, 10 Sep 2024 12:05:45 -0500 Subject: [PATCH 4/4] Fix docker AS case --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 11ffd40..1a62bae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1 as builder +FROM golang:1 AS builder COPY . /app WORKDIR /app