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

plugin: add request-public public prompt #519

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 27 additions & 2 deletions cmd/age/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ func readSecret(prompt string) (s []byte, err error) {
return
}

// readPublic reads a value from the terminal. The prompt is ephemeral.
func readPublic(prompt string) (s []byte, err error) {
err = withTerminal(func(in, out *os.File) error {
fmt.Fprintf(out, "%s ", prompt)
defer clearLine(out)
oldState, err := term.MakeRaw(int(in.Fd()))
if err != nil {
return err
}
defer term.Restore(int(in.Fd()), oldState)

t := term.NewTerminal(in, "")
line, err := t.ReadLine()
s = []byte(line)
return err
})
return
}

// readCharacter reads a single character from the terminal with no echo. The
// prompt is ephemeral.
func readCharacter(prompt string) (c byte, err error) {
Expand Down Expand Up @@ -159,13 +178,19 @@ var pluginTerminalUI = &plugin.ClientUI{
printf("%s plugin: %s", name, message)
return nil
},
RequestValue: func(name, message string, _ bool) (s string, err error) {
RequestValue: func(name, message string, isSecret bool) (s string, err error) {
defer func() {
if err != nil {
warningf("could not read value for age-plugin-%s: %v", name, err)
}
}()
secret, err := readSecret(message)
var reader func(string) ([]byte, error)
if isSecret {
reader = readSecret
} else {
reader = readPublic
}
secret, err := reader(message)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a better name for secret might be value now?

if err != nil {
return "", err
}
Expand Down