mirror of
https://github.com/FiloSottile/age.git
synced 2026-09-19 22:44:33 +00:00
internal/plugin,cmd/age: implement confirm protocol verb
This commit is contained in:
@@ -37,6 +37,7 @@ func parseRecipient(arg string) (age.Recipient, error) {
|
||||
}
|
||||
r.DisplayMessage = pluginDisplayMessage(r.Name())
|
||||
r.RequestValue = pluginRequestSecret(r.Name())
|
||||
r.Confirm = pluginConfirm(r.Name())
|
||||
return r, nil
|
||||
case strings.HasPrefix(arg, "age1"):
|
||||
return age.ParseX25519Recipient(arg)
|
||||
@@ -210,6 +211,7 @@ func parseIdentity(s string) (age.Identity, error) {
|
||||
}
|
||||
i.DisplayMessage = pluginDisplayMessage(i.Name())
|
||||
i.RequestValue = pluginRequestSecret(i.Name())
|
||||
i.Confirm = pluginConfirm(i.Name())
|
||||
return i, nil
|
||||
case strings.HasPrefix(s, "AGE-SECRET-KEY-1"):
|
||||
return age.ParseX25519Identity(s)
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/term"
|
||||
)
|
||||
@@ -111,3 +112,30 @@ func pluginRequestSecret(name string) func(string, bool) (string, error) {
|
||||
return string(secret), nil
|
||||
}
|
||||
}
|
||||
|
||||
func pluginConfirm(name string) func(msg, yes, no string) (bool, error) {
|
||||
return func(message, yes, no string) (bool, error) {
|
||||
if no != "" {
|
||||
message += fmt.Sprintf(" (1 for %q, 2 for %q)", yes, no)
|
||||
selection, err := readSecret(message)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("could not read value for age-plugin-%s: %v", name, err)
|
||||
}
|
||||
switch strings.TrimSpace(string(selection)) {
|
||||
case "1":
|
||||
return true, nil
|
||||
case "2":
|
||||
return false, nil
|
||||
default:
|
||||
return false, fmt.Errorf("invalid selection %q", selection)
|
||||
}
|
||||
} else {
|
||||
message += fmt.Sprintf(" (press enter for %q)", yes)
|
||||
_, err := readSecret(message)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("could not read value for age-plugin-%s: %v", name, err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user