Skip to content

Commit

Permalink
Add optional default values to open-ended questions (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmahsax authored Jun 28, 2024
1 parent 907b9de commit ed526d4
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 28 deletions.
17 changes: 2 additions & 15 deletions cmd/codeRequest/codeRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,7 @@ func (cr *CodeRequest) createGitLab() {
}

func (cr *CodeRequest) baseBranch() string {
g := git.NewGit(cr.Debug, cr.Executor)
answer := commandline.AskYesNoQuestion("Is '" + g.DefaultBranch() + "' the correct base branch for your new code request?")

if answer {
return g.DefaultBranch()
} else {
return commandline.AskOpenEndedQuestion("Base branch", false)
}
return commandline.AskOpenEndedQuestion("Base branch", git.NewGit(cr.Debug, cr.Executor).DefaultBranch(), false)
}

func (cr *CodeRequest) draft() string {
Expand All @@ -121,13 +114,7 @@ func (cr *CodeRequest) newMrTitle() string {
}

func (cr *CodeRequest) newPrTitle() string {
answer := commandline.AskYesNoQuestion("Accept the autogenerated code request title '" + cr.autogeneratedTitle() + "'?")

if answer {
return cr.autogeneratedTitle()
} else {
return commandline.AskOpenEndedQuestion("Title", false)
}
return commandline.AskOpenEndedQuestion("Title", cr.autogeneratedTitle(), false)
}

func (cr *CodeRequest) autogeneratedTitle() string {
Expand Down
2 changes: 1 addition & 1 deletion cmd/newBranch/newBranch.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func determineBranch(args []string) string {
}

func askForBranch() string {
return commandline.AskOpenEndedQuestion("New branch name", false)
return commandline.AskOpenEndedQuestion("New branch name", "", false)
}

func (nb *NewBranch) execute() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/newBranch/newBranch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Test_determineBranch(t *testing.T) {
})

for _, test := range tests {
commandline.AskOpenEndedQuestion = func(question string, secret bool) string {
commandline.AskOpenEndedQuestion = func(question, defaultVal string, secret bool) string {
return test.branch
}

Expand Down Expand Up @@ -65,7 +65,7 @@ func Test_askForBranch(t *testing.T) {
})

for _, test := range tests {
commandline.AskOpenEndedQuestion = func(question string, secret bool) string {
commandline.AskOpenEndedQuestion = func(question, defaultVal string, secret bool) string {
return test.branch
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ func (s *Setup) generateConfigFileContents() string {
github := commandline.AskYesNoQuestion("Do you wish to set up GitHub credentials?")

if github {
contents = contents + "github_username: " + commandline.AskOpenEndedQuestion("GitHub username", false) + "\n"
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"
contents = contents + "github_username: " + commandline.AskOpenEndedQuestion("GitHub username", "", false) + "\n"
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"
}

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

if gitlab {
contents = contents + "gitlab_username: " + commandline.AskOpenEndedQuestion("GitLab username", false) + "\n"
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"
contents = contents + "gitlab_username: " + commandline.AskOpenEndedQuestion("GitLab username", "", false) + "\n"
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"
}

contents = strings.TrimSpace(contents) + "\n"
Expand Down
4 changes: 2 additions & 2 deletions cmd/setup/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ gitlab_token: hello_world
t.Cleanup(func() {
commandline.AskOpenEndedQuestion = originalAskOpenEndedQuestion
})
commandline.AskOpenEndedQuestion = func(question string, secret bool) string {
commandline.AskOpenEndedQuestion = func(question, defaultVal string, secret bool) string {
return "hello_world"
}

Expand Down Expand Up @@ -150,7 +150,7 @@ func Test_generateConfigFileContents(t *testing.T) {
t.Cleanup(func() {
commandline.AskOpenEndedQuestion = originalAskOpenEndedQuestion
})
commandline.AskOpenEndedQuestion = func(question string, secret bool) string {
commandline.AskOpenEndedQuestion = func(question, defaultVal string, secret bool) string {
return "hello_world"
}

Expand Down
15 changes: 12 additions & 3 deletions internal/commandline/commandline.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,31 @@ var AskMultipleChoice = func(question string, choices []string) string {
return selectedOption
}

var AskOpenEndedQuestion = func(question string, secret bool) string {
var AskOpenEndedQuestion = func(question, defaultVal string, secret bool) string {
var result string
for {
if secret {
result, _ = pterm.DefaultInteractiveTextInput.
WithMultiLine(false).
WithDefaultText(question).
WithMask("*").
WithMultiLine(false).
WithOnInterruptFunc(func() {
os.Exit(1)
}).
Show()
} else {
} else if defaultVal == "" {
result, _ = pterm.DefaultInteractiveTextInput.
WithDefaultText(question).
WithMultiLine(false).
WithOnInterruptFunc(func() {
os.Exit(1)
}).
Show()
} else {
result, _ = pterm.DefaultInteractiveTextInput.
WithDefaultText(question).
WithDefaultValue(defaultVal).
WithMultiLine(false).
WithOnInterruptFunc(func() {
os.Exit(1)
}).
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
var (
packageOwner = "emmahsax"
packageRepository = "go-git-helper"
packageVersion = "0.0.7"
packageVersion = "0.0.8"
)

func main() {
Expand Down

0 comments on commit ed526d4

Please sign in to comment.