mirror of
https://github.com/fiatjaf/nak.git
synced 2025-12-08 16:48:51 +00:00
Allow --prompt-sec to be used with pipes
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/nbd-wtf/go-nostr/nip19"
|
"github.com/nbd-wtf/go-nostr/nip19"
|
||||||
"github.com/nbd-wtf/go-nostr/nip46"
|
"github.com/nbd-wtf/go-nostr/nip46"
|
||||||
"github.com/nbd-wtf/go-nostr/nip49"
|
"github.com/nbd-wtf/go-nostr/nip49"
|
||||||
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
var defaultKeyFlags = []cli.Flag{
|
var defaultKeyFlags = []cli.Flag{
|
||||||
@@ -84,9 +85,6 @@ func gatherSecretKeyOrBunkerFromArguments(ctx context.Context, c *cli.Command) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if c.Bool("prompt-sec") {
|
if c.Bool("prompt-sec") {
|
||||||
if isPiped() {
|
|
||||||
return "", nil, fmt.Errorf("can't prompt for a secret key when processing data from a pipe, try again without --prompt-sec")
|
|
||||||
}
|
|
||||||
sec, err = askPassword("type your secret key as ncryptsec, nsec or hex: ", nil)
|
sec, err = askPassword("type your secret key as ncryptsec, nsec or hex: ", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil, fmt.Errorf("failed to get secret key: %w", err)
|
return "", nil, fmt.Errorf("failed to get secret key: %w", err)
|
||||||
@@ -133,29 +131,59 @@ func promptDecrypt(ncryptsec string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func askPassword(msg string, shouldAskAgain func(answer string) bool) (string, error) {
|
func askPassword(msg string, shouldAskAgain func(answer string) bool) (string, error) {
|
||||||
config := &readline.Config{
|
if isPiped() {
|
||||||
Stdout: color.Error,
|
// Use TTY method when stdin is piped
|
||||||
Prompt: color.YellowString(msg),
|
tty, err := os.Open("/dev/tty")
|
||||||
InterruptPrompt: "^C",
|
if err != nil {
|
||||||
DisableAutoSaveHistory: true,
|
return "", fmt.Errorf("can't prompt for a secret key when processing data from a pipe on this system (failed to open /dev/tty: %w), try again without --prompt-sec or provide the key via --sec or NOSTR_SECRET_KEY environment variable", err)
|
||||||
EnableMask: true,
|
}
|
||||||
MaskRune: '*',
|
defer tty.Close()
|
||||||
}
|
|
||||||
|
|
||||||
rl, err := readline.NewEx(config)
|
for {
|
||||||
if err != nil {
|
// Print the prompt to stderr so it's visible to the user
|
||||||
return "", err
|
fmt.Fprintf(color.Error, color.YellowString(msg))
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
// Read password from TTY with masking
|
||||||
answer, err := rl.Readline()
|
password, err := term.ReadPassword(int(tty.Fd()))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print newline after password input
|
||||||
|
fmt.Fprintln(color.Error)
|
||||||
|
|
||||||
|
answer := strings.TrimSpace(string(password))
|
||||||
|
if shouldAskAgain != nil && shouldAskAgain(answer) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return answer, nil
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Use normal readline method when stdin is not piped
|
||||||
|
config := &readline.Config{
|
||||||
|
Stdout: color.Error,
|
||||||
|
Prompt: color.YellowString(msg),
|
||||||
|
InterruptPrompt: "^C",
|
||||||
|
DisableAutoSaveHistory: true,
|
||||||
|
EnableMask: true,
|
||||||
|
MaskRune: '*',
|
||||||
|
}
|
||||||
|
|
||||||
|
rl, err := readline.NewEx(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
answer = strings.TrimSpace(answer)
|
|
||||||
if shouldAskAgain != nil && shouldAskAgain(answer) {
|
for {
|
||||||
continue
|
answer, err := rl.Readline()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
answer = strings.TrimSpace(answer)
|
||||||
|
if shouldAskAgain != nil && shouldAskAgain(answer) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return answer, err
|
||||||
}
|
}
|
||||||
return answer, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user