|
| 1 | +package magic |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "runtime" |
| 8 | + |
| 9 | + "github.com/pterm/pterm" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var goProxies = map[string]string{ |
| 14 | + "default": "https://proxy.golang.org,direct", |
| 15 | + "aliyun": "https://mirrors.aliyun.com/goproxy/,direct", |
| 16 | + "tsinghua": "https://mirrors.tuna.tsinghua.edu.cn/goproxy/,direct", |
| 17 | + "goproxy": "https://goproxy.cn,direct", |
| 18 | + "ustc": "https://goproxy.ustc.edu.cn,direct", |
| 19 | + "nju": "https://goproxy.njuer.org,direct", |
| 20 | +} |
| 21 | + |
| 22 | +var goCmd = &cobra.Command{ |
| 23 | + Use: "go", |
| 24 | + Short: "Manage Go module proxy settings", |
| 25 | + Long: `Switch between different Go module proxy sources. |
| 26 | +
|
| 27 | +Supported proxies: |
| 28 | + - default: Go Official (https://proxy.golang.org,direct) |
| 29 | + - aliyun: Alibaba Cloud (https://mirrors.aliyun.com/goproxy/,direct) |
| 30 | + - tsinghua: Tsinghua University (https://mirrors.tuna.tsinghua.edu.cn/goproxy/,direct) |
| 31 | + - goproxy: Goproxy China (https://goproxy.cn,direct) |
| 32 | + - ustc: USTC (https://goproxy.ustc.edu.cn,direct) |
| 33 | + - nju: Nanjing University (https://goproxy.njuer.org,direct) |
| 34 | +
|
| 35 | +Examples: |
| 36 | + spark magic go list # List all available proxies |
| 37 | + spark magic go use goproxy # Switch to Goproxy China |
| 38 | + spark magic go use default # Switch back to official proxy |
| 39 | + spark magic go current # Show current proxy settings`, |
| 40 | +} |
| 41 | + |
| 42 | +var goListCmd = &cobra.Command{ |
| 43 | + Use: "list", |
| 44 | + Short: "List all available Go module proxies", |
| 45 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 46 | + pterm.Info.Println("Available Go module proxies:") |
| 47 | + pterm.Println() |
| 48 | + |
| 49 | + for name, url := range goProxies { |
| 50 | + pterm.Printf(" %s: %s\n", pterm.Green(name), url) |
| 51 | + } |
| 52 | + return nil |
| 53 | + }, |
| 54 | +} |
| 55 | + |
| 56 | +var goUseCmd = &cobra.Command{ |
| 57 | + Use: "use <proxy-name>", |
| 58 | + Short: "Switch to specified Go module proxy", |
| 59 | + Args: cobra.ExactArgs(1), |
| 60 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 61 | + proxyName := args[0] |
| 62 | + url, ok := goProxies[proxyName] |
| 63 | + if !ok { |
| 64 | + return fmt.Errorf("unknown proxy: %s. Run 'spark magic go list' to see available proxies", proxyName) |
| 65 | + } |
| 66 | + |
| 67 | + return setGoProxy(proxyName, url) |
| 68 | + }, |
| 69 | +} |
| 70 | + |
| 71 | +var goCurrentCmd = &cobra.Command{ |
| 72 | + Use: "current", |
| 73 | + Short: "Show current Go proxy configuration", |
| 74 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 75 | + return showCurrentGoProxy() |
| 76 | + }, |
| 77 | +} |
| 78 | + |
| 79 | +func setGoProxy(name, url string) error { |
| 80 | + goBin := "go" |
| 81 | + if runtime.GOOS == "windows" { |
| 82 | + goBin = "go.exe" |
| 83 | + } |
| 84 | + |
| 85 | + cmd := exec.Command(goBin, "env", "-w", fmt.Sprintf("GOPROXY=%s", url)) |
| 86 | + cmd.Stdout = os.Stdout |
| 87 | + cmd.Stderr = os.Stderr |
| 88 | + |
| 89 | + if err := cmd.Run(); err != nil { |
| 90 | + return fmt.Errorf("failed to set Go proxy: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + pterm.Success.Printf("Switched to %s Go proxy: %s\n", name, url) |
| 94 | + |
| 95 | + if name == "default" { |
| 96 | + pterm.Info.Println("Reverted to official Go proxy.") |
| 97 | + } |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +func showCurrentGoProxy() error { |
| 103 | + goBin := "go" |
| 104 | + if runtime.GOOS == "windows" { |
| 105 | + goBin = "go.exe" |
| 106 | + } |
| 107 | + |
| 108 | + cmd := exec.Command(goBin, "env", "GOPROXY") |
| 109 | + output, err := cmd.Output() |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("failed to get Go proxy config: %w", err) |
| 112 | + } |
| 113 | + |
| 114 | + proxy := string(output) |
| 115 | + if len(proxy) > 0 && proxy[len(proxy)-1] == '\n' { |
| 116 | + proxy = proxy[:len(proxy)-1] |
| 117 | + } |
| 118 | + |
| 119 | + pterm.Info.Printf("Current GOPROXY: %s\n", proxy) |
| 120 | + |
| 121 | + for name, url := range goProxies { |
| 122 | + if proxy == url { |
| 123 | + pterm.Success.Printf("Using %s mirror\n", name) |
| 124 | + return nil |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if proxy == "" || proxy == "https://proxy.golang.org,direct" { |
| 129 | + pterm.Info.Println("Using default Go proxy") |
| 130 | + } else { |
| 131 | + pterm.Warning.Println("Using custom proxy") |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func init() { |
| 138 | + goCmd.AddCommand(goListCmd) |
| 139 | + goCmd.AddCommand(goUseCmd) |
| 140 | + goCmd.AddCommand(goCurrentCmd) |
| 141 | + MagicCmd.AddCommand(goCmd) |
| 142 | +} |
0 commit comments