// 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) } var export *exporter.Exporter if filepath.Ext(outFile) == ".gz" { gzip := gzip.NewWriter(f) defer gzip.Close() export = exporter.NewExporter(gzip, qs) } else { export = exporter.NewExporter(f, qs) } //TODO: add possible support for exporting specific queries only switch typ { case "quad": export.ExportQuad() case "json": export.ExportJson() // gml/graphml experimental case "gml": export.ExportGml() case "graphml": export.ExportGraphml() default: return fmt.Errorf("unknown format %q", typ) } if export.Err() != nil { return export.Err() } if outFile != "-" { fmt.Printf("%d entries were written\n", export.Count()) } return nil }
// 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) } var w io.Writer = f if filepath.Ext(outFile) == ".gz" { gzip := gzip.NewWriter(f) defer gzip.Close() w = gzip } qr := graph.NewQuadReader(qs) //TODO: add possible support for exporting specific queries only if typ == "quad" { // compatibility typ = "nquads" } format := quad.FormatByName(typ) if format == nil { return fmt.Errorf("unknown format %q", typ) } else if format.Writer == nil { return fmt.Errorf("format %q: encoding is not supported", typ) } qw := format.Writer(w) count, err := quad.Copy(qw, qr) if err != nil { qw.Close() return err } if err = qw.Close(); err != nil { return err } if outFile != "-" { fmt.Printf("%d entries were written\n", count) } return nil }
func save(file, passphrase string, sites []Site) error { // If the file doesn't exist then create it if _, err := os.Stat(file); os.IsNotExist(err) { _, err = os.Create(file) if err != nil { return err } } // Marshal the JSON b, err := json.Marshal(sites) if err != nil { return err } b, err = encrypt(encodeBase64(b), passphrase) if err != nil { return err } // Compress the contents var buffer bytes.Buffer gzip := gzip.NewWriter(&buffer) if err != nil { return err } gzip.Write(b) gzip.Close() // Write to the file fi, err := os.OpenFile(file, os.O_WRONLY, 0666) if err != nil { return err } _, err = fi.Write(buffer.Bytes()) if err != nil { return err } fi.Close() return nil }
// DecodedData returns the Base64 decoded, gunzip'd decoded data per // http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/Subscriptions.html // You should expect to see a response with an array of Amazon Kinesis. // The Data attribute in the Amazon Kinesis record is Base64 encoded and compressed // with the gzip format. You can examine the raw data from the command line // using the following Unix commands: // echo -n "<Content of Data>" | base64 -d | zcat func (awsLogs *AWSLogs) DecodedData() (*CloudWatchLogEvent, error) { data, err := base64.StdEncoding.DecodeString(awsLogs.Data) if err != nil { return nil, err } in := bytes.NewReader(data) gzip, err := gzip.NewReader(in) if nil != err { return nil, err } defer gzip.Close() allData, err := ioutil.ReadAll(gzip) if err != nil { return nil, err } var cloudWatchEvent CloudWatchLogEvent err = json.Unmarshal(allData, &cloudWatchEvent) if nil != err { return nil, err } return &cloudWatchEvent, nil }
// Read the password book func read(file, passphrase string) ([]Site, error) { // If the file doesn't exist yet no worries if _, err := os.Stat(file); os.IsNotExist(err) { return []Site{}, nil } // Bring in the compressed data fi, err := os.Open(file) if err != nil { return nil, err } // Decompress the file contents gzip, err := gzip.NewReader(fi) if err != nil { return nil, err } decompressed, err := ioutil.ReadAll(gzip) gzip.Close() // Decrypt the contents decompressed, err = decrypt(decompressed, passphrase) if err != nil { return nil, err } decompressed = decodeBase64(decompressed) // Unmarshal the JSON information var sites []Site err = json.Unmarshal(decompressed, &sites) if err != nil { return nil, err } fi.Close() return sites, nil }