forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtitle_body_survey.go
More file actions
159 lines (137 loc) · 3.31 KB
/
title_body_survey.go
File metadata and controls
159 lines (137 loc) · 3.31 KB
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
package command
import (
"fmt"
"os"
"github.com/AlecAivazis/survey/v2"
"github.com/cli/cli/pkg/githubtemplate"
"github.com/cli/cli/pkg/surveyext"
"github.com/spf13/cobra"
)
type Action int
type titleBody struct {
Body string
Title string
Action Action
}
const (
PreviewAction Action = iota
SubmitAction
CancelAction
)
var SurveyAsk = func(qs []*survey.Question, response interface{}, opts ...survey.AskOpt) error {
return survey.Ask(qs, response, opts...)
}
func confirmSubmission() (Action, error) {
confirmAnswers := struct {
Confirmation int
}{}
confirmQs := []*survey.Question{
{
Name: "confirmation",
Prompt: &survey.Select{
Message: "What's next?",
Options: []string{
"Preview in browser",
"Submit",
"Cancel",
},
},
},
}
err := SurveyAsk(confirmQs, &confirmAnswers)
if err != nil {
return -1, fmt.Errorf("could not prompt: %w", err)
}
return Action(confirmAnswers.Confirmation), nil
}
func selectTemplate(templatePaths []string) (string, error) {
templateResponse := struct {
Index int
}{}
if len(templatePaths) > 1 {
templateNames := make([]string, 0, len(templatePaths))
for _, p := range templatePaths {
templateNames = append(templateNames, githubtemplate.ExtractName(p))
}
selectQs := []*survey.Question{
{
Name: "index",
Prompt: &survey.Select{
Message: "Choose a template",
Options: templateNames,
},
},
}
if err := SurveyAsk(selectQs, &templateResponse); err != nil {
return "", fmt.Errorf("could not prompt: %w", err)
}
}
templateContents := githubtemplate.ExtractContents(templatePaths[templateResponse.Index])
return string(templateContents), nil
}
func titleBodySurvey(cmd *cobra.Command, providedTitle, providedBody string, defs defaults, templatePaths []string) (*titleBody, error) {
editorCommand := os.Getenv("GH_EDITOR")
if editorCommand == "" {
ctx := contextForCommand(cmd)
cfg, err := ctx.Config()
if err != nil {
return nil, fmt.Errorf("could not read config: %w", err)
}
editorCommand, _ = cfg.Get(defaultHostname, "editor")
}
var inProgress titleBody
inProgress.Title = defs.Title
templateContents := ""
if providedBody == "" {
if len(templatePaths) > 0 {
var err error
templateContents, err = selectTemplate(templatePaths)
if err != nil {
return nil, err
}
inProgress.Body = templateContents
} else {
inProgress.Body = defs.Body
}
}
titleQuestion := &survey.Question{
Name: "title",
Prompt: &survey.Input{
Message: "Title",
Default: inProgress.Title,
},
}
bodyQuestion := &survey.Question{
Name: "body",
Prompt: &surveyext.GhEditor{
EditorCommand: editorCommand,
Editor: &survey.Editor{
Message: "Body",
FileName: "*.md",
Default: inProgress.Body,
HideDefault: true,
AppendDefault: true,
},
},
}
var qs []*survey.Question
if providedTitle == "" {
qs = append(qs, titleQuestion)
}
if providedBody == "" {
qs = append(qs, bodyQuestion)
}
err := SurveyAsk(qs, &inProgress)
if err != nil {
return nil, fmt.Errorf("could not prompt: %w", err)
}
if inProgress.Body == "" {
inProgress.Body = templateContents
}
confirmA, err := confirmSubmission()
if err != nil {
return nil, fmt.Errorf("unable to confirm: %w", err)
}
inProgress.Action = confirmA
return &inProgress, nil
}