Skip to content

Commit e22f049

Browse files
fix: 构建检测前测试网络环境
1 parent 3f58f1f commit e22f049

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

module/go_mod/gotree.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"fmt"
77
"log"
8+
"net/http"
89
"os"
910
"os/exec"
1011
"path/filepath"
@@ -18,6 +19,37 @@ import (
1819
"golang.org/x/mod/modfile"
1920
)
2021

22+
func checkNetworkEnvironment(ctx context.Context) bool {
23+
var goProxy []string
24+
25+
if strings.Contains(os.Getenv("GOPROXY"), ",") {
26+
goProxy = strings.Split(os.Getenv("GOPROXY"), ",")
27+
} else {
28+
goProxy = append(goProxy, os.Getenv("GOPROXY"))
29+
}
30+
networkEnvironment := false
31+
for range 3 {
32+
for _, j := range goProxy {
33+
if j == "direct" {
34+
continue
35+
}
36+
r, e := http.Get(j)
37+
if r != nil && r.StatusCode == http.StatusRequestTimeout {
38+
logctx.Use(ctx).Warn("test network environment http get timeout :" + j)
39+
r.Body.Close()
40+
continue
41+
}
42+
if e != nil {
43+
logctx.Use(ctx).Warn("test network environment http get error :" + e.Error())
44+
continue
45+
}
46+
logctx.Use(ctx).Debug("test network environment http get success :" + j)
47+
networkEnvironment = true
48+
break
49+
}
50+
}
51+
return networkEnvironment
52+
}
2153
func goModTidy(ctx context.Context, path string) error {
2254
logger := logctx.Use(ctx)
2355
logger.Debug("go mod tidy :" + path)
@@ -33,6 +65,10 @@ func goModTidy(ctx context.Context, path string) error {
3365
func buildScan(ctx context.Context) error {
3466
task := model.UseInspectionTask(ctx)
3567
logger := logctx.Use(ctx)
68+
69+
if !checkNetworkEnvironment(ctx) {
70+
return errors.New("network environment error")
71+
}
3672
modFilePath := filepath.Join(task.Dir(), "go.mod")
3773
logger.Debug("Reading go.mod", zap.String("path", modFilePath))
3874
if err := goModTidy(ctx, task.Dir()); err != nil {

0 commit comments

Comments
 (0)