2020-07-29 17:11:41 +08:00
|
|
|
package dartgen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-03-04 15:34:13 +08:00
|
|
|
"fmt"
|
2020-07-29 17:11:41 +08:00
|
|
|
"strings"
|
|
|
|
|
2022-02-09 17:22:52 +08:00
|
|
|
"github.com/urfave/cli"
|
2022-01-25 23:15:07 +08:00
|
|
|
"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")
|
2022-03-04 15:34:13 +08:00
|
|
|
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")
|
|
|
|
}
|
2022-03-04 15:34:13 +08:00
|
|
|
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
|
|
|
|
2021-01-11 15:10:51 +08:00
|
|
|
api, err := parser.Parse(apiFile)
|
2020-07-29 17:11:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-03 20:57:03 +08:00
|
|
|
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)
|
2022-03-04 15:34:13 +08:00
|
|
|
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
|
|
|
|
}
|