Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AskPassword/AskPasswordOnce to Asker #1499

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/incus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func (c *cmdGlobal) PreRun(cmd *cobra.Command, args []string) error {

// Setup password helper
c.conf.PromptPassword = func(filename string) (string, error) {
return ask.AskPasswordOnce(fmt.Sprintf(i18n.G("Password for %s: "), filename)), nil
return c.asker.AskPasswordOnce(fmt.Sprintf(i18n.G("Password for %s: "), filename)), nil
}

// If the user is running a command that may attempt to connect to the local daemon
Expand Down
24 changes: 19 additions & 5 deletions shared/ask/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (a *Asker) AskChoice(question string, choices []string, defaultAnswer strin
}

// AskInt asks the user to enter an integer between a min and max value.
func (a *Asker) AskInt(question string, min int64, max int64, defaultAnswer string, validate func(int64) error) (int64, error) {
func (a *Asker) AskInt(question string, minValue int64, maxValue int64, defaultAnswer string, validate func(int64) error) (int64, error) {
for {
answer, err := a.askQuestion(question, defaultAnswer)
if err != nil {
Expand All @@ -69,7 +69,7 @@ func (a *Asker) AskInt(question string, min int64, max int64, defaultAnswer stri
continue
}

if !((min == -1 || result >= min) && (max == -1 || result <= max)) {
if !((minValue == -1 || result >= minValue) && (maxValue == -1 || result <= maxValue)) {
fmt.Fprintf(os.Stderr, "Invalid input: out of range\n\n")
continue
}
Expand All @@ -96,9 +96,9 @@ func (a *Asker) AskString(question string, defaultAnswer string, validate func(s
}

if validate != nil {
error := validate(answer)
if error != nil {
fmt.Fprintf(os.Stderr, "Invalid input: %s\n\n", error)
err := validate(answer)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid input: %s\n\n", err)
continue
}

Expand All @@ -114,6 +114,12 @@ func (a *Asker) AskString(question string, defaultAnswer string, validate func(s
}

// AskPassword asks the user to enter a password.
func (a *Asker) AskPassword(question string) string {
return AskPassword(question)
}

// AskPassword asks the user to enter a password.
// Deprecated: Use asker.AskPassword instead.
func AskPassword(question string) string {
for {
fmt.Print(question)
Expand Down Expand Up @@ -141,6 +147,14 @@ func AskPassword(question string) string {
// AskPasswordOnce asks the user to enter a password.
//
// It's the same as AskPassword, but it won't ask to enter it again.
func (a *Asker) AskPasswordOnce(question string) string {
return AskPasswordOnce(question)
}

// AskPasswordOnce asks the user to enter a password.
//
// It's the same as AskPassword, but it won't ask to enter it again.
// Deprecated: Use asker.AskPasswordOnce instead.
func AskPasswordOnce(question string) string {
for {
fmt.Print(question)
Expand Down
Loading