From 9f9d651b0c79f1becffa1088bbc038c78bb6ac06 Mon Sep 17 00:00:00 2001 From: diwang Date: Thu, 30 Mar 2023 18:12:39 +0800 Subject: [PATCH] fix: save profile --- pkg/kt/command/config/save.go | 14 ++++++++------ pkg/kt/command/config/save_test.go | 11 +++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 pkg/kt/command/config/save_test.go diff --git a/pkg/kt/command/config/save.go b/pkg/kt/command/config/save.go index 1e9f2f35..46ab3e05 100644 --- a/pkg/kt/command/config/save.go +++ b/pkg/kt/command/config/save.go @@ -2,23 +2,25 @@ package config import ( "fmt" - "github.com/alibaba/kt-connect/pkg/kt/util" - "github.com/rs/zerolog/log" - "github.com/spf13/cobra" "io/ioutil" "os" "strings" + + "github.com/alibaba/kt-connect/pkg/kt/util" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" ) func SaveProfile(args []string) error { if len(args) != 1 { return fmt.Errorf("must specifiy a profile name") } - profile := profileFile(args[0]) + profile := args[0] + absoluteProfile := profileFile(profile) if !profileNamePattern.MatchString(profile) { return fmt.Errorf("invalid profile name, must only contains letter, number, underline, hyphen or dot") } - if _, err := os.Stat(profile); err == nil { + if _, err := os.Stat(absoluteProfile); err == nil { var answer string fmt.Printf("Profile '%s' already exists, overwrite ? (Y/N) ", args[0]) _, err = fmt.Scanln(&answer) @@ -30,7 +32,7 @@ func SaveProfile(args []string) error { if err != nil { return fmt.Errorf("unable to read config file: %s", err) } - err = ioutil.WriteFile(profile, bytesRead, 0644) + err = ioutil.WriteFile(absoluteProfile, bytesRead, 0644) if err != nil { return fmt.Errorf("unable to create profile file: %s", err) } diff --git a/pkg/kt/command/config/save_test.go b/pkg/kt/command/config/save_test.go new file mode 100644 index 00000000..840adbf4 --- /dev/null +++ b/pkg/kt/command/config/save_test.go @@ -0,0 +1,11 @@ +package config + +import ( + "fmt" + "testing" +) + +func TestSaveProfile(t *testing.T) { + err := SaveProfile([]string{"test"}) + fmt.Println(err) +}