forked from yakumioto/mgen
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.go
More file actions
61 lines (51 loc) · 1.24 KB
/
action.go
File metadata and controls
61 lines (51 loc) · 1.24 KB
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
package cmgen
import (
"gopkg.in/urfave/cli.v2"
"html/template"
"io/ioutil"
"log"
"os"
"path"
"strings"
"gopkg.in/yaml.v2"
)
func MgoAction(context *cli.Context) error {
configPath := context.String("config-file")
bytes, err := ioutil.ReadFile(configPath)
if err != nil {
log.Fatalf("[ERROR] read config file error: %s\n", err)
return err
}
mg := new(ModelGenerator)
mg.ConfigName = path.Base(configPath)
if err := yaml.Unmarshal(bytes, mg); err != nil {
log.Fatalf("[ERROR] unmarshal yaml error: %s\n", err)
return err
}
response, err := TemplateBoxes.FindString("cmgo.tmpl")
if err != nil {
log.Fatal(err)
return err
}
t, err := template.New("cmgo").Funcs(template.FuncMap{
"ToLower": strings.ToLower,
"SnakeString": SnakeString,
}).Parse(response)
if err != nil {
log.Fatalf("[ERROR] parse template files error: %s\n", err)
return err
}
filename := strings.Replace(configPath, path.Ext(configPath), ".mg.go", 1)
mg.FileName = filename
fp, err := os.Create(filename)
if err != nil {
log.Fatalf("[ERROR] create %s error: %s\n", filename, err)
return err
}
defer fp.Close()
if err := t.Execute(fp, mg); err != nil {
log.Fatalf("[ERROR] execute template error: %s\n", err)
return err
}
return nil
}