From dad4d5728d19ae3ef84477d99e1beedf3d644951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 26 Jun 2022 19:37:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96GO=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E4=BB=A5=E5=8F=8A=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/golang.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ golang.go | 47 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 define/golang.go diff --git a/define/golang.go b/define/golang.go new file mode 100644 index 0000000..f873e13 --- /dev/null +++ b/define/golang.go @@ -0,0 +1,55 @@ +// Package define ... +// +// Description : define ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2022-06-26 19:16 +package define + +// GoENV go 环境变量 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:16 2022/6/26 +type GoENV struct { + GO111MODULE string `json:"GO111MODULE"` + GOARCH string `json:"GOARCH"` + GOBIN string `json:"GOBIN"` + GOCACHE string `json:"GOCACHE"` + GOENV string `json:"GOENV"` + GOEXE string `json:"GOEXE"` + GOEXPERIMENT string `json:"GOEXPERIMENT"` + GOFLAGS string `json:"GOFLAGS"` + GOHOSTARCH string `json:"GOHOSTARCH"` + GOHOSTOS string `json:"GOHOSTOS"` + GOINSECURE string `json:"GOINSECURE"` + GOMODCACHE string `json:"GOMODCACHE"` + GONOPROXY string `json:"GONOPROXY"` + GONOSUMDB string `json:"GONOSUMDB"` + GOOS string `json:"GOOS"` + GOPATH string `json:"GOPATH"` + GOPRIVATE string `json:"GOPRIVATE"` + GOPROXY string `json:"GOPROXY"` + GOROOT string `json:"GOROOT"` + GOSUMDB string `json:"GOSUMDB"` + GOTMPDIR string `json:"GOTMPDIR"` + GOTOOLDIR string `json:"GOTOOLDIR"` + GOVCS string `json:"GOVCS"` + GOVERSION string `json:"GOVERSION"` + GCCGO string `json:"GCCGO"` + GOAMD64 string `json:"GOAMD64"` + AR string `json:"AR"` + CC string `json:"CC"` + CXX string `json:"CXX"` + CGO_ENABLED string `json:"CGO_ENABLED"` + GOMOD string `json:"GOMOD"` + GOWORK string `json:"GOWORK"` + CGO_CFLAGS string `json:"CGO_CFLAGS"` + CGO_CPPFLAGS string `json:"CGO_CPPFLAGS"` + CGO_CXXFLAGS string `json:"CGO_CXXFLAGS"` + CGO_FFLAGS string `json:"CGO_FFLAGS"` + CGO_LDFLAGS string `json:"CGO_LDFLAGS"` + PKG_CONFIG string `json:"PKG_CONFIG"` + GOGCCFLAGS string `json:"GOGCCFLAGS"` +} diff --git a/golang.go b/golang.go index 2ca995d..a72fefd 100644 --- a/golang.go +++ b/golang.go @@ -8,6 +8,7 @@ package command import ( + "encoding/json" "fmt" "strings" @@ -116,7 +117,7 @@ func (g *golang) NoSumDB(noSumDBList []string) *golang { return g } -// GetRecommendGoProxyList 获取推荐的 GOPROXY列表 +// GetRecommendGoProxyList 获取推荐的 GO PROXY 列表 // // Author : go_developer@163.com<白茶清欢> // @@ -136,3 +137,47 @@ func (g *golang) GetRecommendGoProxyList() []string { func (g *golang) FormatCode() *define.Result { return Execute(g.workDir, "gofmt", []string{"-w", "-l", "./*"}) } + +// GetGoENV 获取环境变量 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:25 2022/6/26 +func (g *golang) GetGoENV() define.GoENV { + result := Execute(g.workDir, "go", []string{"env"}) + return g.FormatGoENV(string(result.Output)) +} + +// FormatGoENV 格式化环境变量 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:26 2022/6/26 +func (g *golang) FormatGoENV(sourceENV string) define.GoENV { + // fmt.Println(sourceENV) + envArr := strings.Split(sourceENV, "\n") + envTable := make(map[string]string) + for _, item := range envArr { + if len(item) == 0 { + continue + } + pair := strings.Split(item, "=") + if len(pair) == 0 { + continue + } + if len(pair) == 1 { + envTable[pair[0]] = "" + continue + } + + envTable[pair[0]] = strings.TrimRight( + strings.TrimLeft( + strings.Join(pair[1:], "="), "\"", + ), "\"", + ) + } + byteData, _ := json.Marshal(envTable) + var envResult define.GoENV + _ = json.Unmarshal(byteData, &envResult) + return envResult +}