top-level cli command that handles the output of nak.
This commit is contained in:
@@ -26,6 +26,7 @@ var app = &cli.App{
|
|||||||
Name: "store",
|
Name: "store",
|
||||||
Aliases: []string{"d"},
|
Aliases: []string{"d"},
|
||||||
Usage: "path to the database file or directory or database connection uri",
|
Usage: "path to the database file or directory or database connection uri",
|
||||||
|
Required: true,
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "type",
|
Name: "type",
|
||||||
@@ -85,10 +86,12 @@ var app = &cli.App{
|
|||||||
return db.Init()
|
return db.Init()
|
||||||
},
|
},
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
|
queryOrPut,
|
||||||
query,
|
query,
|
||||||
put,
|
put,
|
||||||
del,
|
del,
|
||||||
},
|
},
|
||||||
|
DefaultCommand: "query-or-put",
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/mailru/easyjson"
|
"github.com/mailru/easyjson"
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
@@ -22,6 +25,8 @@ var put = &cli.Command{
|
|||||||
lineProcessingError(c, "failed to save event '%s': %s", line, err)
|
lineProcessingError(c, "failed to save event '%s': %s", line, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(os.Stderr, "saved %s", event.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
exitIfLineProcessingError(c)
|
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