proxy.go 810 B

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