command/golang.go
2022-06-26 19:48:30 +08:00

195 lines
4.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package command ...
//
// Description : command ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-05-22 20:03
package command
import (
"encoding/json"
"fmt"
"strings"
"git.zhangdeman.cn/zhangdeman/command/define"
)
// Golang 指令实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:04 2022/5/22
func Golang(workDir string, goCmdPath string) *golang {
if len(goCmdPath) == 0 {
goCmdPath = "go"
}
return &golang{
workDir: workDir,
goCmdPath: goCmdPath,
}
}
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
}
// GetRecommendGoProxyList 获取推荐的 GO PROXY 列表
//
// 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
}
}
// FormatCode 格式化代码
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:09 2022/5/22
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
}
// 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)
}