This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
blueprint.go
147 lines (132 loc) · 3.69 KB
/
blueprint.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package uadmin
import (
"bytes"
"fmt"
"github.com/jessevdk/go-flags"
"github.com/sergeyglazyrindev/uadmin/core"
"html/template"
"io/ioutil"
"os"
"regexp"
"strings"
)
type BlueprintCommand struct {
}
func (c BlueprintCommand) Proceed(subaction string, args []string) error {
var action string
var help string
var isCorrectActionPassed bool = false
commandRegistry := &core.CommandRegistry{
Actions: make(map[string]core.ICommand),
}
commandRegistry.AddAction("create", &CreateBlueprint{})
if len(os.Args) > 2 {
action = os.Args[2]
isCorrectActionPassed = commandRegistry.IsRegisteredCommand(action)
}
if !isCorrectActionPassed {
helpText := commandRegistry.MakeHelpText()
help = fmt.Sprintf(`
Please provide what do you want to do ?
%s
`, helpText)
fmt.Print(help)
return nil
}
return commandRegistry.RunAction(subaction, "", args)
}
func (c BlueprintCommand) GetHelpText() string {
return "Manage your blueprints"
}
type CreateBlueprintOptions struct {
Message string `short:"m" required:"true" description:"Describe what is this migration for"`
Name string `short:"n" required:"true" description:"Blueprint you'd like to create migration for'"`
}
type CreateBlueprint struct {
}
func prepareBlueprintGoPackageName(blueprintName string) string {
return strings.ReplaceAll(blueprintName, "-", "_")
}
func (command CreateBlueprint) Proceed(subaction string, args []string) error {
var opts = &CreateBlueprintOptions{}
parser := flags.NewParser(opts, flags.Default)
var err error
_, err = parser.ParseArgs(args)
if len(args) == 0 {
var help string = `
Please provide flags -n and -m which is name of blueprint and description of blueprint respectively
`
fmt.Printf(help)
return nil
}
if err != nil {
return err
}
name := prepareBlueprintGoPackageName(core.ASCIIRegex.ReplaceAllLiteralString(opts.Name, ""))
bluePrintPath := "blueprint/" + strings.ToLower(name)
if _, err := os.Stat(bluePrintPath); os.IsExist(err) {
panic(fmt.Sprintf("Blueprint %s does exist", name))
}
dirPath := "blueprint/" + strings.ToLower(name)
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
err = os.MkdirAll(dirPath, 0755)
if err != nil {
panic(err)
}
}
read, err := ioutil.ReadFile("go.mod")
if err != nil {
panic(err)
}
moduleNameRegexp := regexp.MustCompile("module\\s([^\\s]+)")
moduleName := moduleNameRegexp.FindAllSubmatch(read, 1)
const blueprint = `package {{.Name}}
import (
"github.com/gin-gonic/gin"
"{{ .ModuleName }}/blueprint/{{.Name}}/migrations"
"github.com/sergeyglazyrindev/uadmin/core"
)
type Blueprint struct {
core.Blueprint
}
func (b Blueprint) InitRouter(app core.IApp, group *gin.RouterGroup) {
}
func (b Blueprint) InitApp(app core.IApp) {
}
var ConcreteBlueprint = Blueprint{
core.Blueprint{
Name: "{{.Name}}",
Description: "{{.Message}}",
MigrationRegistry: migrations.BMigrationRegistry,
},
}
`
var blueprintTplBuffer bytes.Buffer
blueprintTpl := template.Must(template.New("blueprintmain").Parse(blueprint))
tplData := struct {
Name string
Message string
ModuleName string
}{
Name: name,
ModuleName: string(moduleName[0][1]),
Message: strings.ReplaceAll(core.ASCIIRegex.ReplaceAllLiteralString(opts.Message, ""), `"`, `\"`),
}
if err = blueprintTpl.Execute(&blueprintTplBuffer, tplData); err != nil {
panic(err)
}
err = ioutil.WriteFile(dirPath+"/"+name+".go", blueprintTplBuffer.Bytes(), 0755)
if err != nil {
panic(err)
}
fmt.Printf(
"Created blueprint with name %s\n",
opts.Name,
)
migrateCommand := MigrateCommand{}
migrateCommand.Proceed("create", []string{"-b", name, "-m", "initial"})
return nil
}
func (command CreateBlueprint) GetHelpText() string {
return "Create blueprint"
}