From 4c28b8ed87f5a4550bed113eafb2e8a77ddf6086 Mon Sep 17 00:00:00 2001 From: levisyin <150114626+levisyin@users.noreply.github.com> Date: Mon, 15 Jan 2024 15:48:14 +0800 Subject: [PATCH] refactor(confix): using `filepath.Join` instead of using `fmt.Sprintf` (#19055) --- tools/confix/cmd/config.go | 4 ++++ tools/confix/cmd/diff.go | 3 ++- tools/confix/cmd/migrate.go | 3 ++- tools/confix/cmd/mutate.go | 5 +++-- tools/confix/cmd/view.go | 3 ++- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/tools/confix/cmd/config.go b/tools/confix/cmd/config.go index 3b3196aabf..2a75ae7e32 100644 --- a/tools/confix/cmd/config.go +++ b/tools/confix/cmd/config.go @@ -4,6 +4,10 @@ import ( "github.com/spf13/cobra" ) +const ( + tomlSuffix = ".toml" +) + // ConfigCommand contains all the confix commands // These command can be used to interactively update an application config value. func ConfigCommand() *cobra.Command { diff --git a/tools/confix/cmd/diff.go b/tools/confix/cmd/diff.go index 4ce4b4b026..f6d71bcaa5 100644 --- a/tools/confix/cmd/diff.go +++ b/tools/confix/cmd/diff.go @@ -3,6 +3,7 @@ package cmd import ( "errors" "fmt" + "path/filepath" "strings" "github.com/spf13/cobra" @@ -27,7 +28,7 @@ func DiffCommand() *cobra.Command { case len(args) > 1: configPath = args[1] case clientCtx.HomeDir != "": - configPath = fmt.Sprintf("%s/config/app.toml", clientCtx.HomeDir) + configPath = filepath.Join(clientCtx.HomeDir, "config", "app.toml") default: return errors.New("must provide a path to the app.toml or client.toml") } diff --git a/tools/confix/cmd/migrate.go b/tools/confix/cmd/migrate.go index 2b27c24478..ccab1f4e21 100644 --- a/tools/confix/cmd/migrate.go +++ b/tools/confix/cmd/migrate.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "path/filepath" "strings" "github.com/spf13/cobra" @@ -35,7 +36,7 @@ In case of any error in updating the file, no output is written.`, case len(args) > 1: configPath = args[1] case clientCtx.HomeDir != "": - configPath = fmt.Sprintf("%s/config/app.toml", clientCtx.HomeDir) + configPath = filepath.Join(clientCtx.HomeDir, "config", "app.toml") default: return errors.New("must provide a path to the app.toml or client.toml") } diff --git a/tools/confix/cmd/mutate.go b/tools/confix/cmd/mutate.go index 0f3c030d38..5cd89cbd5c 100644 --- a/tools/confix/cmd/mutate.go +++ b/tools/confix/cmd/mutate.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "path/filepath" "strings" "github.com/creachadair/tomledit" @@ -30,7 +31,7 @@ func SetCommand() *cobra.Command { clientCtx := client.GetClientContextFromCmd(cmd) if clientCtx.HomeDir != "" { - filename = fmt.Sprintf("%s/config/%s.toml", clientCtx.HomeDir, filename) + filename = filepath.Join(clientCtx.HomeDir, "config", filename+tomlSuffix) } plan := transform.Plan{ @@ -97,7 +98,7 @@ func GetCommand() *cobra.Command { clientCtx := client.GetClientContextFromCmd(cmd) if clientCtx.HomeDir != "" { - filename = fmt.Sprintf("%s/config/%s.toml", clientCtx.HomeDir, filename) + filename = filepath.Join(clientCtx.HomeDir, "config", filename+tomlSuffix) } doc, err := confix.LoadConfig(filename) diff --git a/tools/confix/cmd/view.go b/tools/confix/cmd/view.go index 0c845ce880..4b3b1ab327 100644 --- a/tools/confix/cmd/view.go +++ b/tools/confix/cmd/view.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "os" + "path/filepath" "github.com/pelletier/go-toml/v2" "github.com/spf13/cobra" @@ -23,7 +24,7 @@ func ViewCommand() *cobra.Command { clientCtx := client.GetClientContextFromCmd(cmd) if clientCtx.HomeDir != "" { - filename = fmt.Sprintf("%s/config/%s.toml", clientCtx.HomeDir, filename) + filename = filepath.Join(clientCtx.HomeDir, "config", filename+tomlSuffix) } file, err := os.ReadFile(filename)