wujian_develop_tool/api.go

124 lines
2.8 KiB
Go
Raw Permalink Normal View History

2022-01-05 20:25:29 +08:00
package main
import (
"fmt"
2022-01-06 23:34:37 +08:00
"git.icechen.cn/pkg/wujian_develop_tool/config"
2022-01-06 23:04:25 +08:00
"git.icechen.cn/pkg/wujian_develop_tool/util"
2022-01-06 20:27:18 +08:00
"os"
2022-01-06 23:04:25 +08:00
"git.icechen.cn/pkg/wujian_develop_tool/template/api/deploy"
2022-01-06 20:27:18 +08:00
2022-01-06 23:04:25 +08:00
"git.icechen.cn/pkg/wujian_develop_tool/template/api/docker/golang"
2022-01-05 20:25:29 +08:00
"github.com/fatih/color"
2022-01-06 20:27:18 +08:00
_ "embed"
2022-01-05 20:25:29 +08:00
"github.com/urfave/cli/v2"
)
2022-01-06 20:27:18 +08:00
// ActionListApi API应用列表
func ActionListApi(c *cli.Context) error {
2022-01-06 23:34:37 +08:00
if err := config.Init(c); err != nil {
2022-01-05 20:25:29 +08:00
return err
}
color.Green("API应用列表:")
2022-01-06 23:34:37 +08:00
for _, api := range config.Config.HasApi {
2022-01-05 20:25:29 +08:00
color.Green("\t%s", api.Name)
color.Green("\t\t路径:\t%s", api.Root)
color.Green("\t\t类型:\t%s", api.Type)
color.Green("\t\t端口:\t%s", api.Port)
2022-01-06 20:27:18 +08:00
c := color.New(color.Bold, color.FgRed)
2022-01-05 20:25:29 +08:00
c.EnableColor()
_, err := c.Println(healthApi(api))
if err != nil {
return err
}
fmt.Print("\n")
}
return nil
}
2022-01-06 00:19:22 +08:00
// ActionNewApi 创建API应用
func ActionNewApi(c *cli.Context) error {
2022-01-06 23:34:37 +08:00
if err := config.Init(c); err != nil {
2022-01-06 20:27:18 +08:00
return err
}
appList := c.Args().Slice()
if len(appList) == 0 {
2022-01-06 22:32:03 +08:00
appName := ""
for {
appName = util.ReadLineWithMessage("请输入要创建的api名称: ")
if appName != "" {
break
} else {
color.Red("应用名称不能为空!")
}
}
2022-01-06 20:27:18 +08:00
appList = append(appList, appName)
}
for _, newApp := range appList {
color.Green("正在创建[%s]API应用...", newApp)
2022-01-06 23:34:37 +08:00
app := config.Config.Api.GetApp(newApp)
app = config.Api{
2022-01-06 22:32:03 +08:00
Name: newApp,
Root: "app/api/" + newApp,
}
2022-01-06 20:27:18 +08:00
2022-01-06 22:32:03 +08:00
if util.ExistDir(app.Root) && !util.ReadBoolWithMessage(app.Root+"目录已存在,是否要覆盖app文件目录(y/n): ") {
2022-01-06 20:27:18 +08:00
return nil
}
2022-01-06 22:32:03 +08:00
err := os.MkdirAll(app.Root, os.ModePerm)
2022-01-06 20:27:18 +08:00
if err != nil {
return err
}
2022-01-06 22:32:03 +08:00
app.AliasName = util.ReadLineWithMessage("请输入应用的简短描述(别名): ")
for {
app.Type = util.ReadLineWithMessage("请输入应用的类型(默认golang): ")
if app.Type == "golang" || app.Type == "" {
app.Type = "golang"
break
} else {
color.Red("暂不支持: [%s]类型", app.Type)
}
}
for {
app.Port = util.ReadLineWithMessage("请输入应用暴露的端口: ")
if app.Port != "" {
break
} else {
color.Red("端口号不能为空!")
}
}
app.Host = util.ReadLineWithMessage("请输入API应用请求的域名(host,如:api.seamlesser.com,建议为空): ")
app.Path = util.ReadLineWithMessage("请输入API应用请求的路由(path,如:/uwe/core/?(.*),建议为空): ")
2022-01-06 20:27:18 +08:00
2022-01-06 23:34:37 +08:00
err = deploy.GenDeploy(config.Config.Name, app)
2022-01-06 20:27:18 +08:00
if err != nil {
return err
}
err = golang.GenDockerfile(newApp)
if err != nil {
return err
}
2022-01-06 23:34:37 +08:00
err = config.Config.AppendAPI(app)
2022-01-06 22:32:03 +08:00
if err != nil {
return err
}
2022-01-06 20:27:18 +08:00
color.Green("[%s]API应用创建成功!!!\n\n", newApp)
}
return nil
}
// ActionFixApi 修复API应用
func ActionFixApi(c *cli.Context) error {
2022-01-06 23:34:37 +08:00
color.Red("开发中...")
2022-01-06 00:19:22 +08:00
return nil
}