-
Notifications
You must be signed in to change notification settings - Fork 13
/
generate_context.go
214 lines (170 loc) · 4.53 KB
/
generate_context.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package tools
import (
"fmt"
"go/build"
"path/filepath"
"github.com/sacloud/usacloud/define"
"github.com/sacloud/usacloud/schema"
)
type GenerateContext struct {
R string
C string
P string
ResourceDef map[string]*schema.Resource
}
func NewGenerateContext() *GenerateContext {
ctx := &GenerateContext{
ResourceDef: define.Resources,
}
return ctx
}
func (c *GenerateContext) SetCurrentR(k string) {
c.R = k
}
/*
Get name functions
*/
func (c *GenerateContext) CamelR() string {
return ToCamelCaseName(c.R)
}
func (c *GenerateContext) Camelr() string {
return ToCamelWithFirstLower(c.R)
}
func (c *GenerateContext) DashR() string {
return ToDashedName(c.R)
}
func (c *GenerateContext) SnakeR() string {
return ToSnakeCaseName(c.R)
}
func (c *GenerateContext) CamelC() string {
return ToCamelCaseName(c.C)
}
func (c *GenerateContext) Camelc() string {
return ToCamelWithFirstLower(c.C)
}
func (c *GenerateContext) DashC() string {
return ToDashedName(c.C)
}
func (c *GenerateContext) SnakeC() string {
return ToSnakeCaseName(c.C)
}
func (c *GenerateContext) CamelP() string {
return ToCamelCaseName(c.P)
}
func (c *GenerateContext) Camelp() string {
return ToCamelWithFirstLower(c.P)
}
func (c *GenerateContext) DashP() string {
return ToDashedName(c.P)
}
func (c *GenerateContext) SnakeP() string {
return ToSnakeCaseName(c.P)
}
func (c *GenerateContext) Gopath() string {
gopath := build.Default.GOPATH
gopath = filepath.SplitList(gopath)[0]
return gopath
}
/*
Get current context schema
*/
func (c *GenerateContext) CurrentResource() *schema.Resource {
return c.ResourceDef[c.R]
}
func (c *GenerateContext) CurrentCommand() *schema.Command {
return c.CurrentResource().Commands[c.C]
}
func (c *GenerateContext) CurrentParam() *schema.Schema {
return c.CurrentCommand().Params[c.P]
}
/*
Get contextual name functions
*/
func (c *GenerateContext) InputModelFileName() string {
return fmt.Sprintf("params_%s_gen.go", c.SnakeR())
}
func (c *GenerateContext) InputModelTypeName() string {
return fmt.Sprintf("%s%sParam", c.CamelC(), c.CamelR())
}
func (c *GenerateContext) InputParamFieldName() string {
return c.CamelP()
}
func (c *GenerateContext) InputParamFlagName() string {
return c.DashP()
}
func (c *GenerateContext) InputParamSetterFuncName() string {
n := c.CurrentParam().DestinationProp
if n == "" {
n = fmt.Sprintf("Set%s", c.CamelP())
}
return n
}
func (c *GenerateContext) InputParamDestinationName() string {
n := c.CurrentParam().DestinationProp
if n == "" {
n = c.CamelP()
}
return n
}
func (c *GenerateContext) InputParamCLIFlagName() string {
return ToCLIFlagName(c.P)
}
func (c *GenerateContext) InputParamVariableName() string {
return fmt.Sprintf("%sParam", ToCamelWithFirstLower(c.C))
}
func (c *GenerateContext) CommandFuncName() string {
return fmt.Sprintf("%s%s", c.CamelR(), c.CamelC())
}
func (c *GenerateContext) CompleteArgsFuncName() string {
return fmt.Sprintf("%s%sCompleteArgs", c.CamelR(), c.CamelC())
}
func (c *GenerateContext) CompleteFlagsFuncName() string {
return fmt.Sprintf("%s%sCompleteFlags", c.CamelR(), c.CamelC())
}
func (c *GenerateContext) CommandFileName(useCustomCommand bool) string {
if useCustomCommand {
return fmt.Sprintf("%s_%s.go", c.SnakeR(), c.SnakeC())
}
return fmt.Sprintf("%s_%s_gen.go", c.SnakeR(), c.SnakeC())
}
func (c *GenerateContext) CommandArgsCompletionFileName(useCustom bool) string {
if useCustom {
return fmt.Sprintf("%s_%s_args.go", c.SnakeR(), c.SnakeC())
}
return fmt.Sprintf("%s_%s_args_gen.go", c.SnakeR(), c.SnakeC())
}
func (c *GenerateContext) CommandFlagsCompletionFileName(useCustom bool) string {
if useCustom {
return fmt.Sprintf("%s_%s_flags.go", c.SnakeR(), c.SnakeC())
}
return fmt.Sprintf("%s_%s_flags_gen.go", c.SnakeR(), c.SnakeC())
}
func (c *GenerateContext) ResourceArgsCompletionFileName() string {
return fmt.Sprintf("%s_args_gen.go", c.SnakeR())
}
func (c *GenerateContext) ResourceFlagsCompletionFileName() string {
return fmt.Sprintf("%s_flags_gen.go", c.SnakeR())
}
func (c *GenerateContext) CLICommandsFileName() string {
return fmt.Sprintf("cli_%s_gen.go", c.SnakeR())
}
func (c *GenerateContext) CommandResourceName() string {
n := c.CurrentCommand().AltResource
if n == "" {
n = c.CurrentResource().AltResource
}
if n == "" {
n = c.CamelR()
}
return n
}
func (c *GenerateContext) FindResultFieldName() string {
n := c.CurrentCommand().ListResultFieldName
if n == "" {
n = c.CurrentResource().ListResultFieldName
}
if n == "" {
n = c.CamelR() + "s"
}
return n
}