eventstore: cmd can read from files and doesn't break on long lines.
This commit is contained in:
@@ -104,6 +104,7 @@ func writeStdinLinesOrNothing(ch chan string) (hasStdinLines bool) {
|
||||
// piped
|
||||
go func() {
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
scanner.Buffer(make([]byte, 16*1024*1024), 256*1024*1024)
|
||||
for scanner.Scan() {
|
||||
ch <- strings.TrimSpace(scanner.Text())
|
||||
}
|
||||
@@ -115,3 +116,26 @@ func writeStdinLinesOrNothing(ch chan string) (hasStdinLines bool) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func getFileLines(filename string) chan string {
|
||||
ch := make(chan string)
|
||||
go func() {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to open file '%s': %s\n", filename, err)
|
||||
close(ch)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
scanner := bufio.NewScanner(f)
|
||||
scanner.Buffer(make([]byte, 16*1024*1024), 256*1024*1024)
|
||||
for scanner.Scan() {
|
||||
ch <- strings.TrimSpace(scanner.Text())
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error reading file '%s': %s\n", filename, err)
|
||||
}
|
||||
close(ch)
|
||||
}()
|
||||
return ch
|
||||
}
|
||||
|
||||
@@ -14,10 +14,23 @@ var save = &cli.Command{
|
||||
Name: "save",
|
||||
ArgsUsage: "[<event-json>]",
|
||||
Usage: "stores an event",
|
||||
Description: "takes either an event as an argument or reads a stream of events from stdin and inserts those in the currently opened eventstore.\ndoesn't perform any kind of signature checking or replacement.",
|
||||
Description: "takes either an event as an argument, reads a stream of events from stdin, or from a file specified with --file, and inserts those in the currently opened eventstore.\ndoesn't perform any kind of signature checking or replacement.",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "file",
|
||||
Aliases: []string{"f"},
|
||||
Usage: "file to read events from",
|
||||
},
|
||||
},
|
||||
Action: func(ctx context.Context, c *cli.Command) error {
|
||||
hasError := false
|
||||
for line := range getStdinLinesOrFirstArgument(c) {
|
||||
var lines chan string
|
||||
if file := c.String("file"); file != "" {
|
||||
lines = getFileLines(file)
|
||||
} else {
|
||||
lines = getStdinLinesOrFirstArgument(c)
|
||||
}
|
||||
for line := range lines {
|
||||
var event nostr.Event
|
||||
if err := easyjson.Unmarshal([]byte(line), &event); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "invalid event '%s': %s\n", line, err)
|
||||
|
||||
Reference in New Issue
Block a user