diff --git a/pkg/cmd/repo/create/create.go b/pkg/cmd/repo/create/create.go index 470e50467..c6d37285e 100644 --- a/pkg/cmd/repo/create/create.go +++ b/pkg/cmd/repo/create/create.go @@ -31,6 +31,8 @@ type CreateOptions struct { EnableIssues bool EnableWiki bool Public bool + Private bool + Internal bool ConfirmSubmit bool } @@ -80,7 +82,9 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co cmd.Flags().StringVarP(&opts.Team, "team", "t", "", "The name of the organization team to be granted access") 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 (default: private)") + 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, "Confirm the submission directly") return cmd @@ -92,7 +96,12 @@ func createRun(opts *CreateOptions) error { orgName := "" name := opts.Name + isNameAnArg := false + isDescEmpty := opts.Description == "" + isVisibilityPassed := false + if name != "" { + isNameAnArg = true if strings.Contains(name, "/") { newRepo, err := ghrepo.FromFullName(name) if err != nil { @@ -108,10 +117,52 @@ func createRun(opts *CreateOptions) error { name = path.Base(projectDir) } - visibility := "PRIVATE" + 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 + } + + // Trigger interactive prompt if name is not passed + if !isNameAnArg { + newName, newDesc, newVisibility, err := interactiveRepoCreate(isDescEmpty, isVisibilityPassed, name) + if err != nil { + return err + } + if newName != "" { + 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 + } + } input := repoCreateInput{ Name: name, @@ -206,6 +257,56 @@ func createRun(opts *CreateOptions) error { return nil } +func interactiveRepoCreate(isDescEmpty bool, isVisibilityPassed bool, repoName string) (string, string, string, error) { + qs := []*survey.Question{} + + repoOwnerQuestion := &survey.Question{ + Name: "repoOwner", + Prompt: &survey.Input{ + Message: "Repository name", + Default: repoName, + }, + } + qs = append(qs, repoOwnerQuestion) + + 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 { + RepoOwner string + RepoDescription string + RepoVisibility string + }{} + + err := prompt.SurveyAsk(qs, &answers) + + if err != nil { + return "", "", "", err + } + + return answers.RepoOwner, answers.RepoDescription, answers.RepoVisibility, nil + +} + func confirmSubmission(repoName string, repoOwner string, isConfirmFlagPassed *bool) (bool, error) { qs := []*survey.Question{} @@ -235,3 +336,27 @@ func confirmSubmission(repoName string, repoOwner string, isConfirmFlagPassed *b 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 answer.RepoVisibility, nil +} diff --git a/pkg/cmd/repo/create/create_test.go b/pkg/cmd/repo/create/create_test.go index dd7c5d2b5..25f5519ab 100644 --- a/pkg/cmd/repo/create/create_test.go +++ b/pkg/cmd/repo/create/create_test.go @@ -91,6 +91,12 @@ func TestRepoCreate(t *testing.T) { as, surveyTearDown := prompt.InitAskStubber() defer surveyTearDown() + as.Stub([]*prompt.QuestionStub{ + { + Name: "repoVisibility", + Value: "PRIVATE", + }, + }) as.Stub([]*prompt.QuestionStub{ { Name: "confirmSubmit", @@ -167,6 +173,12 @@ func TestRepoCreate_org(t *testing.T) { as, surveyTearDown := prompt.InitAskStubber() defer surveyTearDown() + as.Stub([]*prompt.QuestionStub{ + { + Name: "repoVisibility", + Value: "PRIVATE", + }, + }) as.Stub([]*prompt.QuestionStub{ { Name: "confirmSubmit", @@ -243,6 +255,12 @@ func TestRepoCreate_orgWithTeam(t *testing.T) { as, surveyTearDown := prompt.InitAskStubber() defer surveyTearDown() + as.Stub([]*prompt.QuestionStub{ + { + Name: "repoVisibility", + Value: "PRIVATE", + }, + }) as.Stub([]*prompt.QuestionStub{ { Name: "confirmSubmit",