From 9d304b862c0b506f87a980130a5416995eed848d Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sat, 31 Aug 2024 21:13:16 +0200 Subject: [PATCH] saving new config after prompting for data --- cmd/save.go | 16 ++++++++++++++++ util/config.go | 13 +++++++++++++ 2 files changed, 29 insertions(+) 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 +}