nak event --confirm

This commit is contained in:
fiatjaf 2025-05-03 21:44:59 -03:00
parent f98bd7483f
commit 02f22a8c2f
2 changed files with 61 additions and 0 deletions

View File

@ -129,6 +129,11 @@ example:
Value: nostr.Now(),
Category: CATEGORY_EVENT_FIELDS,
},
&cli.BoolFlag{
Name: "confirm",
Usage: "ask before publishing the event",
Category: CATEGORY_EXTRAS,
},
),
ArgsUsage: "[relay...]",
Action: func(ctx context.Context, c *cli.Command) error {
@ -314,6 +319,17 @@ example:
if len(relays) > 0 {
os.Stdout.Sync()
if c.Bool("confirm") {
relaysStr := make([]string, len(relays))
for i, r := range relays {
relaysStr[i] = strings.ToLower(strings.Split(r.URL, "://")[1])
}
time.Sleep(time.Millisecond * 10)
if !askConfirmation("publish to [ " + strings.Join(relaysStr, " ") + " ]? ") {
return nil
}
}
if supportsDynamicMultilineMagic() {
// overcomplicated multiline rendering magic
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)

View File

@ -21,8 +21,10 @@ import (
"fiatjaf.com/nostr/nip19"
"fiatjaf.com/nostr/nip42"
"fiatjaf.com/nostr/sdk"
"github.com/chzyer/readline"
"github.com/fatih/color"
jsoniter "github.com/json-iterator/go"
"github.com/mattn/go-tty"
"github.com/urfave/cli/v3"
"golang.org/x/term"
)
@ -408,6 +410,49 @@ ex:
return list
}
func askConfirmation(msg string) bool {
if isPiped() {
tty, err := tty.Open()
if err != nil {
return false
}
defer tty.Close()
fmt.Fprintf(os.Stderr, color.YellowString(msg))
answer, err := tty.ReadString()
if err != nil {
return false
}
// print newline after password input
fmt.Fprintln(os.Stderr)
answer = strings.TrimSpace(string(answer))
return answer == "y" || answer == "yes"
} else {
config := &readline.Config{
Stdout: color.Error,
Prompt: color.YellowString(msg),
InterruptPrompt: "^C",
DisableAutoSaveHistory: true,
EnableMask: false,
MaskRune: '*',
}
rl, err := readline.NewEx(config)
if err != nil {
return false
}
answer, err := rl.Readline()
if err != nil {
return false
}
answer = strings.ToLower(strings.TrimSpace(answer))
return answer == "y" || answer == "yes"
}
}
var colors = struct {
reset func(...any) (int, error)
italic func(...any) string