From 633c8c070b4e09eeb719600b4e1df51d3ec79772 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 20 Nov 2019 11:39:42 -0600 Subject: [PATCH] factor out title body prompting --- command/pr_create.go | 88 +++++------------------------- command/title_body_survey.go | 102 +++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 75 deletions(-) create mode 100644 command/title_body_survey.go diff --git a/command/pr_create.go b/command/pr_create.go index c8d86f53a..8f635e306 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -5,7 +5,6 @@ import ( "os" "runtime" - "github.com/AlecAivazis/survey/v2" "github.com/github/gh-cli/api" "github.com/github/gh-cli/context" "github.com/github/gh-cli/git" @@ -55,86 +54,25 @@ func prCreate(cmd *cobra.Command, _ []string) error { interactive := title == "" || body == "" - inProgress := struct { - Body string - Title string - }{} - if interactive { - confirmed := false - editor := determineEditor() + tb, err := titleBodySurvey(cmd, title, body) + if err != nil { + return errors.Wrap(err, "could not collect title and/or body") + } - for !confirmed { - titleQuestion := &survey.Question{ - Name: "title", - Prompt: &survey.Input{ - Message: "PR Title", - Default: inProgress.Title, - }, - } - bodyQuestion := &survey.Question{ - Name: "body", - Prompt: &survey.Editor{ - Message: fmt.Sprintf("PR Body (%s)", editor), - FileName: "*.md", - Default: inProgress.Body, - AppendDefault: true, - Editor: editor, - }, - } + if tb == nil { + // editing was canceled, we can just leave + return nil + } - qs := []*survey.Question{} - if title == "" { - qs = append(qs, titleQuestion) - } - if body == "" { - qs = append(qs, bodyQuestion) - } - - err := survey.Ask(qs, &inProgress) - if err != nil { - return errors.Wrap(err, "could not prompt") - } - confirmAnswers := struct { - Confirmation string - }{} - confirmQs := []*survey.Question{ - { - Name: "confirmation", - Prompt: &survey.Select{ - Message: "Submit?", - Options: []string{ - "Yes", - "Edit", - "Cancel", - }, - }, - }, - } - - err = survey.Ask(confirmQs, &confirmAnswers) - if err != nil { - return errors.Wrap(err, "could not prompt") - } - - switch confirmAnswers.Confirmation { - case "Yes": - confirmed = true - case "Edit": - continue - case "Cancel": - cmd.Println("Discarding pull request") - return nil - } + if title == "" { + title = tb.Title + } + if body == "" { + body = tb.Body } } - if title == "" { - title = inProgress.Title - } - if body == "" { - body = inProgress.Body - } base, err := cmd.Flags().GetString("base") if err != nil { return errors.Wrap(err, "could not parse base") diff --git a/command/title_body_survey.go b/command/title_body_survey.go new file mode 100644 index 000000000..c1d62ed4a --- /dev/null +++ b/command/title_body_survey.go @@ -0,0 +1,102 @@ +package command + +import ( + "fmt" + "github.com/AlecAivazis/survey/v2" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +type titleBody struct { + Body string + Title string +} + +const _confirmed = 0 +const _unconfirmed = 1 +const _cancel = 2 + +func confirm() (int, error) { + confirmAnswers := struct { + Confirmation int + }{} + confirmQs := []*survey.Question{ + { + Name: "confirmation", + Prompt: &survey.Select{ + Message: "Submit?", + Options: []string{ + "Yes", + "Edit", + "Cancel", + }, + }, + }, + } + + err := survey.Ask(confirmQs, &confirmAnswers) + if err != nil { + return -1, errors.Wrap(err, "could not prompt") + } + + return confirmAnswers.Confirmation, nil +} + +func titleBodySurvey(cmd *cobra.Command, providedTitle string, providedBody string) (*titleBody, error) { + inProgress := titleBody{} + + confirmed := false + editor := determineEditor() + + for !confirmed { + titleQuestion := &survey.Question{ + Name: "title", + Prompt: &survey.Input{ + Message: "Title", + Default: inProgress.Title, + }, + } + bodyQuestion := &survey.Question{ + Name: "body", + Prompt: &survey.Editor{ + Message: fmt.Sprintf("Body (%s)", editor), + FileName: "*.md", + Default: inProgress.Body, + AppendDefault: true, + Editor: editor, + }, + } + + qs := []*survey.Question{} + if providedTitle == "" { + qs = append(qs, titleQuestion) + } + if providedBody == "" { + qs = append(qs, bodyQuestion) + } + + err := survey.Ask(qs, &inProgress) + if err != nil { + return nil, errors.Wrap(err, "could not prompt") + } + + confirmA, err := confirm() + if err != nil { + return nil, errors.Wrap(err, "unable to confirm") + } + switch confirmA { + case _confirmed: + confirmed = true + case _unconfirmed: + continue + case _cancel: + cmd.Println("Discarding.") + return nil, nil + default: + panic("reached unreachable case") + } + + } + + return &inProgress, nil +}