Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create config fix tool #14342

Merged
merged 32 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
bc015b1
feat: create config fix tool
julienrbrt Dec 16, 2022
3760019
Merge branch 'main' into julien/config-fix
julienrbrt Jan 2, 2023
d3909d9
wip
julienrbrt Jan 4, 2023
6822b81
updates
julienrbrt Jan 4, 2023
fbe2f40
wip
julienrbrt Jan 4, 2023
b0fcfa9
Merge branch 'main' into julien/config-fix
julienrbrt Jan 4, 2023
52325e9
updates
julienrbrt Jan 5, 2023
c4130ff
update changelog
julienrbrt Jan 5, 2023
5588b8f
Merge branch 'main' into julien/config-fix
julienrbrt Jan 5, 2023
61924ff
add tests and diff command
julienrbrt Jan 5, 2023
34388a6
updates
julienrbrt Jan 5, 2023
81a8f52
renaming
julienrbrt Jan 5, 2023
25d24fc
updates
julienrbrt Jan 5, 2023
79eb103
wip
julienrbrt Jan 6, 2023
f2cabde
Merge branch 'main' into julien/config-fix
julienrbrt Jan 6, 2023
ee11e8b
naming
julienrbrt Jan 6, 2023
a98c4ee
store value in diff
julienrbrt Jan 6, 2023
77be207
finish migrate (only comments addition missing)
julienrbrt Jan 6, 2023
14210fa
Merge branch 'main' into julien/config-fix
julienrbrt Jan 8, 2023
1dfd460
add section comments
julienrbrt Jan 8, 2023
c425738
parse comments from diff
julienrbrt Jan 8, 2023
1643cc7
remove dep and imp tests
julienrbrt Jan 9, 2023
86fd670
Merge branch 'main' into julien/config-fix
julienrbrt Jan 9, 2023
598817d
chore: add docs
julienrbrt Jan 9, 2023
d6f21ca
fix tests
julienrbrt Jan 9, 2023
942ae4e
fix tests
julienrbrt Jan 9, 2023
de3794d
Merge branch 'main' into julien/config-fix
julienrbrt Jan 9, 2023
bda3028
Merge branch 'main' into julien/config-fix
julienrbrt Jan 9, 2023
a6bac20
use tagged version
julienrbrt Jan 9, 2023
a99937f
Merge branch 'main' into julien/config-fix
julienrbrt Jan 10, 2023
d87310d
go mod tidy all
julienrbrt Jan 10, 2023
4e19b7f
go mod tidy
julienrbrt Jan 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix tests
  • Loading branch information
julienrbrt committed Jan 9, 2023
commit 942ae4e3c9c5e7cd1da77c9ba4644b6541c45494
34 changes: 32 additions & 2 deletions tools/confix/cmd/condiff/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,41 @@ package main
import (
"os"

confixcmd "cosmossdk.io/tools/confix/cmd"
"cosmossdk.io/tools/confix"
"github.com/spf13/cobra"
)

func main() {
if err := confixcmd.DiffCommand().Execute(); err != nil {
cmd := &cobra.Command{
Use: "condiff f1 f2",
Short: "Diff the keyspaces of the TOML documents in files f1 and f2",
Long: `Diff the keyspaces of the TOML documents in files f1 and f2.
The output prints one line per key that differs:

-S name -- section exists in f1 but not f2
+S name -- section exists in f2 but not f1
-M name -- mapping exists in f1 but not f2
+M name -- mapping exists in f2 but not f1

Comments, order, and values are ignored for comparison purposes.`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
lhs, err := confix.LoadConfig(args[0])
if err != nil {
return err
}

rhs, err := confix.LoadConfig(args[1])
if err != nil {
return err
}

confix.PrintDiff(cmd.OutOrStdout(), confix.DiffKeys(lhs, rhs))
return nil
},
}

if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
1 change: 1 addition & 0 deletions tools/confix/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func ConfigCommand() *cobra.Command {

cmd.AddCommand(
MigrateCommand(),
DiffCommand(),
GetCommand(),
SetCommand(),
)
Expand Down
28 changes: 4 additions & 24 deletions tools/confix/cmd/diff.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
package cmd

import (
"cosmossdk.io/tools/confix"
"github.com/spf13/cobra"
)

func DiffCommand() *cobra.Command {
return &cobra.Command{
Use: "condiff f1 f2",
Short: "Diff the keyspaces of the TOML documents in files f1 and f2",
Long: `Diff the keyspaces of the TOML documents in files f1 and f2.
The output prints one line per key that differs:

-S name -- section exists in f1 but not f2
+S name -- section exists in f2 but not f1
-M name -- mapping exists in f1 but not f2
+M name -- mapping exists in f2 but not f1

Comments, order, and values are ignored for comparison purposes.`,
Args: cobra.ExactArgs(2),
Use: "diff [config]",
Short: "Display the diff between the current config and the SDK default config",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
lhs, err := confix.LoadConfig(args[0])
if err != nil {
return err
}

rhs, err := confix.LoadConfig(args[1])
if err != nil {
return err
}

confix.PrintDiff(cmd.OutOrStdout(), confix.DiffDocs(lhs, rhs))
// TODO to implement in the next PR
return nil
},
}
Expand Down
3 changes: 1 addition & 2 deletions tools/confix/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"context"
"fmt"
"os"

"cosmossdk.io/tools/confix"
"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -50,7 +49,7 @@ In case of any error in updating the file, no output is written.`,

ctx := context.Background()
if FlagVerbose {
ctx = confix.WithLogWriter(ctx, os.Stderr)
ctx = confix.WithLogWriter(ctx, cmd.ErrOrStderr())
}

outputPath := filename
Expand Down
6 changes: 4 additions & 2 deletions tools/confix/cmd/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd_test

import (
"fmt"
"strings"
"testing"

"cosmossdk.io/tools/confix/cmd"
Expand All @@ -25,10 +26,11 @@ func TestMigradeCmd(t *testing.T) {
assert.ErrorContains(t, err, "no such file or directory")

// try to migrate from client.toml it should fail without --skip-validate
_, err = clitestutil.ExecTestCLICmd(clientCtx, cmd.MigrateCommand(), []string{"v0.45", fmt.Sprintf("%s/config/client.toml", t.TempDir())})
_, err = clitestutil.ExecTestCLICmd(clientCtx, cmd.MigrateCommand(), []string{"v0.46", fmt.Sprintf("%s/config/client.toml", clientCtx.HomeDir)})
assert.ErrorContains(t, err, "failed to migrate config")

// try to migrate from client.toml - it should work and give us a big diff
_, err = clitestutil.ExecTestCLICmd(clientCtx, cmd.MigrateCommand(), []string{"v0.45", fmt.Sprintf("%s/config/client.toml", t.TempDir()), "--skip-validate"})
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd.MigrateCommand(), []string{"v0.46", fmt.Sprintf("%s/config/client.toml", clientCtx.HomeDir), "--skip-validate", "--verbose"})
assert.NilError(t, err)
assert.Assert(t, strings.Contains(out.String(), "add app-db-backend key"))
}
3 changes: 1 addition & 2 deletions tools/confix/cmd/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"os"
"strings"

"cosmossdk.io/tools/confix"
Expand Down Expand Up @@ -69,7 +68,7 @@ func SetCommand() *cobra.Command {

ctx := cmd.Context()
if FlagVerbose {
ctx = confix.WithLogWriter(ctx, os.Stderr)
ctx = confix.WithLogWriter(ctx, cmd.ErrOrStderr())
}

return confix.Upgrade(ctx, plan, filename, outputPath, FlagSkipValidate)
Expand Down
4 changes: 2 additions & 2 deletions tools/confix/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ type Diff struct {
KV KV
}

// DiffDocs diffs the keyspaces of the TOML documents in files lhs and rhs.
// DiffKeys diffs the keyspaces of the TOML documents in files lhs and rhs.
// Comments, order, and values are ignored for comparison purposes.
func DiffDocs(lhs, rhs *tomledit.Document) []Diff {
func DiffKeys(lhs, rhs *tomledit.Document) []Diff {
diff := diffSections(lhs.Global, rhs.Global)

lsec, rsec := lhs.Sections, rhs.Sections
Expand Down
2 changes: 1 addition & 1 deletion tools/confix/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func PlanBuilder(from *tomledit.Document, to string) transform.Plan {

deletedSections := map[string]bool{}

diffs := DiffDocs(from, target)
diffs := DiffKeys(from, target)
for _, diff := range diffs {
diff := diff
kv := diff.KV
Expand Down