Skip to content

Commit

Permalink
export from STDIN
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed Aug 20, 2021
1 parent 3a895e0 commit 892a013
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cmd/exportify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
wof_reader "github.com/whosonfirst/go-whosonfirst-reader"
wof_writer "github.com/whosonfirst/go-whosonfirst-writer"
"github.com/whosonfirst/go-writer"
"io"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -106,6 +107,19 @@ func main() {
log.Fatalf("Failed to create writer for '%s', %v", *writer_uri, err)
}

// please to be exposing reader.STDIN_SCHEME

if *reader_uri == "stdin://" {

err = exportStdin(ctx, r, wr, ex)

if err != nil {
log.Fatalf("Failed to export from STDIN, %v", err)
}

return
}

for _, id := range ids {

err := exportId(ctx, r, wr, ex, id)
Expand All @@ -116,6 +130,23 @@ func main() {
}
}

func exportStdin(ctx context.Context, r reader.Reader, wr writer.Writer, ex export.Exporter) error {

fh, err := r.Read(ctx, "-")

if err != nil {
return err
}

body, err := io.ReadAll(fh)

if err != nil {
return err
}

return exportBytes(ctx, body, wr, ex)
}

func exportId(ctx context.Context, r reader.Reader, wr writer.Writer, ex export.Exporter, id int64) error {

body, err := wof_reader.LoadBytesFromID(ctx, r, id)
Expand All @@ -124,6 +155,11 @@ func exportId(ctx context.Context, r reader.Reader, wr writer.Writer, ex export.
return err
}

return exportBytes(ctx, body, wr, ex)
}

func exportBytes(ctx context.Context, body []byte, wr writer.Writer, ex export.Exporter) error {

new_body, err := ex.Export(ctx, body)

if err != nil {
Expand Down

0 comments on commit 892a013

Please sign in to comment.