Skip to content

Commit ed526d4

Browse files
authored
Add optional default values to open-ended questions (#51)
1 parent 907b9de commit ed526d4

File tree

7 files changed

+24
-28
lines changed

7 files changed

+24
-28
lines changed

cmd/codeRequest/codeRequest.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,7 @@ func (cr *CodeRequest) createGitLab() {
9696
}
9797

9898
func (cr *CodeRequest) baseBranch() string {
99-
g := git.NewGit(cr.Debug, cr.Executor)
100-
answer := commandline.AskYesNoQuestion("Is '" + g.DefaultBranch() + "' the correct base branch for your new code request?")
101-
102-
if answer {
103-
return g.DefaultBranch()
104-
} else {
105-
return commandline.AskOpenEndedQuestion("Base branch", false)
106-
}
99+
return commandline.AskOpenEndedQuestion("Base branch", git.NewGit(cr.Debug, cr.Executor).DefaultBranch(), false)
107100
}
108101

109102
func (cr *CodeRequest) draft() string {
@@ -121,13 +114,7 @@ func (cr *CodeRequest) newMrTitle() string {
121114
}
122115

123116
func (cr *CodeRequest) newPrTitle() string {
124-
answer := commandline.AskYesNoQuestion("Accept the autogenerated code request title '" + cr.autogeneratedTitle() + "'?")
125-
126-
if answer {
127-
return cr.autogeneratedTitle()
128-
} else {
129-
return commandline.AskOpenEndedQuestion("Title", false)
130-
}
117+
return commandline.AskOpenEndedQuestion("Title", cr.autogeneratedTitle(), false)
131118
}
132119

133120
func (cr *CodeRequest) autogeneratedTitle() string {

cmd/newBranch/newBranch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func determineBranch(args []string) string {
5353
}
5454

5555
func askForBranch() string {
56-
return commandline.AskOpenEndedQuestion("New branch name", false)
56+
return commandline.AskOpenEndedQuestion("New branch name", "", false)
5757
}
5858

5959
func (nb *NewBranch) execute() {

cmd/newBranch/newBranch_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func Test_determineBranch(t *testing.T) {
3434
})
3535

3636
for _, test := range tests {
37-
commandline.AskOpenEndedQuestion = func(question string, secret bool) string {
37+
commandline.AskOpenEndedQuestion = func(question, defaultVal string, secret bool) string {
3838
return test.branch
3939
}
4040

@@ -65,7 +65,7 @@ func Test_askForBranch(t *testing.T) {
6565
})
6666

6767
for _, test := range tests {
68-
commandline.AskOpenEndedQuestion = func(question string, secret bool) string {
68+
commandline.AskOpenEndedQuestion = func(question, defaultVal string, secret bool) string {
6969
return test.branch
7070
}
7171

cmd/setup/setup.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ func (s *Setup) generateConfigFileContents() string {
101101
github := commandline.AskYesNoQuestion("Do you wish to set up GitHub credentials?")
102102

103103
if github {
104-
contents = contents + "github_username: " + commandline.AskOpenEndedQuestion("GitHub username", false) + "\n"
105-
contents = contents + "github_token: " + commandline.AskOpenEndedQuestion("GitHub personal access token - navigate to https://github.com/settings/tokens to create a new personal access token", true) + "\n"
104+
contents = contents + "github_username: " + commandline.AskOpenEndedQuestion("GitHub username", "", false) + "\n"
105+
contents = contents + "github_token: " + commandline.AskOpenEndedQuestion("GitHub personal access token - navigate to https://github.com/settings/tokens to create a new personal access token", "", true) + "\n"
106106
}
107107

108108
gitlab := commandline.AskYesNoQuestion("Do you wish to set up GitLab credentials?")
109109

110110
if gitlab {
111-
contents = contents + "gitlab_username: " + commandline.AskOpenEndedQuestion("GitLab username", false) + "\n"
112-
contents = contents + "gitlab_token: " + commandline.AskOpenEndedQuestion("GitLab personal access token - navigate to https://gitlab.com/-/profile/personal_access_tokens to create a new personal access token", true) + "\n"
111+
contents = contents + "gitlab_username: " + commandline.AskOpenEndedQuestion("GitLab username", "", false) + "\n"
112+
contents = contents + "gitlab_token: " + commandline.AskOpenEndedQuestion("GitLab personal access token - navigate to https://gitlab.com/-/profile/personal_access_tokens to create a new personal access token", "", true) + "\n"
113113
}
114114

115115
contents = strings.TrimSpace(contents) + "\n"

cmd/setup/setup_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ gitlab_token: hello_world
9595
t.Cleanup(func() {
9696
commandline.AskOpenEndedQuestion = originalAskOpenEndedQuestion
9797
})
98-
commandline.AskOpenEndedQuestion = func(question string, secret bool) string {
98+
commandline.AskOpenEndedQuestion = func(question, defaultVal string, secret bool) string {
9999
return "hello_world"
100100
}
101101

@@ -150,7 +150,7 @@ func Test_generateConfigFileContents(t *testing.T) {
150150
t.Cleanup(func() {
151151
commandline.AskOpenEndedQuestion = originalAskOpenEndedQuestion
152152
})
153-
commandline.AskOpenEndedQuestion = func(question string, secret bool) string {
153+
commandline.AskOpenEndedQuestion = func(question, defaultVal string, secret bool) string {
154154
return "hello_world"
155155
}
156156

internal/commandline/commandline.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,31 @@ var AskMultipleChoice = func(question string, choices []string) string {
1919
return selectedOption
2020
}
2121

22-
var AskOpenEndedQuestion = func(question string, secret bool) string {
22+
var AskOpenEndedQuestion = func(question, defaultVal string, secret bool) string {
2323
var result string
2424
for {
2525
if secret {
2626
result, _ = pterm.DefaultInteractiveTextInput.
27-
WithMultiLine(false).
2827
WithDefaultText(question).
2928
WithMask("*").
29+
WithMultiLine(false).
3030
WithOnInterruptFunc(func() {
3131
os.Exit(1)
3232
}).
3333
Show()
34-
} else {
34+
} else if defaultVal == "" {
3535
result, _ = pterm.DefaultInteractiveTextInput.
36+
WithDefaultText(question).
3637
WithMultiLine(false).
38+
WithOnInterruptFunc(func() {
39+
os.Exit(1)
40+
}).
41+
Show()
42+
} else {
43+
result, _ = pterm.DefaultInteractiveTextInput.
3744
WithDefaultText(question).
45+
WithDefaultValue(defaultVal).
46+
WithMultiLine(false).
3847
WithOnInterruptFunc(func() {
3948
os.Exit(1)
4049
}).

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
var (
2323
packageOwner = "emmahsax"
2424
packageRepository = "go-git-helper"
25-
packageVersion = "0.0.7"
25+
packageVersion = "0.0.8"
2626
)
2727

2828
func main() {

0 commit comments

Comments
 (0)