cosmos-sdk/tools/confix/cmd/diff.go
Alex | Interchain Labs f5e8e517ab
chore: set up release/v0.53.x branch (#23660)
Co-authored-by: Zygimantas <5236121+Zygimantass@users.noreply.github.com>
Co-authored-by: Zygimantas <zygis@skip.build>
2025-02-12 15:48:20 -05:00

62 lines
1.6 KiB
Go

package cmd
import (
"fmt"
"maps"
"slices"
"github.com/spf13/cobra"
"cosmossdk.io/tools/confix"
"github.com/cosmos/cosmos-sdk/client"
)
func DiffCommand() *cobra.Command {
return &cobra.Command{
Use: "diff [target-version] <app-toml-path>",
Short: "Outputs all config values that are different from the app.toml defaults.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var filename string
clientCtx := client.GetClientContextFromCmd(cmd)
switch {
case len(args) > 1:
filename = args[1]
case clientCtx.HomeDir != "":
filename = fmt.Sprintf("%s/config/app.toml", clientCtx.HomeDir)
default:
return fmt.Errorf("must provide a path to the app.toml file")
}
targetVersion := args[0]
if _, ok := confix.Migrations[targetVersion]; !ok {
return fmt.Errorf("unknown version %q, supported versions are: %q", targetVersion, slices.Collect(maps.Keys(confix.Migrations)))
}
targetVersionFile, err := confix.LoadLocalConfig(targetVersion)
if err != nil {
panic(fmt.Errorf("failed to load internal config: %w", err))
}
rawFile, err := confix.LoadConfig(filename)
if err != nil {
return fmt.Errorf("failed to load config: %v", err)
}
diff := confix.DiffValues(rawFile, targetVersionFile)
if len(diff) == 0 {
return clientCtx.PrintString("All config values are the same as the defaults.\n")
}
if err := clientCtx.PrintString("The following config values are different from the defaults:\n"); err != nil {
return err
}
confix.PrintDiff(cmd.OutOrStdout(), diff)
return nil
},
}
}