support --jq flag on fetch and event commands.

This commit is contained in:
fiatjaf
2026-06-17 11:39:30 -03:00
parent 814059fcec
commit 483bf94ff4
2 changed files with 51 additions and 8 deletions
+28 -7
View File
@@ -90,6 +90,11 @@ example:
Usage: "print the event enveloped in a [\"EVENT\", ...] message ready to be sent to a relay",
Category: CATEGORY_EXTRAS,
},
&cli.StringFlag{
Name: "jq",
Usage: "filter returned events with jq expression",
Category: CATEGORY_EXTRAS,
},
&cli.BoolFlag{
Name: "nevent",
Usage: "print the nevent code (to stderr) after the event is published",
@@ -182,6 +187,11 @@ example:
return err
}
jq, err := jqPrepare(c.String("jq"))
if err != nil {
return err
}
// then process input and generate events:
// will reuse this
@@ -401,15 +411,26 @@ example:
}
// print event as json
var result string
if c.Bool("envelope") {
j, _ := json.Marshal(nostr.EventEnvelope{Event: evt})
result = string(j)
if jq == nil {
var result string
if c.Bool("envelope") {
j, _ := json.Marshal(nostr.EventEnvelope{Event: evt})
result = string(j)
} else {
j, _ := easyjson.Marshal(&evt)
result = string(j)
}
stdout(result)
} else {
j, _ := easyjson.Marshal(&evt)
result = string(j)
v, matches, err := jq(evt)
if err != nil {
return fmt.Errorf("jq filter failed: %w", err)
}
if matches {
out, _ := json.MarshalToString(v)
stdout(out)
}
}
stdout(result)
return publishFlow(ctx, c, kr, evt, relays)
}
+23 -1
View File
@@ -24,9 +24,18 @@ var fetch = &cli.Command{
Aliases: []string{"r"},
Usage: "also use these relays to fetch from",
},
&cli.StringFlag{
Name: "jq",
Usage: "filter returned events with jq expression",
},
),
ArgsUsage: "[nip05_or_nip19_code]",
Action: func(ctx context.Context, c *cli.Command) error {
jq, err := jqPrepare(c.String("jq"))
if err != nil {
return err
}
for code := range getStdinLinesOrArguments(c.Args()) {
filter := nostr.Filter{}
var authorHint nostr.PubKey
@@ -111,7 +120,20 @@ var fetch = &cli.Command{
Label: "nak-fetch",
}) {
found = true
stdout(ie.Event)
var out string
if jq == nil {
out = ie.Event.String()
} else {
v, matches, err := jq(ie.Event)
if err != nil {
return fmt.Errorf("jq filter failed: %w", err)
}
if !matches {
continue
}
out, _ = json.MarshalToString(v)
}
stdout(out)
}
if !found {