diff --git a/cmd/save.go b/cmd/save.go index 7edeee2..8fc1166 100644 --- a/cmd/save.go +++ b/cmd/save.go @@ -1,8 +1,11 @@ package cmd import ( + "encoding/json" "fmt" + "strings" + "github.com/Baipyrus/ProxySwitcher/util" "github.com/spf13/cobra" ) @@ -18,6 +21,19 @@ var saveCmd = &cobra.Command{ var command string fmt.Print("Command? ") fmt.Scanln(&command) + + config := util.Config{Name: name, Cmd: command, Set: set, Unset: unset} + + fmt.Println("\n\nPlease confirm the following data:") + + data, _ := json.Marshal(config) + fmt.Printf("%s\n", string(data)) + + var input string + fmt.Print("Save this data? (Y/n) ") + if input == "" || strings.ToLower(input) == "y" { + util.SaveConfig(config) + } }, } diff --git a/util/config.go b/util/config.go index f983da2..94a6997 100644 --- a/util/config.go +++ b/util/config.go @@ -24,3 +24,16 @@ func ReadConfigs() ([]*Config, error) { } return config, nil } + +func SaveConfig(config Config) error { + configs, _ := ReadConfigs() + configs = append(configs, &config) + + data, err := json.Marshal(configs) + if err != nil { + return err + } + + err = os.WriteFile("configs.json", data, 0666) + return err +}