wujian_develop_tool/api.go

135 lines
3.1 KiB
Go

package main
import (
"fmt"
"os"
"git.icechen.cn/pkg/wujian_develop_tool/config"
"git.icechen.cn/pkg/wujian_develop_tool/util"
"git.icechen.cn/pkg/wujian_develop_tool/template/api/deploy"
"git.icechen.cn/pkg/wujian_develop_tool/template/api/docker/golang"
"github.com/fatih/color"
_ "embed"
"github.com/urfave/cli/v2"
)
// ActionListApi API应用列表
func ActionListApi(c *cli.Context) error {
if err := config.Init(c); err != nil {
return err
}
color.Green("API应用列表:")
for _, api := range config.Config.HasApi {
color.Green("\t%s.%s", api.Name, api.Namespace)
color.Green("\t\t路径:\t%s", api.Root)
color.Green("\t\t类型:\t%s", api.Type)
color.Green("\t\t端口:\t%s", api.Port)
c := color.New(color.Bold, color.FgRed)
c.EnableColor()
_, err := c.Println(healthApi(api))
if err != nil {
return err
}
fmt.Print("\n")
}
return nil
}
// ActionNewApi 创建API应用
func ActionNewApi(c *cli.Context) error {
if err := config.Init(c); err != nil {
return err
}
appList := c.Args().Slice()
if len(appList) == 0 {
appName := ""
for {
appName = util.ReadLineWithMessage("请输入要创建的api名称: ")
if appName != "" {
break
} else {
color.Red("应用名称不能为空!")
}
}
appList = append(appList, appName)
}
for _, newApp := range appList {
namespace := ""
for {
namespace = util.ReadLineWithMessage(fmt.Sprintf("请输入%s所属的项目(namespace): ", newApp))
if namespace != "" {
break
} else {
color.Red("项目名称不能为空!")
}
}
color.Green("正在创建[%s]API应用...", newApp)
app := config.Config.Api.GetApp(namespace, newApp)
app = config.Api{
Namespace: namespace,
Name: newApp,
Root: fmt.Sprintf("app/%s/api/%s", namespace, newApp),
}
if util.ExistDir(app.Root) && !util.ReadBoolWithMessage(app.Root+"目录已存在,是否要覆盖app文件目录(y/n): ") {
return nil
}
err := os.MkdirAll(app.Root, os.ModePerm)
if err != nil {
return err
}
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/?(.*),建议为空): ")
err = deploy.GenDeploy(app)
if err != nil {
return err
}
err = golang.GenDockerfile(app)
if err != nil {
return err
}
err = config.Config.AppendAPI(app)
if err != nil {
return err
}
color.Green("[%s]API应用创建成功!!!\n\n", newApp)
}
return nil
}
// ActionFixApi 修复API应用
func ActionFixApi(c *cli.Context) error {
color.Red("开发中...")
return nil
}