top-level cli command that handles the output of nak.
This commit is contained in:
@@ -23,9 +23,10 @@ var app = &cli.App{
|
||||
UsageText: "eventstore -d ./data/sqlite <query|put|del> ...",
|
||||
Flags: []cli.Flag{
|
||||
&cli.PathFlag{
|
||||
Name: "store",
|
||||
Aliases: []string{"d"},
|
||||
Usage: "path to the database file or directory or database connection uri",
|
||||
Name: "store",
|
||||
Aliases: []string{"d"},
|
||||
Usage: "path to the database file or directory or database connection uri",
|
||||
Required: true,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "type",
|
||||
@@ -85,10 +86,12 @@ var app = &cli.App{
|
||||
return db.Init()
|
||||
},
|
||||
Commands: []*cli.Command{
|
||||
queryOrPut,
|
||||
query,
|
||||
put,
|
||||
del,
|
||||
},
|
||||
DefaultCommand: "query-or-put",
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/mailru/easyjson"
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"github.com/urfave/cli/v2"
|
||||
@@ -22,6 +25,8 @@ var put = &cli.Command{
|
||||
lineProcessingError(c, "failed to save event '%s': %s", line, err)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "saved %s", event.ID)
|
||||
}
|
||||
|
||||
exitIfLineProcessingError(c)
|
||||
|
||||
59
cmd/eventstore/query-or-put.go
Normal file
59
cmd/eventstore/query-or-put.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var queryOrPut = &cli.Command{
|
||||
Hidden: true,
|
||||
Name: "query-or-put",
|
||||
Action: func(c *cli.Context) error {
|
||||
line := getStdin()
|
||||
|
||||
ee := &nostr.EventEnvelope{}
|
||||
re := &nostr.ReqEnvelope{}
|
||||
e := &nostr.Event{}
|
||||
f := &nostr.Filter{}
|
||||
if json.Unmarshal([]byte(line), ee) == nil && ee.Event.ID != "" {
|
||||
e = &ee.Event
|
||||
return doPut(c, line, e)
|
||||
}
|
||||
if json.Unmarshal([]byte(line), e) == nil && e.ID != "" {
|
||||
return doPut(c, line, e)
|
||||
}
|
||||
if json.Unmarshal([]byte(line), re) == nil && len(re.Filters) > 0 {
|
||||
f = &re.Filters[0]
|
||||
return doQuery(c, f)
|
||||
}
|
||||
if json.Unmarshal([]byte(line), f) == nil && len(f.String()) > 2 {
|
||||
return doQuery(c, f)
|
||||
}
|
||||
|
||||
return fmt.Errorf("couldn't parse input '%s'", line)
|
||||
},
|
||||
}
|
||||
|
||||
func doPut(c *cli.Context, line string, e *nostr.Event) error {
|
||||
if err := db.SaveEvent(c.Context, e); err != nil {
|
||||
return fmt.Errorf("failed to save event '%s': %s", line, err)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "saved %s", e.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func doQuery(c *cli.Context, f *nostr.Filter) error {
|
||||
ch, err := db.QueryEvents(c.Context, *f)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error querying: %w", err)
|
||||
}
|
||||
|
||||
for evt := range ch {
|
||||
fmt.Println(evt)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user