-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #262 from CircleCI-Public/prompt
Refactor common prompt code to package
- Loading branch information
Showing
4 changed files
with
69 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package prompt | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/manifoldco/promptui" | ||
) | ||
|
||
// ReadSecretStringFromUser can be used to read a value from the user by masking their input. | ||
// It's useful for token input in our case. | ||
func ReadSecretStringFromUser(message string) (string, error) { | ||
prompt := promptui.Prompt{ | ||
Label: message, | ||
Mask: '*', | ||
} | ||
|
||
secret, err := prompt.Run() | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return secret, nil | ||
} | ||
|
||
// ReadStringFromUser can be used to read any value from the user or the defaultValue when provided. | ||
func ReadStringFromUser(message string, defaultValue string) string { | ||
prompt := promptui.Prompt{ | ||
Label: message, | ||
} | ||
|
||
if defaultValue != "" { | ||
prompt.Default = defaultValue | ||
} | ||
|
||
token, err := prompt.Run() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return token | ||
} | ||
|
||
// AskUserToConfirm will prompt the user to confirm with the provided message. | ||
func AskUserToConfirm(message string) bool { | ||
prompt := promptui.Prompt{ | ||
Label: message, | ||
IsConfirm: true, | ||
} | ||
|
||
result, err := prompt.Run() | ||
return err == nil && strings.ToLower(result) == "y" | ||
} |