-q to silence stderr, -qq to silence everything.

This commit is contained in:
fiatjaf 2024-01-24 22:38:51 -03:00
parent 3f7089e27e
commit 14b69f36cf
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
9 changed files with 26 additions and 22 deletions

View File

@ -139,7 +139,7 @@ var count = &cli.Command{
var result string var result string
j, _ := json.Marshal([]any{"COUNT", "nak", filter}) j, _ := json.Marshal([]any{"COUNT", "nak", filter})
result = string(j) result = string(j)
fmt.Println(result) stdout(result)
} }
return nil return nil

View File

@ -3,7 +3,6 @@ package main
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt"
"strings" "strings"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
@ -68,7 +67,7 @@ var decode = &cli.Command{
continue continue
} }
fmt.Println(decodeResult.JSON()) stdout(decodeResult.JSON())
} }

View File

@ -36,7 +36,7 @@ var encode = &cli.Command{
} }
if npub, err := nip19.EncodePublicKey(target); err == nil { if npub, err := nip19.EncodePublicKey(target); err == nil {
fmt.Println(npub) stdout(npub)
} else { } else {
return err return err
} }
@ -57,7 +57,7 @@ var encode = &cli.Command{
} }
if npub, err := nip19.EncodePrivateKey(target); err == nil { if npub, err := nip19.EncodePrivateKey(target); err == nil {
fmt.Println(npub) stdout(npub)
} else { } else {
return err return err
} }
@ -90,7 +90,7 @@ var encode = &cli.Command{
} }
if npub, err := nip19.EncodeProfile(target, relays); err == nil { if npub, err := nip19.EncodeProfile(target, relays); err == nil {
fmt.Println(npub) stdout(npub)
} else { } else {
return err return err
} }
@ -134,7 +134,7 @@ var encode = &cli.Command{
} }
if npub, err := nip19.EncodeEvent(target, relays, author); err == nil { if npub, err := nip19.EncodeEvent(target, relays, author); err == nil {
fmt.Println(npub) stdout(npub)
} else { } else {
return err return err
} }
@ -198,7 +198,7 @@ var encode = &cli.Command{
} }
if npub, err := nip19.EncodeEntity(pubkey, kind, d, relays); err == nil { if npub, err := nip19.EncodeEntity(pubkey, kind, d, relays); err == nil {
fmt.Println(npub) stdout(npub)
} else { } else {
return err return err
} }
@ -219,7 +219,7 @@ var encode = &cli.Command{
} }
if note, err := nip19.EncodeNote(target); err == nil { if note, err := nip19.EncodeNote(target); err == nil {
fmt.Println(note) stdout(note)
} else { } else {
return err return err
} }

View File

@ -226,7 +226,7 @@ example:
j, _ := easyjson.Marshal(&evt) j, _ := easyjson.Marshal(&evt)
result = string(j) result = string(j)
} }
fmt.Println(result) stdout(result)
// publish to relays // publish to relays
if len(relays) > 0 { if len(relays) > 0 {

View File

@ -1,8 +1,6 @@
package main package main
import ( import (
"fmt"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19" "github.com/nbd-wtf/go-nostr/nip19"
sdk "github.com/nbd-wtf/nostr-sdk" 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}) { for ie := range pool.SubManyEose(c.Context, relays, nostr.Filters{filter}) {
fmt.Println(ie.Event) stdout(ie.Event)
} }
} }

View File

@ -25,6 +25,8 @@ var log = func(msg string, args ...any) {
fmt.Fprintf(os.Stderr, msg, args...) fmt.Fprintf(os.Stderr, msg, args...)
} }
var stdout = fmt.Println
func isPiped() bool { func isPiped() bool {
stat, _ := os.Stdin.Stat() stat, _ := os.Stdin.Stat()
return stat.Mode()&os.ModeCharDevice == 0 return stat.Mode()&os.ModeCharDevice == 0

17
main.go
View File

@ -1,12 +1,13 @@
package main package main
import ( import (
"fmt"
"os" "os"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
var q int
var app = &cli.App{ var app = &cli.App{
Name: "nak", Name: "nak",
Suggest: true, Suggest: true,
@ -25,12 +26,16 @@ var app = &cli.App{
}, },
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.BoolFlag{ &cli.BoolFlag{
Name: "silent", Name: "quiet",
Usage: "do not print logs and info messages to stderr", Usage: "do not print logs and info messages to stderr, use -qq to also not print anything to stdout",
Aliases: []string{"s"}, Count: &q,
Aliases: []string{"q"},
Action: func(ctx *cli.Context, b bool) error { Action: func(ctx *cli.Context, b bool) error {
if b { if q >= 1 {
log = func(msg string, args ...any) {} log = func(msg string, args ...any) {}
if q >= 2 {
stdout = func(a ...any) (int, error) { return 0, nil }
}
} }
return nil return nil
}, },
@ -40,7 +45,7 @@ var app = &cli.App{
func main() { func main() {
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
fmt.Println(err) stdout(err)
os.Exit(1) os.Exit(1)
} }
} }

View File

@ -31,7 +31,7 @@ var relay = &cli.Command{
} }
pretty, _ := json.MarshalIndent(info, "", " ") pretty, _ := json.MarshalIndent(info, "", " ")
fmt.Println(string(pretty)) stdout(string(pretty))
return nil return nil
}, },
} }

4
req.go
View File

@ -231,7 +231,7 @@ example:
fn = pool.SubMany fn = pool.SubMany
} }
for ie := range fn(c.Context, relayUrls, nostr.Filters{filter}) { for ie := range fn(c.Context, relayUrls, nostr.Filters{filter}) {
fmt.Println(ie.Event) stdout(ie.Event)
} }
} else { } else {
// no relays given, will just print the filter // no relays given, will just print the filter
@ -243,7 +243,7 @@ example:
result = string(j) result = string(j)
} }
fmt.Println(result) stdout(result)
} }
} }