@@ -94,6 +94,10 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
9494 opts .Name = args [0 ]
9595 }
9696
97+ if len (args ) == 0 && (opts .GitIgnoreTemplate != "" || opts .LicenseTemplate != "" ) {
98+ return & cmdutil.FlagError {Err : errors .New (".gitignore and license templates are added only when a specific repository name is passed" )}
99+ }
100+
97101 if ! opts .IO .CanPrompt () {
98102 if opts .Name == "" {
99103 return & cmdutil.FlagError {Err : errors .New ("name argument required when not running interactively" )}
@@ -192,6 +196,17 @@ func createRun(opts *CreateOptions) error {
192196 if newVisibility != "" {
193197 visibility = newVisibility
194198 }
199+
200+ } else {
201+ // Go for a prompt only if visibility isn't passed
202+ if ! isVisibilityPassed {
203+ newVisibility , err := getVisibility ()
204+ if err != nil {
205+ return nil
206+ }
207+ visibility = newVisibility
208+ }
209+
195210 httpClient , err := opts .HttpClient ()
196211 if err != nil {
197212 return err
@@ -202,21 +217,20 @@ func createRun(opts *CreateOptions) error {
202217 return err
203218 }
204219
205- gt , lt , err := interactiveGitIgnoreLicense (api .NewClientFromHTTP (httpClient ), host )
206- if err != nil {
207- return err
220+ if gitIgnoreTemplate == "" {
221+ gt , err := interactiveGitIgnore (api .NewClientFromHTTP (httpClient ), host )
222+ if err != nil {
223+ return err
224+ }
225+ gitIgnoreTemplate = gt
208226 }
209227
210- gitIgnoreTemplate = gt
211- repoLicenseTemplate = lt
212- } else {
213- // Go for a prompt only if visibility isn't passed
214- if ! isVisibilityPassed {
215- newVisibility , err := getVisibility ()
228+ if repoLicenseTemplate == "" {
229+ lt , err := interactiveLicense (api .NewClientFromHTTP (httpClient ), host )
216230 if err != nil {
217- return nil
231+ return err
218232 }
219- visibility = newVisibility
233+ repoLicenseTemplate = lt
220234 }
221235 }
222236
@@ -359,109 +373,99 @@ func createRun(opts *CreateOptions) error {
359373 return nil
360374}
361375
362- func interactiveGitIgnoreLicense (client * api.Client , hostname string ) (string , string , error ) {
376+ func interactiveGitIgnore (client * api.Client , hostname string ) (string , error ) {
363377
364- var addBoth bool
365- var initialQs []* survey.Question
378+ var addGitIgnore bool
379+ var addGitIgnoreSurvey []* survey.Question
366380
367- addGitIgnoreLicense := & survey.Question {
368- Name : "addGitIgnoreLicense " ,
381+ addGitIgnoreQuestion := & survey.Question {
382+ Name : "addGitIgnore " ,
369383 Prompt : & survey.Confirm {
370- Message : "Would you like to add a .gitignore or a license ?" ,
384+ Message : "Would you like to add a .gitignore?" ,
371385 Default : false ,
372386 },
373387 }
374388
375- initialQs = append (initialQs , addGitIgnoreLicense )
376- err := prompt .SurveyAsk (initialQs , & addBoth )
389+ addGitIgnoreSurvey = append (addGitIgnoreSurvey , addGitIgnoreQuestion )
390+ err := prompt .SurveyAsk (addGitIgnoreSurvey , & addGitIgnore )
377391 if err != nil {
378- return "" , "" , err
392+ return "" , err
379393 }
380394
381- if addBoth {
382- var addQs []* survey.Question
395+ var wantedIgnoreTemplate string
396+
397+ if addGitIgnore {
398+ var gitIg []* survey.Question
383399
384- gitIgnoreLicenseQuestion := & survey.Question {
385- Name : "gitIgnoreLicense" ,
386- Prompt : & survey.MultiSelect {
387- Message : "What do you want to add?" ,
388- Options : []string {".gitignore" , "license" },
400+ gitIgnoretemplates , err := ListGitIgnoreTemplates (client , hostname )
401+ if err != nil {
402+ return "" , err
403+ }
404+ gitIgnoreQuestion := & survey.Question {
405+ Name : "chooseGitIgnore" ,
406+ Prompt : & survey.Select {
407+ Message : "Choose a .gitignore template" ,
408+ Options : gitIgnoretemplates ,
389409 },
390410 }
391- addQs = append (addQs , gitIgnoreLicenseQuestion )
392-
393- var answers []string
394- err = prompt .SurveyAsk (addQs , & answers )
411+ gitIg = append (gitIg , gitIgnoreQuestion )
412+ err = prompt .SurveyAsk (gitIg , & wantedIgnoreTemplate )
395413 if err != nil {
396- return "" , "" , err
414+ return "" , err
397415 }
416+ }
398417
399- wantGitIgnore , wantLicense := false , false
400-
401- if len (answers ) > 0 {
402- if len (answers ) > 1 {
403- wantGitIgnore , wantLicense = answers [0 ] == ".gitignore" , answers [1 ] == "license"
404- } else if answers [0 ] == ".gitignore" {
405- wantGitIgnore = true
406- } else if answers [0 ] == "license" {
407- wantLicense = true
408- }
418+ return wantedIgnoreTemplate , nil
419+ }
409420
410- qs := []* survey.Question {}
421+ func interactiveLicense (client * api.Client , hostname string ) (string , error ) {
422+ var addLicense bool
423+ var addLicenseSurvey []* survey.Question
424+ var wantedLicense string
411425
412- if wantGitIgnore {
413- gitIgnoretemplates , err := ListGitIgnoreTemplates (client , hostname )
414- if err != nil {
415- fmt .Println (err )
416- }
417- gitIgnoreQuestion := & survey.Question {
418- Name : "repoGitIgnore" ,
419- Prompt : & survey.Select {
420- Message : "Choose a .gitignore template" ,
421- Options : gitIgnoretemplates ,
422- },
423- }
424- qs = append (qs , gitIgnoreQuestion )
425- }
426-
427- licenseKey := map [string ]string {}
426+ addLicenseQuestion := & survey.Question {
427+ Name : "addLicense" ,
428+ Prompt : & survey.Confirm {
429+ Message : "Would you like to add a license?" ,
430+ Default : false ,
431+ },
432+ }
428433
429- if wantLicense {
430- licenseTemplates , err := ListLicenseTemplates (client , hostname )
431- if err != nil {
432- fmt .Println (err )
433- }
434- var licenseNames []string
435- for _ , l := range licenseTemplates {
436- licenseNames = append (licenseNames , l .Name )
437- licenseKey [l .Name ] = l .Key
438- }
439- licenseQuestion := & survey.Question {
440- Name : "repoLicense" ,
441- Prompt : & survey.Select {
442- Message : "Choose a license" ,
443- Options : licenseNames ,
444- },
445- }
446- qs = append (qs , licenseQuestion )
447- }
434+ addLicenseSurvey = append (addLicenseSurvey , addLicenseQuestion )
435+ err := prompt .SurveyAsk (addLicenseSurvey , & addLicense )
436+ if err != nil {
437+ return "" , err
438+ }
448439
449- templateAnswers := struct {
450- RepoGitIgnore string
451- RepoLicense string
452- }{}
440+ licenseKey := map [string ]string {}
453441
454- err = prompt .SurveyAsk (qs , & templateAnswers )
455- if err != nil {
456- return "" , "" , err
457- }
458- return templateAnswers .RepoGitIgnore , licenseKey [templateAnswers .RepoLicense ], nil
442+ if addLicense {
443+ licenseTemplates , err := ListLicenseTemplates (client , hostname )
444+ if err != nil {
445+ return "" , err
446+ }
447+ var licenseNames []string
448+ for _ , l := range licenseTemplates {
449+ licenseNames = append (licenseNames , l .Name )
450+ licenseKey [l .Name ] = l .Key
451+ }
452+ var licenseQs []* survey.Question
459453
454+ licenseQuestion := & survey.Question {
455+ Name : "chooseLicense" ,
456+ Prompt : & survey.Select {
457+ Message : "Choose a license" ,
458+ Options : licenseNames ,
459+ },
460460 }
461- return "" , "" , nil
461+ licenseQs = append (licenseQs , licenseQuestion )
462+ err = prompt .SurveyAsk (licenseQs , & wantedLicense )
463+ if err != nil {
464+ return "" , err
465+ }
466+ return licenseKey [wantedLicense ], nil
462467 }
463-
464- return "" , "" , nil
468+ return "" , nil
465469}
466470
467471func localInit (io * iostreams.IOStreams , remoteURL , path , checkoutBranch string ) error {
0 commit comments