Skip to content

Commit 5d8d9aa

Browse files
committed
update git magic command
1 parent d929393 commit 5d8d9aa

File tree

4 files changed

+447
-0
lines changed

4 files changed

+447
-0
lines changed

cmd/magic/go.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}

cmd/magic/node.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package magic
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
8+
"github.com/pterm/pterm"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
var npmRegistries = map[string]string{
13+
"default": "https://registry.npmjs.org/",
14+
"taobao": "https://registry.npmmirror.com/",
15+
"aliyun": "https://registry.npmmirror.com/",
16+
"tencent": "https://mirrors.cloud.tencent.com/npm/",
17+
"huawei": "https://mirrors.huaweicloud.com/repository/npm/",
18+
"ustc": "https://npmreg.mirrors.ustc.edu.cn/",
19+
}
20+
21+
var nodeCmd = &cobra.Command{
22+
Use: "node",
23+
Short: "Manage Node.js npm registry settings",
24+
Long: `Switch between different npm registry mirrors.
25+
26+
Supported registries:
27+
- default: npm Official (https://registry.npmjs.org/)
28+
- taobao: npmmirror (Taobao) (https://registry.npmmirror.com/)
29+
- aliyun: npmmirror (Aliyun) (https://registry.npmmirror.com/)
30+
- tencent: Tencent Cloud (https://mirrors.cloud.tencent.com/npm/)
31+
- huawei: Huawei Cloud (https://mirrors.huaweicloud.com/repository/npm/)
32+
- ustc: USTC (https://npmreg.mirrors.ustc.edu.cn/)
33+
34+
Examples:
35+
spark magic node list # List all available registries
36+
spark magic node use taobao # Switch to npmmirror (Taobao)
37+
spark magic node use default # Switch back to official npm registry
38+
spark magic node current # Show current registry`,
39+
}
40+
41+
var nodeListCmd = &cobra.Command{
42+
Use: "list",
43+
Short: "List all available npm registries",
44+
RunE: func(cmd *cobra.Command, args []string) error {
45+
pterm.Info.Println("Available npm registries:")
46+
pterm.Println()
47+
48+
for name, url := range npmRegistries {
49+
pterm.Printf(" %s: %s\n", pterm.Green(name), url)
50+
}
51+
return nil
52+
},
53+
}
54+
55+
var nodeUseCmd = &cobra.Command{
56+
Use: "use <registry-name>",
57+
Short: "Switch to specified npm registry",
58+
Args: cobra.ExactArgs(1),
59+
RunE: func(cmd *cobra.Command, args []string) error {
60+
registryName := args[0]
61+
url, ok := npmRegistries[registryName]
62+
if !ok {
63+
return fmt.Errorf("unknown registry: %s. Run 'spark magic node list' to see available registries", registryName)
64+
}
65+
66+
return setNpmRegistry(registryName, url)
67+
},
68+
}
69+
70+
var nodeCurrentCmd = &cobra.Command{
71+
Use: "current",
72+
Short: "Show current npm registry configuration",
73+
RunE: func(cmd *cobra.Command, args []string) error {
74+
return showCurrentNpmRegistry()
75+
},
76+
}
77+
78+
func setNpmRegistry(name, url string) error {
79+
npmBin := "npm"
80+
81+
cmd := exec.Command(npmBin, "config", "set", "registry", url)
82+
cmd.Stdout = os.Stdout
83+
cmd.Stderr = os.Stderr
84+
85+
if err := cmd.Run(); err != nil {
86+
return fmt.Errorf("failed to set npm registry: %w", err)
87+
}
88+
89+
pterm.Success.Printf("Switched to %s npm registry: %s\n", name, url)
90+
91+
if name == "default" {
92+
pterm.Info.Println("Reverted to official npm registry.")
93+
}
94+
95+
return nil
96+
}
97+
98+
func showCurrentNpmRegistry() error {
99+
npmBin := "npm"
100+
101+
cmd := exec.Command(npmBin, "config", "get", "registry")
102+
output, err := cmd.Output()
103+
if err != nil {
104+
return fmt.Errorf("failed to get npm registry config: %w", err)
105+
}
106+
107+
registry := string(output)
108+
if len(registry) > 0 && registry[len(registry)-1] == '\n' {
109+
registry = registry[:len(registry)-1]
110+
}
111+
112+
pterm.Info.Printf("Current npm registry: %s\n", registry)
113+
114+
for name, url := range npmRegistries {
115+
if registry == url {
116+
pterm.Success.Printf("Using %s mirror\n", name)
117+
return nil
118+
}
119+
}
120+
121+
if registry == "https://registry.npmjs.org/" {
122+
pterm.Info.Println("Using default npm registry")
123+
} else {
124+
pterm.Warning.Println("Using custom registry")
125+
}
126+
127+
return nil
128+
}
129+
130+
func init() {
131+
nodeCmd.AddCommand(nodeListCmd)
132+
nodeCmd.AddCommand(nodeUseCmd)
133+
nodeCmd.AddCommand(nodeCurrentCmd)
134+
MagicCmd.AddCommand(nodeCmd)
135+
}

0 commit comments

Comments
 (0)