You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/api/apigen/gen.go

79 lines
1.7 KiB
Go

4 years ago
package apigen
import (
_ "embed"
4 years ago
"errors"
"fmt"
"html/template"
4 years ago
"path/filepath"
"strings"
"github.com/gookit/color"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
4 years ago
)
//go:embed api.tpl
var apiTemplate string
4 years ago
var (
// VarStringOutput describes the output.
VarStringOutput string
// VarStringHome describes the goctl home.
VarStringHome string
// VarStringRemote describes the remote git repository.
VarStringRemote string
// VarStringBranch describes the git branch.
VarStringBranch string
)
// CreateApiTemplate create api template file
func CreateApiTemplate(_ *cobra.Command, _ []string) error {
apiFile := VarStringOutput
4 years ago
if len(apiFile) == 0 {
return errors.New("missing -o")
}
fp, err := pathx.CreateIfNotExist(apiFile)
4 years ago
if err != nil {
return err
}
defer fp.Close()
if len(VarStringRemote) > 0 {
repo, _ := util.CloneIntoGitHome(VarStringRemote, VarStringBranch)
if len(repo) > 0 {
VarStringHome = repo
}
}
if len(VarStringHome) > 0 {
pathx.RegisterGoctlHome(VarStringHome)
}
text, err := pathx.LoadTemplate(category, apiTemplateFile, apiTemplate)
if err != nil {
return err
}
baseName := pathx.FileNameWithoutExt(filepath.Base(apiFile))
4 years ago
if strings.HasSuffix(strings.ToLower(baseName), "-api") {
baseName = baseName[:len(baseName)-4]
} else if strings.HasSuffix(strings.ToLower(baseName), "api") {
baseName = baseName[:len(baseName)-3]
}
t := template.Must(template.New("etcTemplate").Parse(text))
4 years ago
if err := t.Execute(fp, map[string]string{
"gitUser": getGitName(),
"gitEmail": getGitEmail(),
"serviceName": baseName + "-api",
}); err != nil {
return err
}
fmt.Println(color.Green.Render("Done."))
4 years ago
return nil
}