diff --git a/README.md b/README.md index f256157..9e56314 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ can_manage = true After running `just build`, a number of scripts will be available: +- `./bin/export` prints JSONL events to stdout for a given virtual relay - `./bin/import` takes JSONL events on stdin and imports it into the given virtual relay ## Development diff --git a/cmd/export/main.go b/cmd/export/main.go new file mode 100644 index 0000000..53c6a00 --- /dev/null +++ b/cmd/export/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "log" + "os" + + "fiatjaf.com/nostr" + "zooid/zooid" +) + +func main() { + log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) + + var ( + relay = flag.String("relay", "", "Relay name (required)") + ) + flag.Parse() + + if *relay == "" { + fmt.Fprintln(os.Stderr, "Error: --relay is required") + flag.Usage() + os.Exit(1) + } + + // Load config for the specified relay + filename := fmt.Sprintf("%s.toml", *relay) + config, err := zooid.LoadConfig(filename) + if err != nil { + fmt.Fprintln(os.Stderr, "No such config file", filename) + os.Exit(1) + } + + // Create event store + events := &zooid.EventStore{ + Config: config, + Schema: &zooid.Schema{Name: config.Schema}, + } + + // Initialize the event store + if err := events.Init(); err != nil { + log.Fatalf("Failed to initialize event store: %v", err) + } + + // Query all events and output as JSONL + count := 0 + filter := nostr.Filter{} // Empty filter matches all events + + for event := range events.QueryEvents(filter, 0) { + jsonBytes, err := json.Marshal(event) + if err != nil { + log.Printf("Failed to marshal event %s: %v", event.ID.Hex(), err) + continue + } + fmt.Println(string(jsonBytes)) + count++ + } + + fmt.Fprintf(os.Stderr, "Exported %d events\n", count) +} diff --git a/justfile b/justfile index ae71f80..d4a00cc 100644 --- a/justfile +++ b/justfile @@ -7,7 +7,10 @@ build-relay: build-import: CGO_ENABLED=1 go build -o bin/import cmd/import/main.go -build: build-relay build-import +build-export: + CGO_ENABLED=1 go build -o bin/export cmd/export/main.go + +build: build-relay build-import build-export test: go test -v ./...