From ed7034dec7661ba050cbed7a8828f2cc00cdc5c3 Mon Sep 17 00:00:00 2001 From: Prajjawalk Date: Sat, 19 Aug 2023 22:07:10 +0530 Subject: [PATCH 1/3] Add: cli command to save default config to a file --- app/client/cli/config.go | 19 +++++++++++++++++++ app/client/doc/CHANGELOG.md | 4 ++++ runtime/configs/config.go | 14 ++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 app/client/cli/config.go diff --git a/app/client/cli/config.go b/app/client/cli/config.go new file mode 100644 index 000000000..7eee24475 --- /dev/null +++ b/app/client/cli/config.go @@ -0,0 +1,19 @@ +package cli + +import ( + "github.com/pokt-network/pocket/runtime/configs" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(saveDefaultConf) +} + +var saveDefaultConf = &cobra.Command{ + Use: "save_default_config", + Short: "Save the default config in a file", + Long: "The default config generated during application start is saved in a config.yaml file", + Run: func(cmd *cobra.Command, args []string) { + configs.SaveConfig(args[0]) + }, +} diff --git a/app/client/doc/CHANGELOG.md b/app/client/doc/CHANGELOG.md index 0069d5d69..4fe72e7a9 100644 --- a/app/client/doc/CHANGELOG.md +++ b/app/client/doc/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.0.0.37] - 2023-08-19 + +- Add cli to generate the default config and save it to a file. + ## [0.0.0.36] - 2023-06-19 - Add a new trustless relay sub-command to servicer command diff --git a/runtime/configs/config.go b/runtime/configs/config.go index a4cbb0e53..a9ff77645 100644 --- a/runtime/configs/config.go +++ b/runtime/configs/config.go @@ -2,8 +2,10 @@ package configs import ( "encoding/json" + "fmt" "log" "os" + "path/filepath" "strings" "github.com/mitchellh/mapstructure" @@ -243,3 +245,15 @@ func defaultServicerConfig() *ServicerConfig { }, } } + +func SaveConfig(path string) { + filePath, err := filepath.Abs(path) + if err != nil { + log.Fatalf("[ERROR] failed to resolve config path, %s", err.Error()) + } + + NewDefaultConfig() + viper.WriteConfigAs(filePath) + + fmt.Printf("save_default_config: saved default config at %v", filePath) +} From ad9994f6ad44f0d0e1857f823ab0f712e96d69db Mon Sep 17 00:00:00 2001 From: Prajjawalk Date: Sat, 19 Aug 2023 22:08:45 +0530 Subject: [PATCH 2/3] Add: cli command to save default config to a file --- app/client/cli/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/client/cli/config.go b/app/client/cli/config.go index 7eee24475..5483641f9 100644 --- a/app/client/cli/config.go +++ b/app/client/cli/config.go @@ -12,7 +12,7 @@ func init() { var saveDefaultConf = &cobra.Command{ Use: "save_default_config", Short: "Save the default config in a file", - Long: "The default config generated during application start is saved in a config.yaml file", + Long: "The default config generated during application start is saved in a config file path passed in the argument", Run: func(cmd *cobra.Command, args []string) { configs.SaveConfig(args[0]) }, From 91ad43e727b57a0a619944a43615d053253b6cd5 Mon Sep 17 00:00:00 2001 From: Prajjawalk Date: Sun, 20 Aug 2023 12:46:04 +0530 Subject: [PATCH 3/3] Handle error in viper.WriteConfigAs function --- runtime/configs/config.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/runtime/configs/config.go b/runtime/configs/config.go index a9ff77645..c40d3c891 100644 --- a/runtime/configs/config.go +++ b/runtime/configs/config.go @@ -253,7 +253,10 @@ func SaveConfig(path string) { } NewDefaultConfig() - viper.WriteConfigAs(filePath) + err = viper.WriteConfigAs(filePath) + if err != nil { + log.Fatalf("[ERROR] failed to write config to file, error: %s", err.Error()) + } fmt.Printf("save_default_config: saved default config at %v", filePath) }