mirror of
https://github.com/fiatjaf/nak.git
synced 2026-10-05 15:18:23 +00:00
support --jq flag on fetch and event commands.
This commit is contained in:
@@ -90,6 +90,11 @@ example:
|
|||||||
Usage: "print the event enveloped in a [\"EVENT\", ...] message ready to be sent to a relay",
|
Usage: "print the event enveloped in a [\"EVENT\", ...] message ready to be sent to a relay",
|
||||||
Category: CATEGORY_EXTRAS,
|
Category: CATEGORY_EXTRAS,
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "jq",
|
||||||
|
Usage: "filter returned events with jq expression",
|
||||||
|
Category: CATEGORY_EXTRAS,
|
||||||
|
},
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "nevent",
|
Name: "nevent",
|
||||||
Usage: "print the nevent code (to stderr) after the event is published",
|
Usage: "print the nevent code (to stderr) after the event is published",
|
||||||
@@ -182,6 +187,11 @@ example:
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
jq, err := jqPrepare(c.String("jq"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// then process input and generate events:
|
// then process input and generate events:
|
||||||
|
|
||||||
// will reuse this
|
// will reuse this
|
||||||
@@ -401,6 +411,7 @@ example:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// print event as json
|
// print event as json
|
||||||
|
if jq == nil {
|
||||||
var result string
|
var result string
|
||||||
if c.Bool("envelope") {
|
if c.Bool("envelope") {
|
||||||
j, _ := json.Marshal(nostr.EventEnvelope{Event: evt})
|
j, _ := json.Marshal(nostr.EventEnvelope{Event: evt})
|
||||||
@@ -410,6 +421,16 @@ example:
|
|||||||
result = string(j)
|
result = string(j)
|
||||||
}
|
}
|
||||||
stdout(result)
|
stdout(result)
|
||||||
|
} else {
|
||||||
|
v, matches, err := jq(evt)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("jq filter failed: %w", err)
|
||||||
|
}
|
||||||
|
if matches {
|
||||||
|
out, _ := json.MarshalToString(v)
|
||||||
|
stdout(out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return publishFlow(ctx, c, kr, evt, relays)
|
return publishFlow(ctx, c, kr, evt, relays)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,9 +24,18 @@ var fetch = &cli.Command{
|
|||||||
Aliases: []string{"r"},
|
Aliases: []string{"r"},
|
||||||
Usage: "also use these relays to fetch from",
|
Usage: "also use these relays to fetch from",
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "jq",
|
||||||
|
Usage: "filter returned events with jq expression",
|
||||||
|
},
|
||||||
),
|
),
|
||||||
ArgsUsage: "[nip05_or_nip19_code]",
|
ArgsUsage: "[nip05_or_nip19_code]",
|
||||||
Action: func(ctx context.Context, c *cli.Command) error {
|
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()) {
|
for code := range getStdinLinesOrArguments(c.Args()) {
|
||||||
filter := nostr.Filter{}
|
filter := nostr.Filter{}
|
||||||
var authorHint nostr.PubKey
|
var authorHint nostr.PubKey
|
||||||
@@ -111,7 +120,20 @@ var fetch = &cli.Command{
|
|||||||
Label: "nak-fetch",
|
Label: "nak-fetch",
|
||||||
}) {
|
}) {
|
||||||
found = true
|
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 {
|
if !found {
|
||||||
|
|||||||
Reference in New Issue
Block a user