From f2d5aeb984399ad296baf02ed44f0dc41f456f66 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 28 Sep 2023 11:25:13 +0200 Subject: [PATCH] feat(confix): add `view` command (#17904) --- tools/confix/CHANGELOG.md | 1 + tools/confix/README.md | 12 ++++++++ tools/confix/cmd/config.go | 1 + tools/confix/cmd/view.go | 54 +++++++++++++++++++++++++++++++++++ tools/confix/cmd/view_test.go | 39 +++++++++++++++++++++++++ tools/confix/go.mod | 2 +- 6 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 tools/confix/cmd/view.go create mode 100644 tools/confix/cmd/view_test.go diff --git a/tools/confix/CHANGELOG.md b/tools/confix/CHANGELOG.md index 902c1f7f60..cd3ae1ffc3 100644 --- a/tools/confix/CHANGELOG.md +++ b/tools/confix/CHANGELOG.md @@ -31,5 +31,6 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +* [#17904](https://github.com/cosmos/cosmos-sdk/pull/17904) Add `view` command. * [#14568](https://github.com/cosmos/cosmos-sdk/pull/14568) Add `diff` and `home` commands. * [#14342](https://github.com/cosmos/cosmos-sdk/pull/14342) Add `confix` tool to manage configuration files. diff --git a/tools/confix/README.md b/tools/confix/README.md index 0c2eab53e1..f6badd4973 100644 --- a/tools/confix/README.md +++ b/tools/confix/README.md @@ -115,6 +115,18 @@ simd config diff v0.47 # gets the diff between defaultHome/config/app.toml and t confix diff v0.47 ~/.simapp/config/app.toml # gets the diff between ~/.simapp/config/app.toml and the latest v0.47 config ``` +### View + +View a configuration file, e.g: + +```shell +simd config view client # views the current app client config +``` + +```shell +confix view ~/.simapp/config/client.toml # views the current app client conf +``` + ### Maintainer At each SDK modification of the default configuration, add the default SDK config under `data/v0.XX-app.toml`. diff --git a/tools/confix/cmd/config.go b/tools/confix/cmd/config.go index 6ceb6247a2..3b3196aabf 100644 --- a/tools/confix/cmd/config.go +++ b/tools/confix/cmd/config.go @@ -17,6 +17,7 @@ func ConfigCommand() *cobra.Command { DiffCommand(), GetCommand(), SetCommand(), + ViewCommand(), HomeCommand(), ) diff --git a/tools/confix/cmd/view.go b/tools/confix/cmd/view.go new file mode 100644 index 0000000000..bab2116c04 --- /dev/null +++ b/tools/confix/cmd/view.go @@ -0,0 +1,54 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "os" + + "github.com/pelletier/go-toml/v2" + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/client" +) + +func ViewCommand() *cobra.Command { + flagOutputFomat := "output-format" + + cmd := &cobra.Command{ + Use: "view [config]", + Short: "View the config file", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + filename := args[0] + + clientCtx := client.GetClientContextFromCmd(cmd) + if clientCtx.HomeDir != "" { + filename = fmt.Sprintf("%s/config/%s.toml", clientCtx.HomeDir, filename) + } + + file, err := os.ReadFile(filename) + if err != nil { + return err + } + + if format, _ := cmd.Flags().GetString(flagOutputFomat); format == "toml" { + cmd.Println(string(file)) + return nil + } + + var v interface{} + if err := toml.Unmarshal(file, &v); err != nil { + return fmt.Errorf("failed to decode config file: %w", err) + } + + e := json.NewEncoder(cmd.OutOrStdout()) + e.SetIndent("", " ") + return e.Encode(v) + }, + } + + // output flag + cmd.Flags().String(flagOutputFomat, "toml", "Output format (json|toml)") + + return cmd +} diff --git a/tools/confix/cmd/view_test.go b/tools/confix/cmd/view_test.go new file mode 100644 index 0000000000..c022af1576 --- /dev/null +++ b/tools/confix/cmd/view_test.go @@ -0,0 +1,39 @@ +package cmd_test + +import ( + "fmt" + "os" + "strings" + "testing" + + "gotest.tools/v3/assert" + + "cosmossdk.io/tools/confix/cmd" + + "github.com/cosmos/cosmos-sdk/client" + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" +) + +func TestViewCmd(t *testing.T) { + clientCtx, cleanup := initClientContext(t) + defer cleanup() + + _, err := clitestutil.ExecTestCLICmd(clientCtx, cmd.ViewCommand(), []string{"unexisting"}) + assert.ErrorContains(t, err, "no such file or directory") + + expectedCfg := fmt.Sprintf("%s/config/client.toml", clientCtx.HomeDir) + bz, err := os.ReadFile(expectedCfg) + assert.NilError(t, err) + + out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd.ViewCommand(), []string{"client"}) + assert.NilError(t, err) + assert.DeepEqual(t, strings.TrimSpace(out.String()), strings.TrimSpace(string(bz))) + + out, err = clitestutil.ExecTestCLICmd(client.Context{}, cmd.ViewCommand(), []string{fmt.Sprintf("%s/config/client.toml", clientCtx.HomeDir)}) + assert.NilError(t, err) + assert.DeepEqual(t, strings.TrimSpace(out.String()), strings.TrimSpace(string(bz))) + + out, err = clitestutil.ExecTestCLICmd(clientCtx, cmd.ViewCommand(), []string{"client", "--output-format", "json"}) + assert.NilError(t, err) + assert.Assert(t, strings.Contains(out.String(), `"chain-id": "test-chain"`)) +} diff --git a/tools/confix/go.mod b/tools/confix/go.mod index 4be15b640c..393bc5f00c 100644 --- a/tools/confix/go.mod +++ b/tools/confix/go.mod @@ -6,6 +6,7 @@ require ( github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230614103911-b3da8bb4e801 github.com/creachadair/atomicfile v0.3.2 github.com/creachadair/tomledit v0.0.25 + github.com/pelletier/go-toml/v2 v2.0.9 github.com/spf13/cobra v1.7.0 github.com/spf13/viper v1.16.0 golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 @@ -111,7 +112,6 @@ require ( github.com/mtibben/percent v0.2.1 // indirect github.com/oasisprotocol/curve25519-voi v0.0.0-20230110094441-db37f07504ce // indirect github.com/oklog/run v1.1.0 // indirect - github.com/pelletier/go-toml/v2 v2.0.9 // indirect github.com/petermattis/goid v0.0.0-20230808133559-b036b712a89b // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect