github.com/attic-labs/noms@v0.0.0-20210827224422-e5fa29d95e8b/samples/go/csv/csv-export/exporter.go (about) 1 // Copyright 2016 Attic Labs, Inc. All rights reserved. 2 // Licensed under the Apache License, version 2.0: 3 // http://www.apache.org/licenses/LICENSE-2.0 4 5 package main 6 7 import ( 8 "fmt" 9 "os" 10 11 "github.com/attic-labs/kingpin" 12 13 "github.com/attic-labs/noms/go/config" 14 "github.com/attic-labs/noms/go/d" 15 "github.com/attic-labs/noms/go/types" 16 "github.com/attic-labs/noms/go/util/profile" 17 "github.com/attic-labs/noms/go/util/verbose" 18 "github.com/attic-labs/noms/samples/go/csv" 19 ) 20 21 func main() { 22 app := kingpin.New("exporter", "") 23 24 // Actually the delimiter uses runes, which can be multiple characters long. 25 // https://blog.golang.org/strings 26 delimiter := app.Flag("delimiter", "field delimiter for csv file, must be exactly one character long.").Default(",").String() 27 dataset := app.Arg("dataset", "dataset to export").Required().String() 28 29 verbose.RegisterVerboseFlags(app) 30 profile.RegisterProfileFlags(app) 31 32 kingpin.MustParse(app.Parse(os.Args[1:])) 33 34 cfg := config.NewResolver() 35 db, ds, err := cfg.GetDataset(*dataset) 36 d.CheckError(err) 37 38 defer db.Close() 39 40 comma, err := csv.StringToRune(*delimiter) 41 d.CheckError(err) 42 43 err = d.Try(func() { 44 defer profile.MaybeStartProfile().Stop() 45 46 hv := ds.HeadValue() 47 if l, ok := hv.(types.List); ok { 48 structDesc := csv.GetListElemDesc(l, db) 49 csv.WriteList(l, structDesc, comma, os.Stdout) 50 } else if m, ok := hv.(types.Map); ok { 51 structDesc := csv.GetMapElemDesc(m, db) 52 csv.WriteMap(m, structDesc, comma, os.Stdout) 53 } else { 54 panic(fmt.Sprintf("Expected ListKind or MapKind, found %s", hv.Kind())) 55 } 56 }) 57 if err != nil { 58 fmt.Println("Failed to export dataset as CSV:") 59 fmt.Println(err) 60 } 61 }