go-zero/tools/goctl/api/ktgen/cmd.go

54 lines
933 B
Go
Raw Normal View History

2020-08-14 09:02:32 +08:00
package ktgen
import (
"errors"
2020-08-14 10:35:35 +08:00
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
2020-08-14 09:02:32 +08:00
)
var (
// VarStringDir describes a directory.
VarStringDir string
// VarStringAPI describes an API.
VarStringAPI string
// VarStringPKG describes a package.
VarStringPKG string
)
// KtCommand generates kotlin code command entrance
func KtCommand(_ *cobra.Command, _ []string) error {
apiFile := VarStringAPI
2020-08-14 09:02:32 +08:00
if apiFile == "" {
return errors.New("missing -api")
}
dir := VarStringDir
2020-08-14 09:02:32 +08:00
if dir == "" {
return errors.New("missing -dir")
}
pkg := VarStringPKG
2020-08-14 09:02:32 +08:00
if pkg == "" {
return errors.New("missing -pkg")
}
api, e := parser.Parse(apiFile)
2020-08-14 10:35:35 +08:00
if e != nil {
return e
2020-08-14 09:02:32 +08:00
}
if err := api.Validate(); err != nil {
return err
}
api.Service = api.Service.JoinPrefix()
2020-08-14 19:25:34 +08:00
e = genBase(dir, pkg, api)
if e != nil {
return e
}
e = genApi(dir, pkg, api)
if e != nil {
return e
}
2020-08-14 09:02:32 +08:00
return nil
}