apigen/lib/main.go

116 lines
2.6 KiB
Go
Raw Normal View History

2021-09-06 12:54:09 +00:00
package lib
2020-10-17 22:31:19 +00:00
import (
2021-01-30 09:53:34 +00:00
"github.com/alecthomas/kong"
"github.com/pkg/errors"
2020-10-17 22:31:19 +00:00
"log"
)
var knownMethods map[string]bool = make(map[string]bool)
var httpMapper map[string]map[string]string = make(map[string]map[string]string)
var packageName string = "main"
2021-01-30 09:53:34 +00:00
var CLI struct {
Yaml struct {
Src string `arg help:"Source Dir"`
Fname string `arg help:"File to be generated"`
} `cmd help:"Gens YAML metamodel"`
Goserver struct {
Src string `arg help:"Source Dir"`
} `cmd help:"Gens GO Server impl"`
2021-09-06 12:54:09 +00:00
//Gin struct {
// Src string `arg help:"Source Dir"`
//} `cmd help:"Gens Gin Server impl"`
2021-01-30 09:53:34 +00:00
Gocli struct {
Src string `arg help:"Source Dir"`
Dst string `arg help:"Dst file"`
} `cmd help:"Gens Go Cli impl"`
2021-02-20 07:58:09 +00:00
Pycli struct {
Src string `arg help:"Source Dir"`
Dst string `arg help:"Dst file"`
} `cmd help:"Gens Python Cli impl"`
2021-01-30 09:53:34 +00:00
Ts struct {
Src string `arg help:"Source Dir"`
Dst string `arg help:"Dst file"`
} `cmd help:"Gens Typescript Cli impl"`
Http struct {
Src string `arg help:"Source Dir"`
Dst string `arg help:"Dst file"`
} `cmd help:"Gens Http call impl"`
2020-10-17 22:31:19 +00:00
}
2021-09-06 12:54:09 +00:00
func Run() {
2020-10-17 22:31:19 +00:00
2021-01-30 09:53:34 +00:00
var processor func() error
kong.ConfigureHelp(kong.HelpOptions{
NoAppSummary: false,
Summary: true,
Compact: true,
Tree: true,
Indenter: nil,
})
ctx := kong.Parse(&CLI)
var err error
var src string
switch ctx.Command() {
case "yaml <src> <fname>":
log.Printf("Gens YAML")
src = CLI.Yaml.Src
processor = func() error {
return processYaml(CLI.Yaml.Fname, nil)
2020-10-17 22:31:19 +00:00
}
2021-01-30 09:53:34 +00:00
case "goserver <src>":
log.Printf("Gen GO Server")
src = CLI.Goserver.Src
processor = func() error {
return processGoServerOutput(CLI.Goserver.Src + "/apigen.go")
2020-10-17 22:31:19 +00:00
}
2021-09-06 12:54:09 +00:00
//case "gin <src>":
// log.Printf("Gen Gin Server")
// src = CLI.Gin.Src
// processor = func() error {
// return processGinServerOutput(CLI.Gin.Src + "/apigen.go")
// }
2021-01-30 09:53:34 +00:00
case "gocli <src> <dst>":
log.Printf("Gen GO Client")
src = CLI.Gocli.Src
processor = func() error {
return processGoClientOutput(CLI.Gocli.Dst)
2020-12-12 12:21:12 +00:00
}
2021-02-20 07:58:09 +00:00
case "pycli <src> <dst>":
log.Printf("Gen Python Client")
src = CLI.Pycli.Src
processor = func() error {
return processPyClientOutput(CLI.Pycli.Dst)
}
2021-01-30 09:53:34 +00:00
case "ts <src> <dst>":
log.Printf("Gen TS Client")
src = CLI.Ts.Src
processor = func() error {
return processTSClientOutput(CLI.Ts.Dst)
2020-12-12 12:21:12 +00:00
}
2021-01-30 09:53:34 +00:00
case "http <src> <dst>":
log.Printf("Gen Http Client")
src = CLI.Http.Src
processor = func() error {
return processHttpCallOut(CLI.Http.Dst)
2020-12-12 12:21:12 +00:00
}
2021-01-30 09:53:34 +00:00
default:
err = errors.New("unknown option")
2020-10-17 22:31:19 +00:00
}
2021-01-30 09:53:34 +00:00
if err != nil {
panic(err)
2020-10-17 22:31:19 +00:00
}
2021-01-30 09:53:34 +00:00
err = load(src)
2020-10-17 22:31:19 +00:00
if err != nil {
panic(err)
}
2021-01-30 09:53:34 +00:00
err = processor()
2020-10-17 22:31:19 +00:00
2021-01-30 09:53:34 +00:00
if err != nil {
panic(err)
2020-10-17 22:31:19 +00:00
}
}