-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
create.go
683 lines (595 loc) · 18.3 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
package create
import (
"errors"
"fmt"
"net/http"
"path"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/api"
"github.com/cli/cli/git"
"github.com/cli/cli/internal/config"
"github.com/cli/cli/internal/ghinstance"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/internal/run"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/pkg/prompt"
"github.com/spf13/cobra"
)
type CreateOptions struct {
HttpClient func() (*http.Client, error)
Config func() (config.Config, error)
IO *iostreams.IOStreams
Name string
Description string
Homepage string
Team string
Template string
EnableIssues bool
EnableWiki bool
Public bool
Private bool
Internal bool
ConfirmSubmit bool
GitIgnoreTemplate string
LicenseTemplate string
}
func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Command {
opts := &CreateOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Config: f.Config,
}
cmd := &cobra.Command{
Use: "create [<name>]",
Short: "Create a new repository",
Long: heredoc.Docf(`
Create a new GitHub repository.
When the current directory is a local git repository, the new repository will be added
as the "origin" git remote. Otherwise, the command will prompt to clone the new
repository into a sub-directory.
To create a repository non-interactively, supply the following:
- the name argument;
- the %[1]s--confirm%[1]s flag;
- one of %[1]s--public%[1]s, %[1]s--private%[1]s, or %[1]s--internal%[1]s.
To toggle off %[1]s--enable-issues%[1]s or %[1]s--enable-wiki%[1]s, which are enabled
by default, use the %[1]s--enable-issues=false%[1]s syntax.
`, "`"),
Args: cobra.MaximumNArgs(1),
Example: heredoc.Doc(`
# create a repository under your account using the current directory name
$ git init my-project
$ cd my-project
$ gh repo create
# create a repository with a specific name
$ gh repo create my-project
# create a repository in an organization
$ gh repo create cli/my-project
# disable issues and wiki
$ gh repo create --enable-issues=false --enable-wiki=false
`),
Annotations: map[string]string{
"help:arguments": heredoc.Doc(`
A repository can be supplied as an argument in any of the following formats:
- "OWNER/REPO"
- by URL, e.g. "https://github.com/OWNER/REPO"
`),
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
opts.Name = args[0]
}
if len(args) == 0 && (opts.GitIgnoreTemplate != "" || opts.LicenseTemplate != "") {
return &cmdutil.FlagError{Err: errors.New(".gitignore and license templates are added only when a specific repository name is passed")}
}
if opts.Template != "" && (opts.GitIgnoreTemplate != "" || opts.LicenseTemplate != "") {
return &cmdutil.FlagError{Err: errors.New(".gitignore and license templates are not added when template is provided")}
}
if !opts.IO.CanPrompt() {
if opts.Name == "" {
return &cmdutil.FlagError{Err: errors.New("name argument required when not running interactively")}
}
if !opts.Internal && !opts.Private && !opts.Public {
return &cmdutil.FlagError{Err: errors.New("`--public`, `--private`, or `--internal` required when not running interactively")}
}
}
if opts.Template != "" && (opts.Homepage != "" || opts.Team != "" || cmd.Flags().Changed("enable-issues") || cmd.Flags().Changed("enable-wiki")) {
return &cmdutil.FlagError{Err: errors.New("The `--template` option is not supported with `--homepage`, `--team`, `--enable-issues`, or `--enable-wiki`")}
}
if runF != nil {
return runF(opts)
}
return createRun(opts)
},
}
cmd.Flags().StringVarP(&opts.Description, "description", "d", "", "Description of the repository")
cmd.Flags().StringVarP(&opts.Homepage, "homepage", "h", "", "Repository home page `URL`")
cmd.Flags().StringVarP(&opts.Team, "team", "t", "", "The `name` of the organization team to be granted access")
cmd.Flags().StringVarP(&opts.Template, "template", "p", "", "Make the new repository based on a template `repository`")
cmd.Flags().BoolVar(&opts.EnableIssues, "enable-issues", true, "Enable issues in the new repository")
cmd.Flags().BoolVar(&opts.EnableWiki, "enable-wiki", true, "Enable wiki in the new repository")
cmd.Flags().BoolVar(&opts.Public, "public", false, "Make the new repository public")
cmd.Flags().BoolVar(&opts.Private, "private", false, "Make the new repository private")
cmd.Flags().BoolVar(&opts.Internal, "internal", false, "Make the new repository internal")
cmd.Flags().BoolVarP(&opts.ConfirmSubmit, "confirm", "y", false, "Skip the confirmation prompt")
cmd.Flags().StringVarP(&opts.GitIgnoreTemplate, "gitignore", "g", "", "Specify a gitignore template for the repository")
cmd.Flags().StringVarP(&opts.LicenseTemplate, "license", "l", "", "Specify an Open Source License for the repository")
_ = cmd.RegisterFlagCompletionFunc("gitignore", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
httpClient, err := opts.HttpClient()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
cfg, err := opts.Config()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
hostname, err := cfg.DefaultHost()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
results, err := listGitIgnoreTemplates(httpClient, hostname)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return results, cobra.ShellCompDirectiveNoFileComp
})
_ = cmd.RegisterFlagCompletionFunc("license", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
httpClient, err := opts.HttpClient()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
cfg, err := opts.Config()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
hostname, err := cfg.DefaultHost()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
licenses, err := listLicenseTemplates(httpClient, hostname)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var results []string
for _, license := range licenses {
results = append(results, fmt.Sprintf("%s\t%s", license.Key, license.Name))
}
return results, cobra.ShellCompDirectiveNoFileComp
})
return cmd
}
func createRun(opts *CreateOptions) error {
projectDir, projectDirErr := git.ToplevelDir()
isNameAnArg := false
isDescEmpty := opts.Description == ""
isVisibilityPassed := false
inLocalRepo := projectDirErr == nil
if opts.Name != "" {
isNameAnArg = true
} else {
if projectDirErr != nil {
return projectDirErr
}
opts.Name = path.Base(projectDir)
}
enabledFlagCount := 0
visibility := ""
if opts.Public {
enabledFlagCount++
visibility = "PUBLIC"
}
if opts.Private {
enabledFlagCount++
visibility = "PRIVATE"
}
if opts.Internal {
enabledFlagCount++
visibility = "INTERNAL"
}
if enabledFlagCount > 1 {
return fmt.Errorf("expected exactly one of `--public`, `--private`, or `--internal` to be true")
} else if enabledFlagCount == 1 {
isVisibilityPassed = true
}
cfg, err := opts.Config()
if err != nil {
return err
}
var gitIgnoreTemplate, repoLicenseTemplate string
gitIgnoreTemplate = opts.GitIgnoreTemplate
repoLicenseTemplate = opts.LicenseTemplate
// Trigger interactive prompt if name is not passed
if !isNameAnArg {
newName, newDesc, newVisibility, err := interactiveRepoCreate(isDescEmpty, isVisibilityPassed, opts.Name)
if err != nil {
return err
}
if newName != "" {
opts.Name = newName
}
if newDesc != "" {
opts.Description = newDesc
}
if newVisibility != "" {
visibility = newVisibility
}
} else {
// Go for a prompt only if visibility isn't passed
if !isVisibilityPassed {
newVisibility, err := getVisibility()
if err != nil {
return nil
}
visibility = newVisibility
}
httpClient, err := opts.HttpClient()
if err != nil {
return err
}
host, err := cfg.DefaultHost()
if err != nil {
return err
}
// GitIgnore and License templates not added when a template repository
// is passed, or when the confirm flag is set.
if opts.Template == "" && opts.IO.CanPrompt() && !opts.ConfirmSubmit {
if gitIgnoreTemplate == "" {
gt, err := interactiveGitIgnore(httpClient, host)
if err != nil {
return err
}
gitIgnoreTemplate = gt
}
if repoLicenseTemplate == "" {
lt, err := interactiveLicense(httpClient, host)
if err != nil {
return err
}
repoLicenseTemplate = lt
}
}
}
var repoToCreate ghrepo.Interface
if strings.Contains(opts.Name, "/") {
var err error
repoToCreate, err = ghrepo.FromFullName(opts.Name)
if err != nil {
return fmt.Errorf("argument error: %w", err)
}
} else {
host, err := cfg.DefaultHost()
if err != nil {
return err
}
repoToCreate = ghrepo.NewWithHost("", opts.Name, host)
}
input := repoCreateInput{
Name: repoToCreate.RepoName(),
Visibility: visibility,
OwnerLogin: repoToCreate.RepoOwner(),
TeamSlug: opts.Team,
Description: opts.Description,
HomepageURL: opts.Homepage,
HasIssuesEnabled: opts.EnableIssues,
HasWikiEnabled: opts.EnableWiki,
GitIgnoreTemplate: gitIgnoreTemplate,
LicenseTemplate: repoLicenseTemplate,
}
httpClient, err := opts.HttpClient()
if err != nil {
return err
}
var templateRepoMainBranch string
if opts.Template != "" {
var templateRepo ghrepo.Interface
apiClient := api.NewClientFromHTTP(httpClient)
templateRepoName := opts.Template
if !strings.Contains(templateRepoName, "/") {
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
if err != nil {
return err
}
templateRepoName = currentUser + "/" + templateRepoName
}
templateRepo, err = ghrepo.FromFullName(templateRepoName)
if err != nil {
return fmt.Errorf("argument error: %w", err)
}
repo, err := api.GitHubRepo(apiClient, templateRepo)
if err != nil {
return err
}
input.TemplateRepositoryID = repo.ID
templateRepoMainBranch = repo.DefaultBranchRef.Name
}
createLocalDirectory := opts.ConfirmSubmit
if !opts.ConfirmSubmit {
opts.ConfirmSubmit, err = confirmSubmission(input.Name, input.OwnerLogin, inLocalRepo)
if err != nil {
return err
}
}
if opts.ConfirmSubmit {
repo, err := repoCreate(httpClient, repoToCreate.RepoHost(), input)
if err != nil {
return err
}
stderr := opts.IO.ErrOut
stdout := opts.IO.Out
cs := opts.IO.ColorScheme()
isTTY := opts.IO.IsStdoutTTY()
if isTTY {
fmt.Fprintf(stderr, "%s Created repository %s on GitHub\n", cs.SuccessIconWithColor(cs.Green), ghrepo.FullName(repo))
} else {
fmt.Fprintln(stdout, ghrepo.GenerateRepoURL(repo, ""))
}
protocol, err := cfg.Get(repo.RepoHost(), "git_protocol")
if err != nil {
return err
}
remoteURL := ghrepo.FormatRemoteURL(repo, protocol)
if inLocalRepo {
_, err = git.AddRemote("origin", remoteURL)
if err != nil {
return err
}
if isTTY {
fmt.Fprintf(stderr, "%s Added remote %s\n", cs.SuccessIcon(), remoteURL)
}
} else {
if opts.IO.CanPrompt() {
if !createLocalDirectory && (gitIgnoreTemplate == "" && repoLicenseTemplate == "") {
err := prompt.Confirm(fmt.Sprintf(`Create a local project directory for "%s"?`, ghrepo.FullName(repo)), &createLocalDirectory)
if err != nil {
return err
}
} else if !createLocalDirectory && (gitIgnoreTemplate != "" || repoLicenseTemplate != "") {
err := prompt.Confirm(fmt.Sprintf(`Clone the remote project directory "%s"?`, ghrepo.FullName(repo)), &createLocalDirectory)
if err != nil {
return err
}
}
}
if createLocalDirectory && (gitIgnoreTemplate == "" && repoLicenseTemplate == "") {
path := repo.RepoName()
checkoutBranch := ""
if opts.Template != "" {
// NOTE: we cannot read `defaultBranchRef` from the newly created repository as it will
// be null at this time. Instead, we assume that the main branch name of the new
// repository will be the same as that of the template repository.
checkoutBranch = templateRepoMainBranch
}
if err := localInit(opts.IO, remoteURL, path, checkoutBranch); err != nil {
return err
}
if isTTY {
fmt.Fprintf(stderr, "%s Initialized repository in \"%s\"\n", cs.SuccessIcon(), path)
}
} else if createLocalDirectory && (gitIgnoreTemplate != "" || repoLicenseTemplate != "") {
_, err := git.RunClone(remoteURL, []string{})
if err != nil {
return err
}
}
}
return nil
}
fmt.Fprintln(opts.IO.Out, "Discarding...")
return nil
}
func interactiveGitIgnore(client *http.Client, hostname string) (string, error) {
var addGitIgnore bool
var addGitIgnoreSurvey []*survey.Question
addGitIgnoreQuestion := &survey.Question{
Name: "addGitIgnore",
Prompt: &survey.Confirm{
Message: "Would you like to add a .gitignore?",
Default: false,
},
}
addGitIgnoreSurvey = append(addGitIgnoreSurvey, addGitIgnoreQuestion)
err := prompt.SurveyAsk(addGitIgnoreSurvey, &addGitIgnore)
if err != nil {
return "", err
}
var wantedIgnoreTemplate string
if addGitIgnore {
var gitIg []*survey.Question
gitIgnoretemplates, err := listGitIgnoreTemplates(client, hostname)
if err != nil {
return "", err
}
gitIgnoreQuestion := &survey.Question{
Name: "chooseGitIgnore",
Prompt: &survey.Select{
Message: "Choose a .gitignore template",
Options: gitIgnoretemplates,
},
}
gitIg = append(gitIg, gitIgnoreQuestion)
err = prompt.SurveyAsk(gitIg, &wantedIgnoreTemplate)
if err != nil {
return "", err
}
}
return wantedIgnoreTemplate, nil
}
func interactiveLicense(client *http.Client, hostname string) (string, error) {
var addLicense bool
var addLicenseSurvey []*survey.Question
var wantedLicense string
addLicenseQuestion := &survey.Question{
Name: "addLicense",
Prompt: &survey.Confirm{
Message: "Would you like to add a license?",
Default: false,
},
}
addLicenseSurvey = append(addLicenseSurvey, addLicenseQuestion)
err := prompt.SurveyAsk(addLicenseSurvey, &addLicense)
if err != nil {
return "", err
}
licenseKey := map[string]string{}
if addLicense {
licenseTemplates, err := listLicenseTemplates(client, hostname)
if err != nil {
return "", err
}
var licenseNames []string
for _, l := range licenseTemplates {
licenseNames = append(licenseNames, l.Name)
licenseKey[l.Name] = l.Key
}
var licenseQs []*survey.Question
licenseQuestion := &survey.Question{
Name: "chooseLicense",
Prompt: &survey.Select{
Message: "Choose a license",
Options: licenseNames,
},
}
licenseQs = append(licenseQs, licenseQuestion)
err = prompt.SurveyAsk(licenseQs, &wantedLicense)
if err != nil {
return "", err
}
return licenseKey[wantedLicense], nil
}
return "", nil
}
func localInit(io *iostreams.IOStreams, remoteURL, path, checkoutBranch string) error {
gitInit, err := git.GitCommand("init", path)
if err != nil {
return err
}
isTTY := io.IsStdoutTTY()
if isTTY {
gitInit.Stdout = io.Out
}
gitInit.Stderr = io.ErrOut
err = run.PrepareCmd(gitInit).Run()
if err != nil {
return err
}
gitRemoteAdd, err := git.GitCommand("-C", path, "remote", "add", "origin", remoteURL)
if err != nil {
return err
}
gitRemoteAdd.Stdout = io.Out
gitRemoteAdd.Stderr = io.ErrOut
err = run.PrepareCmd(gitRemoteAdd).Run()
if err != nil {
return err
}
if checkoutBranch == "" {
return nil
}
gitFetch, err := git.GitCommand("-C", path, "fetch", "origin", fmt.Sprintf("+refs/heads/%[1]s:refs/remotes/origin/%[1]s", checkoutBranch))
if err != nil {
return err
}
gitFetch.Stdout = io.Out
gitFetch.Stderr = io.ErrOut
err = run.PrepareCmd(gitFetch).Run()
if err != nil {
return err
}
gitCheckout, err := git.GitCommand("-C", path, "checkout", checkoutBranch)
if err != nil {
return err
}
gitCheckout.Stdout = io.Out
gitCheckout.Stderr = io.ErrOut
return run.PrepareCmd(gitCheckout).Run()
}
func interactiveRepoCreate(isDescEmpty bool, isVisibilityPassed bool, repoName string) (string, string, string, error) {
qs := []*survey.Question{}
repoNameQuestion := &survey.Question{
Name: "repoName",
Prompt: &survey.Input{
Message: "Repository name",
Default: repoName,
},
}
qs = append(qs, repoNameQuestion)
if isDescEmpty {
repoDescriptionQuestion := &survey.Question{
Name: "repoDescription",
Prompt: &survey.Input{
Message: "Repository description",
},
}
qs = append(qs, repoDescriptionQuestion)
}
if !isVisibilityPassed {
repoVisibilityQuestion := &survey.Question{
Name: "repoVisibility",
Prompt: &survey.Select{
Message: "Visibility",
Options: []string{"Public", "Private", "Internal"},
},
}
qs = append(qs, repoVisibilityQuestion)
}
answers := struct {
RepoName string
RepoDescription string
RepoVisibility string
}{}
err := prompt.SurveyAsk(qs, &answers)
if err != nil {
return "", "", "", err
}
return answers.RepoName, answers.RepoDescription, strings.ToUpper(answers.RepoVisibility), nil
}
func confirmSubmission(repoName string, repoOwner string, inLocalRepo bool) (bool, error) {
qs := []*survey.Question{}
promptString := ""
if inLocalRepo {
promptString = `This will add an "origin" git remote to your local repository. Continue?`
} else {
targetRepo := repoName
if repoOwner != "" {
targetRepo = fmt.Sprintf("%s/%s", repoOwner, repoName)
}
promptString = fmt.Sprintf(`This will create the "%s" repository on GitHub. Continue?`, targetRepo)
}
confirmSubmitQuestion := &survey.Question{
Name: "confirmSubmit",
Prompt: &survey.Confirm{
Message: promptString,
Default: true,
},
}
qs = append(qs, confirmSubmitQuestion)
answer := struct {
ConfirmSubmit bool
}{}
err := prompt.SurveyAsk(qs, &answer)
if err != nil {
return false, err
}
return answer.ConfirmSubmit, nil
}
func getVisibility() (string, error) {
qs := []*survey.Question{}
getVisibilityQuestion := &survey.Question{
Name: "repoVisibility",
Prompt: &survey.Select{
Message: "Visibility",
Options: []string{"Public", "Private", "Internal"},
},
}
qs = append(qs, getVisibilityQuestion)
answer := struct {
RepoVisibility string
}{}
err := prompt.SurveyAsk(qs, &answer)
if err != nil {
return "", err
}
return strings.ToUpper(answer.RepoVisibility), nil
}