go-zero/tools/goctl/api/dartgen/gen.go

52 lines
1.1 KiB
Go
Raw Normal View History

2020-07-29 17:11:41 +08:00
package dartgen
import (
"errors"
"fmt"
2020-07-29 17:11:41 +08:00
"strings"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
2020-07-29 17:11:41 +08:00
)
2021-02-20 19:50:03 +08:00
// DartCommand create dart network request code
2020-07-29 17:11:41 +08:00
func DartCommand(c *cli.Context) error {
apiFile := c.String("api")
dir := c.String("dir")
isLegacy := c.Bool("legacy")
hostname := c.String("hostname")
2020-07-29 17:11:41 +08:00
if len(apiFile) == 0 {
return errors.New("missing -api")
}
if len(dir) == 0 {
return errors.New("missing -dir")
}
if len(hostname) == 0 {
fmt.Println("you could use '-hostname' flag to specify your server hostname")
hostname = "go-zero.dev"
}
2020-07-29 17:11:41 +08:00
api, err := parser.Parse(apiFile)
2020-07-29 17:11:41 +08:00
if err != nil {
return err
}
if err := api.Validate(); err != nil {
return err
}
api.Service = api.Service.JoinPrefix()
2020-07-29 17:11:41 +08:00
if !strings.HasSuffix(dir, "/") {
dir = dir + "/"
}
api.Info.Title = strings.Replace(apiFile, ".api", "", -1)
logx.Must(genData(dir+"data/", api, isLegacy))
logx.Must(genApi(dir+"api/", api, isLegacy))
logx.Must(genVars(dir+"vars/", isLegacy, hostname))
if err := formatDir(dir); err != nil {
logx.Errorf("failed to format, %v", err)
}
2020-07-29 17:11:41 +08:00
return nil
}