command/golang.go

210 lines
4.8 KiB
Go
Raw Normal View History

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 (
"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 {
2023-04-09 22:44:37 +08:00
goCmdPath, _ = Check("go")
if len(goCmdPath) == 0 {
goCmdPath = "go"
}
2022-06-14 20:04:19 +08:00
}
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 {
2022-07-04 10:56:17 +08:00
_ = Execute(g.workDir, g.goCmdPath, []string{"mod", "tidy", "-go=1.16"})
_ = Execute(g.workDir, g.goCmdPath, []string{"mod", "tidy", "-go=1.17"})
2022-05-22 21:06:59 +08:00
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
}
// 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
}
// GetGoENV 获取环境变量
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:25 2022/6/26
func (g *golang) GetGoENV() define.GoENV {
2023-04-09 22:44:37 +08:00
result := Execute(g.workDir, g.goCmdPath, []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
}
2022-06-26 19:48:30 +08:00
// SwagInit 初始化swagger
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:45 2022/6/26
func (g *golang) SwagInit(param []string) *define.Result {
envResult := g.GetGoENV()
swagCmdPath := envResult.GOPATH + "/bin/swag"
return Execute(g.workDir, swagCmdPath, param)
}
2023-04-09 22:44:37 +08:00
// Version 获取版本信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:42 2023/4/9
func (g *golang) Version() (string, error) {
result := Execute(g.workDir, g.goCmdPath, []string{"version"})
return string(result.Output), result.Error
}