From 3fd3b44fdc4d5a93eccf1eee871d9410be5cc70c Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Mon, 2 Sep 2024 11:21:10 +0200 Subject: [PATCH] intruduce debug command to output parsed config as commands --- cmd/debug.go | 29 +++++++++++++++++++++++++++++ proxy/debug.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 cmd/debug.go create mode 100644 proxy/debug.go diff --git a/cmd/debug.go b/cmd/debug.go new file mode 100644 index 0000000..8f991dc --- /dev/null +++ b/cmd/debug.go @@ -0,0 +1,29 @@ +package cmd + +import ( + "github.com/Baipyrus/ProxySwitcher/proxy" + "github.com/spf13/cobra" +) + +// debugCmd represents the debug command +var debugCmd = &cobra.Command{ + Use: "debug", + Short: "Output all parsed configurations for debugging", + Run: func(cmd *cobra.Command, args []string) { + proxy.Debug() + }, +} + +func init() { + rootCmd.AddCommand(debugCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // debugCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // debugCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/proxy/debug.go b/proxy/debug.go new file mode 100644 index 0000000..bba1499 --- /dev/null +++ b/proxy/debug.go @@ -0,0 +1,45 @@ +package proxy + +import ( + "fmt" + "strings" + + "github.com/Baipyrus/ProxySwitcher/util" +) + +func mapCmdsToStr(commands []*util.Command) string { + var output []string + + for _, command := range commands { + cmdArgs := strings.Join(command.Arguments, " ") + cmdStr := fmt.Sprintf("%s %s", command.Name, cmdArgs) + output = append(output, cmdStr) + } + + return strings.Join(output, "\n") +} + +func Debug() { + proxy, _ := ReadSystemProxy() + fmt.Println("\nSystem Proxy:") + fmt.Printf("Enabled: %t\n", proxy.Enabled) + fmt.Printf("Server: %s\n\n", proxy.Server) + + configs, _ := util.ReadConfigs() + for _, config := range configs { + configCmd := config.Name + // Use command instead of name, if given + if config.Cmd != "" { + configCmd = config.Cmd + } + fmt.Printf("Loading commands for '%s':\n", config.Name) + + setCmds := generateCommands(config.Set, configCmd, "[PROXY PLACEHOLDER]") + fmt.Println("Set Commands:") + fmt.Printf("%s\n", mapCmdsToStr(setCmds)) + + unsetCmds := generateCommands(config.Unset, configCmd, "") + fmt.Println("Unset Commands:") + fmt.Printf("%s\n\n", mapCmdsToStr(unsetCmds)) + } +}