saving new config after prompting for data

This commit is contained in:
Baipyrus 2024-08-31 21:13:16 +02:00
parent 4f44a2a11f
commit 9d304b862c
2 changed files with 29 additions and 0 deletions

View File

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

View File

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