-
Notifications
You must be signed in to change notification settings - Fork 15
/
gen.go
78 lines (66 loc) · 1.72 KB
/
gen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package apigen
import (
_ "embed"
"errors"
"fmt"
"html/template"
"path/filepath"
"strings"
"github.com/gookit/color"
"github.com/spf13/cobra"
"github.com/suyuan32/goctls/util"
"github.com/suyuan32/goctls/util/pathx"
)
//go:embed api.tpl
var apiTemplate string
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
if len(apiFile) == 0 {
return errors.New("missing -o")
}
fp, err := pathx.CreateIfNotExist(apiFile)
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))
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))
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."))
return nil
}