代码拉取完成,页面将自动刷新
package cmd
import (
"encoding/json"
"errors"
"fmt"
"os"
"regexp"
"strings"
"gitee.com/exi-red/maketools/util"
"github.com/spf13/cobra"
)
// 子命令 air
const air = "air"
// 参数 配置文件
var airFlagFile = Params{
Name: "file",
Shorthand: "f",
Value: ".air.toml",
Usage: "air 配置文件",
}
// 参数 编译命令
var airFlagCmd = Params{
Name: "cmd",
Shorthand: "c",
Value: "",
Usage: "编译命令",
}
// 参数 运行命令
var airFlagBin = Params{
Name: "bin",
Shorthand: "b",
Value: "",
Usage: "运行命令",
}
// 参数 运行命令的参数
var airFlagBinArgs = Params{
Name: "bin-args",
Shorthand: "a",
Value: "",
Usage: "运行命令的参数",
}
// 参数 临时目录
var airFlagTmpDir = Params{
Name: "tmp-dir",
Shorthand: "t",
Value: "",
Usage: "临时目录",
}
// 注册子命令 air
var airCmd = &cobra.Command{
Use: air,
Short: `更新 .air.toml 文件的关键命令`,
Example: fmt.Sprintf(
` %s %s --%s ".air.toml" --%s "go build ..." --%s ".\run.exe" --%s "--port 80" --%s "dist"`,
ROOTNAME,
air,
airFlagFile.Name,
airFlagCmd.Name,
airFlagBin.Name,
airFlagBinArgs.Name,
airFlagTmpDir.Name,
),
Run: func(cmd *cobra.Command, args []string) {
// 处理器执行
err := airHanlder(cmd)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}
// 初始化
func init() {
// 注册参数
StringP(airCmd, airFlagFile)
StringP(airCmd, airFlagCmd)
StringP(airCmd, airFlagBin)
StringP(airCmd, airFlagBinArgs)
StringP(airCmd, airFlagTmpDir)
// 添加到主命令
rootCmd.AddCommand(airCmd)
}
// 复制资源处理器
func airHanlder(cmd *cobra.Command) error {
// air 配置文件
file, _ := cmd.Flags().GetString(airFlagFile.Name)
if file == "" {
return errors.New("no configuration file specified")
}
file = util.PathFormat(file)
// 编译命令
build, _ := cmd.Flags().GetString(airFlagCmd.Name)
if build == "" {
return errors.New("no compilation command specified")
}
build = strings.ReplaceAll(build, "\\", "/")
// 运行命令
run, _ := cmd.Flags().GetString(airFlagBin.Name)
if run == "" {
return errors.New("no run command specified")
}
run = strings.ReplaceAll(run, "\\", "/")
// 运行命令参数
runArg, _ := cmd.Flags().GetString(airFlagBinArgs.Name)
runArgs := make([]string, 0)
if runArg != "" {
for _, arg := range strings.Split(runArg, " ") {
arg = strings.TrimSpace(arg)
if arg != "" {
runArgs = append(runArgs, arg)
}
}
}
stringRunArgs, _ := json.Marshal(runArgs)
// 运行命令
tmpDir, _ := cmd.Flags().GetString(airFlagTmpDir.Name)
if tmpDir == "" {
return errors.New("no run command specified")
}
tmpDir = strings.ReplaceAll(tmpDir, "\\", "/")
// 读取文件
content, err := os.ReadFile(file)
if err != nil {
return err
}
// 匹配 "hello" 开头的行
cmdRe := regexp.MustCompile(`(?m)(?m)^\s?(cmd)\s?=\s?(.*?)(# \[cmd] DO NOT DELETE #)`)
binRe := regexp.MustCompile(`(?m)(?m)^\s?(bin)\s?=\s?(.*?)(# \[bin] DO NOT DELETE #)`)
argRe := regexp.MustCompile(`(?s)(?m)^\s?(args_bin)\s?=\s?(.*?)(# \[args_bin] DO NOT DELETE #)`)
tmpDirRe := regexp.MustCompile(`(?s)(?m)^\s?(tmp_dir)\s?=\s?(.*?)(# \[tmp_dir] DO NOT DELETE #)`)
// 替换所有匹配的行
newContent := string(content)
newContent = cmdRe.ReplaceAllString(newContent, fmt.Sprintf(`$1 = "%s" $3`, build))
newContent = binRe.ReplaceAllString(newContent, fmt.Sprintf(`$1 = "%s" $3`, run))
newContent = argRe.ReplaceAllString(newContent, fmt.Sprintf(`$1 = %s $3`, string(stringRunArgs)))
newContent = tmpDirRe.ReplaceAllString(newContent, fmt.Sprintf(`$1 = "%s" $3`, tmpDir))
// 写回文件
err = os.WriteFile(file, []byte(newContent), 0644)
if err != nil {
return err
}
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。