From 01c9f83e8e9c53988bc78261fbf530434c93bd94 Mon Sep 17 00:00:00 2001 From: Lucas Bremgartner Date: Thu, 12 Dec 2024 16:42:32 +0100 Subject: [PATCH 1/3] shared/ask: Add AskPassword/AskPasswordOnce to Asker Signed-off-by: Lucas Bremgartner --- shared/ask/ask.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/shared/ask/ask.go b/shared/ask/ask.go index 0b1c6c2cb11..b21ac88d380 100644 --- a/shared/ask/ask.go +++ b/shared/ask/ask.go @@ -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) @@ -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) From 648fc5ec571e810aa5c38e1240ab1349e63401fb Mon Sep 17 00:00:00 2001 From: Lucas Bremgartner Date: Thu, 12 Dec 2024 16:43:45 +0100 Subject: [PATCH 2/3] shared/ask: Fix redefinition of the built-in types Signed-off-by: Lucas Bremgartner --- shared/ask/ask.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/shared/ask/ask.go b/shared/ask/ask.go index b21ac88d380..0f43f1453e9 100644 --- a/shared/ask/ask.go +++ b/shared/ask/ask.go @@ -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 { @@ -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 } @@ -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 } From b779e461d20abc0e13c51a8335dea42ba7337675 Mon Sep 17 00:00:00 2001 From: Lucas Bremgartner Date: Thu, 12 Dec 2024 16:47:00 +0100 Subject: [PATCH 3/3] cmd/incus: Use AskPasswordOnce from asker Signed-off-by: Lucas Bremgartner --- cmd/incus/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/incus/main.go b/cmd/incus/main.go index bc435fe0672..d13c63b215e 100644 --- a/cmd/incus/main.go +++ b/cmd/incus/main.go @@ -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