2022-05-22 21:06:59 +08:00
|
|
|
|
// Package command ...
|
|
|
|
|
//
|
|
|
|
|
// Description : command ...
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 2022-05-22 20:03
|
|
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
2022-06-26 19:37:20 +08:00
|
|
|
|
"encoding/json"
|
2022-05-22 21:06:59 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"git.zhangdeman.cn/zhangdeman/command/define"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Golang 指令实例
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 20:04 2022/5/22
|
2022-06-14 20:04:19 +08:00
|
|
|
|
func Golang(workDir string, goCmdPath string) *golang {
|
|
|
|
|
if len(goCmdPath) == 0 {
|
|
|
|
|
goCmdPath = "go"
|
|
|
|
|
}
|
2022-05-22 21:06:59 +08:00
|
|
|
|
return &golang{
|
|
|
|
|
workDir: workDir,
|
2022-06-14 20:04:19 +08:00
|
|
|
|
goCmdPath: goCmdPath,
|
2022-05-22 21:06:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type golang struct {
|
|
|
|
|
workDir string
|
|
|
|
|
goCmdPath string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ResetGoCmdPath 重置go命令路径
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 20:13 2022/5/22
|
|
|
|
|
func (g *golang) ResetGoCmdPath(cmdPath string) *golang {
|
|
|
|
|
g.goCmdPath = cmdPath
|
|
|
|
|
return g
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ModInit go mod init 指令
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 20:10 2022/5/22
|
|
|
|
|
func (g *golang) ModInit(modPath string) *define.Result {
|
|
|
|
|
return Execute(g.workDir, g.goCmdPath, []string{"mod", "init", modPath})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ModTidy go mod tidy 命令
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 20:17 2022/5/22
|
|
|
|
|
func (g *golang) ModTidy() *define.Result {
|
|
|
|
|
return Execute(g.workDir, g.goCmdPath, []string{"mod", "tidy"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ModReplace 映射包
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 20:22 2022/5/22
|
|
|
|
|
func (g *golang) ModReplace(sourcePath string, replacePath string) *define.Result {
|
|
|
|
|
return Execute(g.workDir, g.goCmdPath, []string{
|
|
|
|
|
"mod",
|
|
|
|
|
"edit",
|
|
|
|
|
"-replace",
|
|
|
|
|
fmt.Sprintf("%s=%s", sourcePath, replacePath),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Proxy 设置 GOPROXY, 若要清空,proxyList设置为空即可
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 20:51 2022/5/22
|
|
|
|
|
func (g *golang) Proxy(proxyList []string) *golang {
|
|
|
|
|
_ = Export().Set("GOPROXY", strings.Join(proxyList, ","))
|
|
|
|
|
return g
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NoProxy 指定代码仓库不经过proxy
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 20:53 2022/5/22
|
|
|
|
|
func (g *golang) NoProxy(noProxyList []string) *golang {
|
|
|
|
|
_ = Export().Set("GONOPROXY", strings.Join(noProxyList, ","))
|
|
|
|
|
return g
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Private 设置私有的代码仓库
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 20:55 2022/5/22
|
|
|
|
|
func (g *golang) Private(privateList []string) *golang {
|
|
|
|
|
_ = Export().Set("GOPRIVATE", strings.Join(privateList, ","))
|
|
|
|
|
return g
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NoSumDB 不做sumdb校验
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 20:59 2022/5/22
|
|
|
|
|
func (g *golang) NoSumDB(noSumDBList []string) *golang {
|
|
|
|
|
_ = Export().Set("GONOSUMDB", strings.Join(noSumDBList, ","))
|
|
|
|
|
return g
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-26 19:37:20 +08:00
|
|
|
|
// GetRecommendGoProxyList 获取推荐的 GO PROXY 列表
|
2022-05-22 21:06:59 +08:00
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 21:00 2022/5/22
|
|
|
|
|
func (g *golang) GetRecommendGoProxyList() []string {
|
|
|
|
|
return []string{
|
|
|
|
|
"https://mirrors.aliyun.com/goproxy/", // 阿里云proxy
|
|
|
|
|
"https://goproxy.cn/", // 基于七牛云CDN
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-22 22:09:38 +08:00
|
|
|
|
|
|
|
|
|
// FormatCode 格式化代码
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 22:09 2022/5/22
|
|
|
|
|
func (g *golang) FormatCode() *define.Result {
|
2022-05-22 22:13:01 +08:00
|
|
|
|
return Execute(g.workDir, "gofmt", []string{"-w", "-l", "./*"})
|
2022-05-22 22:09:38 +08:00
|
|
|
|
}
|
2022-06-26 19:37:20 +08:00
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|