refactor(confix): using filepath.Join instead of using fmt.Sprintf (#19055)

This commit is contained in:
levisyin 2024-01-15 15:48:14 +08:00 committed by GitHub
parent 27b8f026f4
commit 4c28b8ed87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 5 deletions

View File

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

View File

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

View File

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

View File

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

View File

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