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+ }
2153func 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 {
3365func 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