-
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.
- Loading branch information
1 parent
45afa93
commit 0f1ff57
Showing
3 changed files
with
71 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,38 @@ | ||
package prompt | ||
|
||
import "github.com/AlecAivazis/survey/v2" | ||
import ( | ||
"github.com/erikgeiser/promptkit/confirmation" | ||
"github.com/erikgeiser/promptkit/textinput" | ||
) | ||
|
||
// 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) { | ||
secret := "" | ||
prompt := &survey.Password{ | ||
Message: message, | ||
} | ||
err := survey.AskOne(prompt, &secret) | ||
input := textinput.New(message) | ||
input.Hidden = true | ||
secret, err := input.RunPrompt() | ||
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 { | ||
token := "" | ||
prompt := &survey.Input{ | ||
Message: message, | ||
} | ||
|
||
if defaultValue != "" { | ||
prompt.Default = defaultValue | ||
} | ||
|
||
err := survey.AskOne(prompt, &token) | ||
input := textinput.New(message) | ||
input.Placeholder = defaultValue | ||
input.InitialValue = defaultValue | ||
result, err := input.RunPrompt() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return token | ||
return result | ||
} | ||
|
||
// AskUserToConfirm will prompt the user to confirm with the provided message. | ||
func AskUserToConfirm(message string) bool { | ||
result := true | ||
prompt := &survey.Confirm{ | ||
Message: message, | ||
} | ||
|
||
err := survey.AskOne(prompt, &result) | ||
input := confirmation.New(message, confirmation.No) | ||
result, err := input.RunPrompt() | ||
return err == nil && result | ||
} |