From 02f22a8c2faa5b4c685aabf25df78a805c5fce9f Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sat, 3 May 2025 21:44:59 -0300 Subject: [PATCH] nak event --confirm --- event.go | 16 ++++++++++++++++ helpers.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/event.go b/event.go index 905e313..be14ce7 100644 --- a/event.go +++ b/event.go @@ -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) diff --git a/helpers.go b/helpers.go index a154d81..903c879 100644 --- a/helpers.go +++ b/helpers.go @@ -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