Skip to content

Commit

Permalink
simplify getting user input functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
knbr13 committed Feb 1, 2024
1 parent 477e986 commit df8a955
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,18 @@ import (
"github.com/gookit/color"
)

type validator func(string) bool

func getUserInput(reader *bufio.Reader, prompt string, fn validator) string {
func getPathFromUser(reader *bufio.Reader) string {
for {
fmt.Print(prompt)
fmt.Print("enter the folder path to scan for Git repositories: ")
input, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintf(os.Stderr, "gitcs: error reading input: %s\n", err.Error())
os.Exit(1)
}
if fn(input) {
input = strings.TrimSpace(input)
if isValidFolderPath(input) {
return input
}
fmt.Println(color.Yellow.Sprint("invalid input. Please try again!"))
fmt.Println(color.Yellow.Sprintf("gitcs: path %q is not found, please enter a valid folder path", input))
}
}

func getPathFromUser(reader *bufio.Reader) string {
folder := getUserInput(reader, "Enter the folder path to scan for Git repositories: ", func(s string) bool {
return isValidFolderPath(strings.ToLower(strings.TrimSpace(s)))
})
folder = strings.ToLower(strings.TrimSpace(folder))

return folder
}

0 comments on commit df8a955

Please sign in to comment.