Merge pull request #14 from Baipyrus/configs-file-cli-flag

Add CLI Flag To Specify Config Location
This commit is contained in:
Baipyrus 2024-09-16 14:25:55 +02:00 committed by GitHub
commit 78cd3f1734
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 41 additions and 26 deletions

View File

@ -10,7 +10,7 @@ var debugCmd = &cobra.Command{
Use: "debug",
Short: "Output all parsed configurations for debugging",
Run: func(cmd *cobra.Command, args []string) {
proxy.Debug()
proxy.Debug(cfgFile)
},
}

View File

@ -6,14 +6,18 @@ import (
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "ProxySwitcher",
Short: "A simple internet proxy switching tool",
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
var (
cfgFile string
// rootCmd represents the base command when called without any subcommands
rootCmd = &cobra.Command{
Use: "ProxySwitcher",
Short: "A simple internet proxy switching tool",
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
)
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
@ -29,7 +33,7 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ProxySwitcher.yaml)")
rootCmd.PersistentFlags().StringVarP(&cfgFile, "configs", "c", "configs.json", "configurations file")
// Cobra also supports local flags, which will only run
// when this action is called directly.

View File

@ -38,7 +38,7 @@ var saveCmd = &cobra.Command{
var input string
fmt.Print("Save this data? (Y/n) ")
if input == "" || strings.ToLower(input) == "y" {
util.SaveConfig(config)
util.SaveConfig(cfgFile, config)
}
},
}

View File

@ -10,7 +10,7 @@ var setCmd = &cobra.Command{
Use: "set",
Short: "Enable the current internet proxy settings",
Run: func(cmd *cobra.Command, args []string) {
proxy.Set()
proxy.Set(cfgFile)
},
}

View File

@ -10,7 +10,7 @@ var unsetCmd = &cobra.Command{
Use: "unset",
Short: "Disable the current internet proxy settings",
Run: func(cmd *cobra.Command, args []string) {
proxy.Unset()
proxy.Unset(cfgFile)
},
}

View File

@ -1,7 +1,10 @@
package proxy
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/Baipyrus/ProxySwitcher/util"
@ -19,18 +22,26 @@ func mapCmdsToStr(commands []*util.Command) string {
return strings.Join(output, "\n")
}
func Debug() {
func Debug(cfgFile string) {
path, _ := filepath.Abs(cfgFile)
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
path = "[N/A]"
}
fmt.Printf("\nConfig:\n")
fmt.Printf("%s\n\n", path)
proxy, _ := ReadSystemProxy()
proxyServer := proxy.Server
if proxyServer == "" {
proxyServer = "[N/A]"
}
fmt.Println("\nSystem Proxy:")
fmt.Println("System Proxy:")
fmt.Printf("Enabled: %t\n", proxy.Enabled)
fmt.Printf("Server: %s\n\n", proxyServer)
configs, _ := util.ReadConfigs()
configs, _ := util.ReadConfigs(cfgFile)
for _, config := range configs {
configCmd := config.Name
// Use command instead of name, if given

View File

@ -4,7 +4,7 @@ import (
"github.com/Baipyrus/ProxySwitcher/util"
)
func Set() {
func Set(cfgFile string) {
stdin, closeFunc, _ := util.ReadyCmd()
proxy, _ := ReadSystemProxy()
@ -13,7 +13,7 @@ func Set() {
SetSystemProxy(true)
}
configs, _ := util.ReadConfigs()
configs, _ := util.ReadConfigs(cfgFile)
for _, config := range configs {
configCmd := config.Name
// Use command instead of name, if given

View File

@ -4,16 +4,16 @@ import (
"github.com/Baipyrus/ProxySwitcher/util"
)
func Unset() {
func Unset(cfgFile string) {
stdin, closeFunc, _ := util.ReadyCmd()
// Unset system proxy, if not already
proxy, _ := ReadSystemProxy()
// Unset system proxy, if not already
if proxy.Enabled {
SetSystemProxy(false)
}
configs, _ := util.ReadConfigs()
configs, _ := util.ReadConfigs(cfgFile)
for _, config := range configs {
configCmd := config.Name
// Use command instead of name, if given

View File

@ -6,8 +6,8 @@ import (
"os"
)
func ReadConfigs() ([]*Config, error) {
file, err := os.Open("configs.json")
func ReadConfigs(name string) ([]*Config, error) {
file, err := os.Open(name)
if err != nil {
return nil, err
@ -25,8 +25,8 @@ func ReadConfigs() ([]*Config, error) {
return config, nil
}
func SaveConfig(config Config) error {
configs, _ := ReadConfigs()
func SaveConfig(name string, config Config) error {
configs, _ := ReadConfigs(name)
configs = append(configs, &config)
data, err := json.Marshal(configs)
@ -34,6 +34,6 @@ func SaveConfig(config Config) error {
return err
}
err = os.WriteFile("configs.json", data, 0666)
err = os.WriteFile(name, data, 0666)
return err
}