mirror of
https://github.com/Baipyrus/ProxySwitcher.git
synced 2024-12-27 05:01:45 +00:00
saving new config after prompting for data
This commit is contained in:
parent
4f44a2a11f
commit
9d304b862c
16
cmd/save.go
16
cmd/save.go
@ -1,8 +1,11 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Baipyrus/ProxySwitcher/util"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,6 +21,19 @@ var saveCmd = &cobra.Command{
|
|||||||
var command string
|
var command string
|
||||||
fmt.Print("Command? ")
|
fmt.Print("Command? ")
|
||||||
fmt.Scanln(&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)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,3 +24,16 @@ func ReadConfigs() ([]*Config, error) {
|
|||||||
}
|
}
|
||||||
return config, nil
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user