From 00b13d2ac171d17f399788805830c27e964d0b8f Mon Sep 17 00:00:00 2001 From: David Schor Date: Mon, 10 Aug 2015 01:58:25 -0400 Subject: [PATCH] internal dump routine --- cmd/cayley/cayley.go | 6 +++++- internal/dump.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 internal/dump.go diff --git a/cmd/cayley/cayley.go b/cmd/cayley/cayley.go index 6106de4..4411d53 100644 --- a/cmd/cayley/cayley.go +++ b/cmd/cayley/cayley.go @@ -219,7 +219,11 @@ func main() { } } - // internal.Dump() + err = internal.Dump(handle.QuadStore, *dumpFile, *dumpType) + if err != nil { + break + } + handle.Close() case "repl": diff --git a/internal/dump.go b/internal/dump.go new file mode 100644 index 0000000..ae5f4ef --- /dev/null +++ b/internal/dump.go @@ -0,0 +1,36 @@ +package internal + +import ( + "fmt" + "os" + + "github.com/google/cayley/graph" + "github.com/google/cayley/exporter" +) + +// Dump the content of the database into a file based +// on a few different formats +func Dump(qs graph.QuadStore, outFile, typ string) error { + var f *os.File + if outFile == "-" { + f = os.Stdout + } else { + var err error + f, err = os.Create(outFile) + if err != nil { + return fmt.Errorf("could not open file %q: %v", outFile, err) + } + defer f.Close() + fmt.Printf("dumping db to file %q\n", outFile) + } + + export := exporter.NewExporter(f, qs) + if export.Err() != nil { + return export.Err() + } + + if outFile != "-" { + fmt.Printf("%d entries were written\n", export.Count()) + } + return nil +}