From c55aec82cec69fe8f61964ca7c2b9de8d764439b Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Fri, 13 Sep 2024 00:14:06 +0200 Subject: [PATCH 1/6] add global persistent flag for config file with default value --- cmd/root.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index f977065..528366f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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. @@ -25,14 +29,22 @@ func Execute() { } func init() { + cobra.OnInitialize(initConfig) + // Here you will define your flags and configuration settings. // 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().StringVar(&cfgFile, "configs", "c", "configs file (default is 'configs.json' in cwd)") // Cobra also supports local flags, which will only run // when this action is called directly. // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } + +func initConfig() { + if cfgFile == "" { + cfgFile = "configs.json" + } +} From 15e81cf2eaacf5bbf0793227fad6289bd92bb261 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Fri, 13 Sep 2024 00:15:56 +0200 Subject: [PATCH 2/6] using parameter to determine config name/path --- util/config.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/util/config.go b/util/config.go index 94a6997..1c2f3be 100644 --- a/util/config.go +++ b/util/config.go @@ -6,7 +6,7 @@ import ( "os" ) -func ReadConfigs() ([]*Config, error) { +func ReadConfigs(name string) ([]*Config, error) { file, err := os.Open("configs.json") if err != nil { @@ -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 } From 9e72eb5171051fbca223ae0fde7e43ebcee889ae Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Fri, 13 Sep 2024 00:16:14 +0200 Subject: [PATCH 3/6] passing config file path through commands to functions --- cmd/debug.go | 2 +- cmd/save.go | 2 +- cmd/set.go | 2 +- cmd/unset.go | 2 +- proxy/debug.go | 4 ++-- proxy/set.go | 4 ++-- proxy/unset.go | 6 +++--- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/debug.go b/cmd/debug.go index 8f991dc..e225a12 100644 --- a/cmd/debug.go +++ b/cmd/debug.go @@ -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) }, } diff --git a/cmd/save.go b/cmd/save.go index e93efb5..56f4e7a 100644 --- a/cmd/save.go +++ b/cmd/save.go @@ -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) } }, } diff --git a/cmd/set.go b/cmd/set.go index a919588..011ff22 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -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) }, } diff --git a/cmd/unset.go b/cmd/unset.go index 65a9917..c619448 100644 --- a/cmd/unset.go +++ b/cmd/unset.go @@ -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) }, } diff --git a/proxy/debug.go b/proxy/debug.go index 8430d06..90fe8ec 100644 --- a/proxy/debug.go +++ b/proxy/debug.go @@ -19,7 +19,7 @@ func mapCmdsToStr(commands []*util.Command) string { return strings.Join(output, "\n") } -func Debug() { +func Debug(cfgFile string) { proxy, _ := ReadSystemProxy() proxyServer := proxy.Server if proxyServer == "" { @@ -30,7 +30,7 @@ func Debug() { 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 diff --git a/proxy/set.go b/proxy/set.go index 00c0b7f..a531657 100644 --- a/proxy/set.go +++ b/proxy/set.go @@ -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 diff --git a/proxy/unset.go b/proxy/unset.go index f4eade3..c24b4b8 100644 --- a/proxy/unset.go +++ b/proxy/unset.go @@ -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 From 777de8f14480cbcd2a80c8eb4d42fa2481eb0f1c Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Fri, 13 Sep 2024 00:29:15 +0200 Subject: [PATCH 4/6] bugfix: using default value flag parameter --- cmd/root.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 528366f..79589a3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,22 +29,14 @@ func Execute() { } func init() { - cobra.OnInitialize(initConfig) - // Here you will define your flags and configuration settings. // Cobra supports persistent flags, which, if defined here, // will be global for your application. - rootCmd.PersistentFlags().StringVar(&cfgFile, "configs", "c", "configs file (default is 'configs.json' in cwd)") + 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. // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } - -func initConfig() { - if cfgFile == "" { - cfgFile = "configs.json" - } -} From c66937fdba30d42371bd75d4c15aea5759877f80 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Mon, 16 Sep 2024 14:22:43 +0200 Subject: [PATCH 5/6] bugfix: using configs filename on read --- util/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/config.go b/util/config.go index 1c2f3be..a29b506 100644 --- a/util/config.go +++ b/util/config.go @@ -7,7 +7,7 @@ import ( ) func ReadConfigs(name string) ([]*Config, error) { - file, err := os.Open("configs.json") + file, err := os.Open(name) if err != nil { return nil, err From 2d126e62939ef26ed165540a2aa4259dbbaa7f83 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Mon, 16 Sep 2024 14:23:04 +0200 Subject: [PATCH 6/6] add config detection to debug command --- proxy/debug.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/proxy/debug.go b/proxy/debug.go index 90fe8ec..9db4400 100644 --- a/proxy/debug.go +++ b/proxy/debug.go @@ -1,7 +1,10 @@ package proxy import ( + "errors" "fmt" + "os" + "path/filepath" "strings" "github.com/Baipyrus/ProxySwitcher/util" @@ -20,13 +23,21 @@ func mapCmdsToStr(commands []*util.Command) string { } 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)