mirror of
https://github.com/fiatjaf/nak.git
synced 2024-11-22 08:19:06 -05:00
add --silent global option to remove the stderr logs.
This commit is contained in:
parent
11fe6b5809
commit
8fbfdc65c8
10
event.go
10
event.go
|
@ -102,7 +102,7 @@ example:
|
||||||
if relayUrls := c.Args().Slice(); len(relayUrls) > 0 {
|
if relayUrls := c.Args().Slice(); len(relayUrls) > 0 {
|
||||||
_, relays = connectToAllRelays(c.Context, relayUrls)
|
_, relays = connectToAllRelays(c.Context, relayUrls)
|
||||||
if len(relays) == 0 {
|
if len(relays) == 0 {
|
||||||
fmt.Fprintf(os.Stderr, "failed to connect to any of the given relays.\n")
|
log("failed to connect to any of the given relays.\n")
|
||||||
os.Exit(3)
|
os.Exit(3)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -218,16 +218,16 @@ example:
|
||||||
fmt.Println(evt.String())
|
fmt.Println(evt.String())
|
||||||
os.Stdout.Sync()
|
os.Stdout.Sync()
|
||||||
for _, relay := range relays {
|
for _, relay := range relays {
|
||||||
fmt.Fprintf(os.Stderr, "publishing to %s... ", relay.URL)
|
log("publishing to %s... ", relay.URL)
|
||||||
if relay, err := nostr.RelayConnect(c.Context, relay.URL); err != nil {
|
if relay, err := nostr.RelayConnect(c.Context, relay.URL); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to connect: %s\n", err)
|
log("failed to connect: %s\n", err)
|
||||||
} else {
|
} else {
|
||||||
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
|
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if status, err := relay.Publish(ctx, evt); err != nil {
|
if status, err := relay.Publish(ctx, evt); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed: %s\n", err)
|
log("failed: %s\n", err)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(os.Stderr, "%s.\n", status)
|
log("%s.\n", status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
12
helpers.go
12
helpers.go
|
@ -17,6 +17,10 @@ const (
|
||||||
LINE_PROCESSING_ERROR = iota
|
LINE_PROCESSING_ERROR = iota
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var log = func(msg string, args ...any) {
|
||||||
|
fmt.Fprintf(os.Stderr, msg, args...)
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
|
@ -104,12 +108,12 @@ func connectToAllRelays(ctx context.Context, relayUrls []string) (*nostr.SimpleP
|
||||||
relays := make([]*nostr.Relay, 0, len(relayUrls))
|
relays := make([]*nostr.Relay, 0, len(relayUrls))
|
||||||
pool := nostr.NewSimplePool(ctx)
|
pool := nostr.NewSimplePool(ctx)
|
||||||
for _, url := range relayUrls {
|
for _, url := range relayUrls {
|
||||||
fmt.Fprintf(os.Stderr, "connecting to %s... ", url)
|
log("connecting to %s... ", url)
|
||||||
if relay, err := pool.EnsureRelay(url); err == nil {
|
if relay, err := pool.EnsureRelay(url); err == nil {
|
||||||
relays = append(relays, relay)
|
relays = append(relays, relay)
|
||||||
fmt.Fprintf(os.Stderr, "ok.\n")
|
log("ok.\n")
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(os.Stderr, err.Error()+"\n")
|
log(err.Error() + "\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return pool, relays
|
return pool, relays
|
||||||
|
@ -117,7 +121,7 @@ func connectToAllRelays(ctx context.Context, relayUrls []string) (*nostr.SimpleP
|
||||||
|
|
||||||
func lineProcessingError(c *cli.Context, msg string, args ...any) {
|
func lineProcessingError(c *cli.Context, msg string, args ...any) {
|
||||||
c.Context = context.WithValue(c.Context, LINE_PROCESSING_ERROR, true)
|
c.Context = context.WithValue(c.Context, LINE_PROCESSING_ERROR, true)
|
||||||
fmt.Fprintf(os.Stderr, msg+"\n", args...)
|
log(msg+"\n", args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func exitIfLineProcessingError(c *cli.Context) {
|
func exitIfLineProcessingError(c *cli.Context) {
|
||||||
|
|
13
main.go
13
main.go
|
@ -19,6 +19,19 @@ var app = &cli.App{
|
||||||
encode,
|
encode,
|
||||||
verify,
|
verify,
|
||||||
},
|
},
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "silent",
|
||||||
|
Usage: "do not print logs and info messages to stderr",
|
||||||
|
Aliases: []string{"s"},
|
||||||
|
Action: func(ctx *cli.Context, b bool) error {
|
||||||
|
if b {
|
||||||
|
log = func(msg string, args ...any) {}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
2
req.go
2
req.go
|
@ -102,7 +102,7 @@ example:
|
||||||
var relays []*nostr.Relay
|
var relays []*nostr.Relay
|
||||||
pool, relays = connectToAllRelays(c.Context, relayUrls)
|
pool, relays = connectToAllRelays(c.Context, relayUrls)
|
||||||
if len(relays) == 0 {
|
if len(relays) == 0 {
|
||||||
fmt.Fprintf(os.Stderr, "failed to connect to any of the given relays.\n")
|
log("failed to connect to any of the given relays.\n")
|
||||||
os.Exit(3)
|
os.Exit(3)
|
||||||
}
|
}
|
||||||
relayUrls = make([]string, len(relays))
|
relayUrls = make([]string, len(relays))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user