2020-08-14 09:02:32 +08:00
|
|
|
package ktgen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-08-14 10:35:35 +08:00
|
|
|
|
2022-05-07 15:40:11 +08:00
|
|
|
"github.com/spf13/cobra"
|
2022-02-09 17:22:52 +08:00
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
|
2020-08-14 09:02:32 +08:00
|
|
|
)
|
|
|
|
|
2022-05-07 15:40:11 +08:00
|
|
|
var (
|
|
|
|
// VarStringDir describes a directory.
|
|
|
|
VarStringDir string
|
|
|
|
// VarStringAPI describes an API.
|
|
|
|
VarStringAPI string
|
|
|
|
// VarStringPKG describes a package.
|
|
|
|
VarStringPKG string
|
|
|
|
)
|
|
|
|
|
2022-04-04 13:12:05 +08:00
|
|
|
// KtCommand generates kotlin code command entrance
|
2022-05-07 15:40:11 +08:00
|
|
|
func KtCommand(_ *cobra.Command, _ []string) error {
|
|
|
|
apiFile := VarStringAPI
|
2020-08-14 09:02:32 +08:00
|
|
|
if apiFile == "" {
|
|
|
|
return errors.New("missing -api")
|
|
|
|
}
|
2022-05-07 15:40:11 +08:00
|
|
|
dir := VarStringDir
|
2020-08-14 09:02:32 +08:00
|
|
|
if dir == "" {
|
|
|
|
return errors.New("missing -dir")
|
|
|
|
}
|
2022-05-07 15:40:11 +08:00
|
|
|
pkg := VarStringPKG
|
2020-08-14 09:02:32 +08:00
|
|
|
if pkg == "" {
|
|
|
|
return errors.New("missing -pkg")
|
|
|
|
}
|
|
|
|
|
2021-01-11 15:10:51 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-21 15:22:43 +08:00
|
|
|
if err := api.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-03 20:57:03 +08:00
|
|
|
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
|
|
|
|
}
|