-
Notifications
You must be signed in to change notification settings - Fork 63
/
create.go
324 lines (284 loc) · 7.83 KB
/
create.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package create
import (
"context"
"fmt"
"os"
"os/exec"
"path"
"strings"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/packmanager"
"kraftkit.sh/tui/confirm"
"kraftkit.sh/tui/textinput"
"kraftkit.sh/unikraft/lib/template"
)
type CreateOptions struct {
ProjectName string `long:"project-name" usage:"Set the project name to the template"`
LibraryName string `long:"library-name" usage:"Set the library name to the template"`
LibraryKName string `long:"library-kname" usage:"Set the library kname to the template"`
Version string `long:"version" short:"v" usage:"Set the library version to the template"`
Description string `long:"description" usage:"Set the description to the template"`
AuthorName string `long:"author-name" usage:"Set the author name to the template"`
AuthorEmail string `long:"author-email" usage:"Set the author email to the template"`
InitialBranch string `long:"initial-branch" usage:"Set the initial branch name to the template"`
CopyrightHolder string `long:"copyright-holder" usage:"Set the copyright holder name to the template"`
Origin string `long:"origin" usage:"Source code origin URL"`
NoProvideCMain bool `long:"no-provide-c-main" usage:"Do not provide C main to the template"`
GitInit bool `long:"git-init" usage:"Init git through the creating library"`
WithPatchdir bool `long:"patch-dir" usage:"provide patch directory to the template"`
UpdateRefs bool `long:"update-refs" usage:"Softly pack the component so that it is available via kraft list"`
ProjectPath string `long:"project-path" usage:"Where to create library"`
}
// Create creates a library template.
func Create(ctx context.Context, opts *CreateOptions, args ...string) error {
if opts == nil {
opts = &CreateOptions{}
}
return opts.Run(ctx, args)
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&CreateOptions{}, cobra.Command{
Short: "Initialize a library from a template",
Use: "create [FLAGS] [NAME]",
Aliases: []string{"init"},
Long: heredoc.Doc(`
Creates a library template
`),
Example: heredoc.Doc(`
# Create a library template
$ kraft lib create
# Create a library template with a name
$ kraft lib create sample-project
`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "lib",
},
})
if err != nil {
panic(err)
}
return cmd
}
func (*CreateOptions) Pre(cmd *cobra.Command, _ []string) error {
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}
cmd.SetContext(ctx)
return nil
}
func (opts *CreateOptions) Run(ctx context.Context, args []string) error {
var err error
if len(args) > 0 {
opts.ProjectName = args[0]
}
cwd, err := os.Getwd()
if err != nil {
return err
}
if !config.G[config.KraftKit](ctx).NoPrompt {
if !opts.GitInit {
opts.GitInit, err = confirm.NewConfirm("Do you want to intialise library with git:")
if err != nil {
return err
}
}
if len(opts.ProjectName) == 0 {
opts.ProjectName, err = textinput.NewTextInput(
"Project name:",
"Project name cannot be empty",
"",
)
if err != nil {
return err
}
}
if len(opts.ProjectPath) == 0 {
opts.ProjectPath, err = textinput.NewTextInput(
"Work directory:",
"Where to create library template",
cwd,
)
if err != nil {
return err
}
}
if !opts.UpdateRefs {
opts.UpdateRefs, err = confirm.NewConfirm("Do you want to package it:")
if err != nil {
return err
}
}
if len(opts.LibraryName) == 0 {
opts.LibraryName, err = textinput.NewTextInput(
"Library name:",
"Library name cannot be empty",
"lib-"+opts.ProjectName,
)
if err != nil {
return err
}
}
if len(opts.LibraryKName) == 0 {
opts.LibraryKName, err = textinput.NewTextInput(
"Library kname:",
"Library kname cannot be empty",
"LIB"+strings.ToUpper(strings.ReplaceAll(opts.ProjectName, "-", "")),
)
if err != nil {
return err
}
}
if len(opts.Description) == 0 {
opts.Description, err = textinput.NewTextInput(
"Description:",
"Description",
"",
)
if err != nil {
return err
}
}
if len(opts.Version) == 0 {
opts.Version, err = textinput.NewTextInput(
"Version:",
"x.y.z",
"1.0.0",
)
if err != nil {
return err
}
}
if len(opts.AuthorName) == 0 {
opts.AuthorName, err = textinput.NewTextInput(
"Author name:",
"Author name cannot be empty",
os.Getenv("USER"),
)
if err != nil {
return err
}
}
if len(opts.AuthorEmail) == 0 {
initialValue := ""
cmd := exec.Command("git", "config", "--get", "user.email")
emailBytes, err := cmd.CombinedOutput()
if err == nil {
initialValue = string(emailBytes)
}
opts.AuthorEmail, err = textinput.NewTextInput(
"Author email:",
"Author email cannot be empty",
initialValue,
)
if err != nil {
return err
}
}
if opts.GitInit && len(opts.InitialBranch) == 0 {
opts.InitialBranch, err = textinput.NewTextInput(
"Initial branch:",
"Initial branch",
"staging",
)
if err != nil {
return err
}
}
if len(opts.Origin) == 0 {
opts.Origin, err = textinput.NewTextInput(
"Origin url:",
"Enter origin url",
"",
)
if err != nil {
return err
}
}
if len(opts.CopyrightHolder) == 0 {
opts.CopyrightHolder, err = textinput.NewTextInput(
"Copyright holder:",
"Copyright holder cannot be empty",
opts.AuthorName,
)
if err != nil {
return err
}
}
} else {
var errs []string
if len(opts.ProjectName) == 0 {
errs = append(errs, fmt.Errorf("project name cannot be empty").Error())
}
if len(opts.Version) == 0 {
errs = append(errs, fmt.Errorf("version cannot be empty").Error())
}
if len(opts.AuthorName) == 0 {
errs = append(errs, fmt.Errorf("author name cannot be empty").Error())
}
if len(opts.AuthorEmail) == 0 {
errs = append(errs, fmt.Errorf("author email cannot be empty").Error())
}
if len(errs) > 0 {
return fmt.Errorf(strings.Join(errs, "\n"))
}
if len(opts.LibraryName) == 0 {
opts.LibraryName = "lib-" + opts.ProjectName
}
if len(opts.LibraryKName) == 0 {
opts.LibraryKName = "LIB" + strings.ToUpper(strings.ReplaceAll(opts.ProjectName, "-", ""))
}
if len(opts.ProjectPath) == 0 {
opts.ProjectPath = cwd
}
if len(opts.CopyrightHolder) == 0 {
opts.CopyrightHolder = opts.AuthorName
}
if opts.GitInit {
opts.InitialBranch = "staging"
}
}
// Creating instance of Template
templ, err := template.NewTemplate(ctx,
template.WithGitInit(opts.GitInit),
template.WithProjectName(opts.ProjectName),
template.WithLibName(opts.LibraryName),
template.WithLibKName(opts.LibraryKName),
template.WithVersion(opts.Version),
template.WithDescription(opts.Description),
template.WithAuthorName(opts.AuthorName),
template.WithAuthorEmail(opts.AuthorEmail),
template.WithInitialBranch(opts.InitialBranch),
template.WithCopyrightHolder(opts.CopyrightHolder),
template.WithProvideCMain(!opts.NoProvideCMain),
template.WithPatchdir(opts.WithPatchdir),
template.WithOriginUrl(opts.Origin),
)
if err != nil {
return err
}
if err = templ.Generate(ctx, opts.ProjectPath); err != nil {
return err
}
// Packaging softly.
if opts.UpdateRefs {
packageManager := packmanager.G(ctx)
if err = packageManager.AddSource(ctx, path.Join(opts.ProjectPath, opts.ProjectName)); err != nil {
return err
}
config.G[config.KraftKit](ctx).Unikraft.Manifests = append(
config.G[config.KraftKit](ctx).Unikraft.Manifests,
path.Join(opts.ProjectPath, opts.ProjectName),
)
if err := config.M[config.KraftKit](ctx).Write(true); err != nil {
return err
}
if err = packageManager.Update(ctx); err != nil {
return err
}
}
return nil
}