Skip to content

Commit

Permalink
Merge pull request #23 from kaypee90/handle-keyboard-interupt-error
Browse files Browse the repository at this point in the history
Handle emitted prompt errors
  • Loading branch information
kaypee90 authored Sep 2, 2024
2 parents a79e7c2 + d4b2f2d commit 85d4159
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 49 deletions.
30 changes: 23 additions & 7 deletions cmd/depbot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ func displayAppVersion() {
os.Exit(0)
}

func handlePromptError(err error) {
if err != nil {
os.Exit(0)
}
}

func launchApplicaton() {
// display introduction text
printIntroductoryText()
Expand All @@ -23,18 +29,27 @@ func launchApplicaton() {
// get dependabot configuration details
for {
if !existingConfigurationChecked && dependabotFileExists() {
if getConfigurationOverrideConfirmation() == NO {
if result, err := getConfigurationOverrideConfirmation(); err != nil || result == NO {
os.Exit(0)
}

existingConfigurationChecked = true
}

packageEcosystem := getPackageEcosystem()
directory := getDirectory()
interval := getInterval()
reviewer := getReviewer()
openPullRequestsLimit := getOpenPullRequestLimit()
packageEcosystem, err := getPackageEcosystem()
handlePromptError(err)

directory, err := getDirectory()
handlePromptError(err)

interval, err := getInterval()
handlePromptError(err)

reviewer, err := getReviewer()
handlePromptError(err)

openPullRequestsLimit, err := getOpenPullRequestLimit()
handlePromptError(err)

updates = append(updates, Update{
PackageEcosystem: packageEcosystem,
Expand All @@ -46,7 +61,8 @@ func launchApplicaton() {
OpenPullRequestsLimit: openPullRequestsLimit,
})

if addAdditionalPackageManager() == NO {
if result, err := addAdditionalPackageManager(); err != nil || result == NO {
handlePromptError(err)
break
}
}
Expand Down
58 changes: 16 additions & 42 deletions cmd/depbot/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@ import (
const YES = "yes"
const NO = "no"

func getConfigurationOverrideConfirmation() string {
func getConfigurationOverrideConfirmation() (string, error) {
prompt := promptui.Select{
Label: "Dependabot is currently configured for this project. Would you like to override the current configuration?",
Items: []string{YES, NO},
}

_, result, err := prompt.Run()

if err != nil {
panic(err)
}

return result
return result, err
}

func getPackageEcosystem() string {
func getPackageEcosystem() (string, error) {
prompt := promptui.Select{
Label: "Select Package Ecosystem",
Items: []string{"cargo", "docker", "gomod", "gradle", "maven",
Expand All @@ -35,89 +31,67 @@ func getPackageEcosystem() string {

_, result, err := prompt.Run()

if err != nil {
panic(err)
}

return result
return result, err
}

// TOD0: Add validataion
func getDirectory() string {
func getDirectory() (string, error) {
prompt := promptui.Prompt{
Label: "Provide Directory (/)",
Default: "/",
}

result, err := prompt.Run()

if err != nil {
panic(err)
}

return result
return result, err
}

func getInterval() string {
func getInterval() (string, error) {
prompt := promptui.Select{
Label: "Select Interval",
Items: []string{"daily", "weekly", "monthly"},
}

_, result, err := prompt.Run()

if err != nil {
panic(err)
}

return result
return result, err
}

// TOD0: Add validataion
func getReviewer() string {
func getReviewer() (string, error) {
prompt := promptui.Prompt{
Label: "Provide Reviewer (optional)",
}

result, err := prompt.Run()

if err != nil {
panic(err)
}

return result
return result, err
}

func getOpenPullRequestLimit() int {
func getOpenPullRequestLimit() (int, error) {
prompt := promptui.Prompt{
Label: "Provide Open Pull Request Limit (optional)",
}

result, err := prompt.Run()

if err != nil {
panic(err)
return 0, err
}

limit, err := strconv.Atoi(result)
if err != nil {
return 5
return 5, nil
}

return limit
return limit, nil
}

func addAdditionalPackageManager() string {
func addAdditionalPackageManager() (string, error) {
prompt := promptui.Select{
Label: "Do you want to add another package manager?",
Items: []string{YES, NO},
}

_, result, err := prompt.Run()

if err != nil {
panic(err)
}

return result
return result, err
}

0 comments on commit 85d4159

Please sign in to comment.