diff --git a/count.go b/count.go index 339c04b..ba3f1ed 100644 --- a/count.go +++ b/count.go @@ -139,7 +139,7 @@ var count = &cli.Command{ var result string j, _ := json.Marshal([]any{"COUNT", "nak", filter}) result = string(j) - fmt.Println(result) + stdout(result) } return nil diff --git a/decode.go b/decode.go index d8b0fd9..371ddc2 100644 --- a/decode.go +++ b/decode.go @@ -3,7 +3,6 @@ package main import ( "encoding/hex" "encoding/json" - "fmt" "strings" "github.com/nbd-wtf/go-nostr" @@ -68,7 +67,7 @@ var decode = &cli.Command{ continue } - fmt.Println(decodeResult.JSON()) + stdout(decodeResult.JSON()) } diff --git a/encode.go b/encode.go index 320bef2..107c47d 100644 --- a/encode.go +++ b/encode.go @@ -36,7 +36,7 @@ var encode = &cli.Command{ } if npub, err := nip19.EncodePublicKey(target); err == nil { - fmt.Println(npub) + stdout(npub) } else { return err } @@ -57,7 +57,7 @@ var encode = &cli.Command{ } if npub, err := nip19.EncodePrivateKey(target); err == nil { - fmt.Println(npub) + stdout(npub) } else { return err } @@ -90,7 +90,7 @@ var encode = &cli.Command{ } if npub, err := nip19.EncodeProfile(target, relays); err == nil { - fmt.Println(npub) + stdout(npub) } else { return err } @@ -134,7 +134,7 @@ var encode = &cli.Command{ } if npub, err := nip19.EncodeEvent(target, relays, author); err == nil { - fmt.Println(npub) + stdout(npub) } else { return err } @@ -198,7 +198,7 @@ var encode = &cli.Command{ } if npub, err := nip19.EncodeEntity(pubkey, kind, d, relays); err == nil { - fmt.Println(npub) + stdout(npub) } else { return err } @@ -219,7 +219,7 @@ var encode = &cli.Command{ } if note, err := nip19.EncodeNote(target); err == nil { - fmt.Println(note) + stdout(note) } else { return err } diff --git a/event.go b/event.go index cdaca12..e75d837 100644 --- a/event.go +++ b/event.go @@ -226,7 +226,7 @@ example: j, _ := easyjson.Marshal(&evt) result = string(j) } - fmt.Println(result) + stdout(result) // publish to relays if len(relays) > 0 { diff --git a/fetch.go b/fetch.go index 33cf46c..7f3d966 100644 --- a/fetch.go +++ b/fetch.go @@ -1,8 +1,6 @@ package main import ( - "fmt" - "github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr/nip19" sdk "github.com/nbd-wtf/nostr-sdk" @@ -93,7 +91,7 @@ var fetch = &cli.Command{ } for ie := range pool.SubManyEose(c.Context, relays, nostr.Filters{filter}) { - fmt.Println(ie.Event) + stdout(ie.Event) } } diff --git a/helpers.go b/helpers.go index 204df0f..5cf95f7 100644 --- a/helpers.go +++ b/helpers.go @@ -25,6 +25,8 @@ var log = func(msg string, args ...any) { fmt.Fprintf(os.Stderr, msg, args...) } +var stdout = fmt.Println + func isPiped() bool { stat, _ := os.Stdin.Stat() return stat.Mode()&os.ModeCharDevice == 0 diff --git a/main.go b/main.go index c18b50e..c0175fa 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,13 @@ package main import ( - "fmt" "os" "github.com/urfave/cli/v2" ) +var q int + var app = &cli.App{ Name: "nak", Suggest: true, @@ -25,12 +26,16 @@ var app = &cli.App{ }, Flags: []cli.Flag{ &cli.BoolFlag{ - Name: "silent", - Usage: "do not print logs and info messages to stderr", - Aliases: []string{"s"}, + Name: "quiet", + Usage: "do not print logs and info messages to stderr, use -qq to also not print anything to stdout", + Count: &q, + Aliases: []string{"q"}, Action: func(ctx *cli.Context, b bool) error { - if b { + if q >= 1 { log = func(msg string, args ...any) {} + if q >= 2 { + stdout = func(a ...any) (int, error) { return 0, nil } + } } return nil }, @@ -40,7 +45,7 @@ var app = &cli.App{ func main() { if err := app.Run(os.Args); err != nil { - fmt.Println(err) + stdout(err) os.Exit(1) } } diff --git a/relay.go b/relay.go index a0faee4..a3f7551 100644 --- a/relay.go +++ b/relay.go @@ -31,7 +31,7 @@ var relay = &cli.Command{ } pretty, _ := json.MarshalIndent(info, "", " ") - fmt.Println(string(pretty)) + stdout(string(pretty)) return nil }, } diff --git a/req.go b/req.go index 878fa7d..d164d6a 100644 --- a/req.go +++ b/req.go @@ -231,7 +231,7 @@ example: fn = pool.SubMany } for ie := range fn(c.Context, relayUrls, nostr.Filters{filter}) { - fmt.Println(ie.Event) + stdout(ie.Event) } } else { // no relays given, will just print the filter @@ -243,7 +243,7 @@ example: result = string(j) } - fmt.Println(result) + stdout(result) } }