forked from zeromicro/go-zero
/
proxy.go
44 lines (39 loc) · 816 Bytes
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package migrate
import (
"net/url"
"os"
"strings"
"github.com/mikokutou1/go-zero-m/core/stringx"
"github.com/mikokutou1/go-zero-m/tools/goctl/rpc/execx"
)
var (
defaultProxy = "https://goproxy.cn"
defaultProxies = []string{defaultProxy}
)
func goProxy() []string {
wd, err := os.Getwd()
if err != nil {
return defaultProxies
}
proxy, err := execx.Run("go env GOPROXY", wd)
if err != nil {
return defaultProxies
}
list := strings.FieldsFunc(proxy, func(r rune) bool {
return r == '|' || r == ','
})
var ret []string
for _, item := range list {
if len(item) == 0 {
continue
}
_, err = url.Parse(item)
if err == nil && !stringx.Contains(ret, item) {
ret = append(ret, item)
}
}
if !stringx.Contains(ret, defaultProxy) {
ret = append(ret, defaultProxy)
}
return ret
}