2023-06-15 21:22:51 +08:00
|
|
|
|
//
|
|
|
|
|
// cli_air.go
|
|
|
|
|
// Copyright (C) 2022 tiglog <me@tiglog.com>
|
|
|
|
|
//
|
|
|
|
|
// Distributed under terms of the MIT license.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
package console
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
|
2023-06-15 21:38:12 +08:00
|
|
|
|
"git.hexq.cn/tiglog/golib/gfile"
|
2023-06-15 21:22:51 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type cAir struct {
|
|
|
|
|
BaseCmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewAirCmd(cli IConsole) *cAir {
|
|
|
|
|
return &cAir{
|
|
|
|
|
BaseCmd{
|
|
|
|
|
Name: "serve",
|
|
|
|
|
Desc: "代码变动时自动重启应用",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *cAir) Init(args []string) {
|
|
|
|
|
var cmd string
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
cmd = "dev"
|
|
|
|
|
} else {
|
|
|
|
|
cmd = args[0]
|
|
|
|
|
args = args[1:]
|
|
|
|
|
}
|
|
|
|
|
if cmd == "conf" {
|
|
|
|
|
c.initConfCmd(args)
|
|
|
|
|
} else {
|
|
|
|
|
c.initDevCmd(args)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
func (c *cAir) initDevCmd(args []string) {
|
|
|
|
|
cmd := flag.NewFlagSet("dev", flag.ExitOnError)
|
|
|
|
|
cmd.Parse(args)
|
|
|
|
|
c.Action = func() error {
|
|
|
|
|
fmt.Println("run dev cmd")
|
|
|
|
|
cm := exec.Command("air")
|
|
|
|
|
cm.Stderr = os.Stderr
|
|
|
|
|
cm.Stdout = os.Stdout
|
|
|
|
|
err := cm.Start()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = cm.Wait()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *cAir) initConfCmd(args []string) {
|
|
|
|
|
cmd := flag.NewFlagSet("conf", flag.ExitOnError)
|
|
|
|
|
output := cmd.String("output", "./.air.toml", "指定文件路径")
|
|
|
|
|
show := cmd.Bool("show", false, "显示配置文件内容")
|
|
|
|
|
cmd.Parse(args)
|
|
|
|
|
c.Action = func() error {
|
|
|
|
|
if *show {
|
|
|
|
|
fmt.Println(conf)
|
|
|
|
|
} else {
|
|
|
|
|
if gfile.Exists(*output) {
|
|
|
|
|
fmt.Printf("Conf file %s exists, SKIP\n\n", *output)
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Printf("Writing conf to %s ...\n", *output)
|
|
|
|
|
ioutil.WriteFile(*output, []byte(conf), 0644)
|
|
|
|
|
fmt.Println("Done")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *cAir) GetHelp() string {
|
|
|
|
|
p1 := fmt.Sprintf("%s <command>\n%s\n\nSub Commands:\n", c.Name, c.Desc)
|
|
|
|
|
p2 := fmt.Sprintf("%10s: %s\n", "dev", "启动开发服务")
|
|
|
|
|
p3 := fmt.Sprintf("%10s: %s\n", "conf", "生成配置信息")
|
|
|
|
|
return p1 + p2 + p3
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var conf = `# [Air](https://github.com/cosmtrek/air) TOML 格式的配置文件
|
|
|
|
|
|
|
|
|
|
# 工作目录
|
|
|
|
|
# 使用 . 或绝对路径,请注意 tmp_dir 目录必须在 root 目录下
|
|
|
|
|
root = "."
|
|
|
|
|
tmp_dir = "var/tmp"
|
|
|
|
|
|
|
|
|
|
[build]
|
|
|
|
|
# 只需要写你平常编译使用的shell命令。你也可以使用 make
|
2023-08-17 17:15:25 +08:00
|
|
|
|
cmd = "go build -o ./var/tmp/main entry/http/main.go"
|
2023-06-15 21:22:51 +08:00
|
|
|
|
# 由 cmd 命令得到的二进制文件名
|
|
|
|
|
bin = "var/tmp/main"
|
|
|
|
|
# 自定义的二进制,可以添加额外的编译标识例如添加 GIN_MODE=release
|
|
|
|
|
# full_bin = "APP_ENV=dev APP_USER=air ./var/tmp/main"
|
|
|
|
|
# 监听以下文件扩展名的文件.
|
|
|
|
|
include_ext = ["go", "tpl", "tmpl", "html"]
|
|
|
|
|
# 忽略这些文件扩展名或目录
|
|
|
|
|
exclude_dir = ["assets", "var", "vendor", "frontend/node_modules"]
|
|
|
|
|
# 监听以下指定目录的文件
|
|
|
|
|
include_dir = []
|
|
|
|
|
# 排除以下文件
|
|
|
|
|
exclude_file = []
|
|
|
|
|
# 如果文件更改过于频繁,则没有必要在每次更改时都触发构建。可以设置触发构建的延迟时间
|
|
|
|
|
delay = 1000 # ms
|
|
|
|
|
# 发生构建错误时,停止运行旧的二进制文件。
|
|
|
|
|
stop_on_error = true
|
|
|
|
|
# air的日志文件名,该日志文件放置在你的 tmp_dir 中
|
|
|
|
|
log = "./var/log/air_errors.log"
|
|
|
|
|
|
|
|
|
|
[log]
|
|
|
|
|
# 显示日志时间
|
|
|
|
|
time = true
|
|
|
|
|
|
|
|
|
|
[color]
|
|
|
|
|
# 自定义每个部分显示的颜色。如果找不到颜色,使用原始的应用程序日志。
|
|
|
|
|
main = "magenta"
|
|
|
|
|
watcher = "cyan"
|
|
|
|
|
build = "yellow"
|
|
|
|
|
runner = "green"
|
|
|
|
|
|
|
|
|
|
[misc]
|
|
|
|
|
# 退出时删除tmp目录
|
|
|
|
|
clean_on_exit = true
|
|
|
|
|
`
|