feat(confix): add view command (#17904)

This commit is contained in:
Julien Robert 2023-09-28 11:25:13 +02:00 committed by GitHub
parent e65083d9e4
commit f2d5aeb984
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 108 additions and 1 deletions

View File

@ -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.

View File

@ -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`.

View File

@ -17,6 +17,7 @@ func ConfigCommand() *cobra.Command {
DiffCommand(),
GetCommand(),
SetCommand(),
ViewCommand(),
HomeCommand(),
)

54
tools/confix/cmd/view.go Normal file
View File

@ -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
}

View File

@ -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"`))
}

View File

@ -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