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

63 lines
1.4 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/spf13/cobra"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
2020-07-29 17:11:41 +08:00
)
var (
// VarStringDir describes the directory.
VarStringDir string
// VarStringAPI defines the API.
VarStringAPI string
// VarStringLegacy describes whether legacy.
VarStringLegacy bool
// VarStringHostname defines the hostname.
VarStringHostname string
)
2021-02-20 19:50:03 +08:00
// DartCommand create dart network request code
func DartCommand(_ *cobra.Command, _ []string) error {
apiFile := VarStringAPI
dir := VarStringDir
isLegacy := VarStringLegacy
hostname := VarStringHostname
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
}