-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_cmd.go
72 lines (66 loc) · 1.49 KB
/
export_cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2021 Wrangle Ltd
package main
import (
"encoding/csv"
"strings"
"github.com/spf13/cobra"
"github.com/wrgl/core/pkg/objects"
"github.com/wrgl/core/pkg/ref"
)
func newExportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export COMMIT",
Short: "Output commit content as CSV",
Args: cobra.ExactArgs(1),
Example: strings.Join([]string{
` # export latest commit to CSV file`,
` wrgl export my-branch > my_branch.csv`,
"",
` # export commit to CSV file`,
` wrgl export 1a2ed6248c7243cdaaecb98ac12213a7 > my_branch.csv`,
}, "\n"),
RunE: func(cmd *cobra.Command, args []string) error {
cStr := args[0]
return exportCommit(cmd, cStr)
},
}
return cmd
}
func exportCommit(cmd *cobra.Command, cStr string) error {
rd := getRepoDir(cmd)
quitIfRepoDirNotExist(cmd, rd)
db, err := rd.OpenObjectsStore()
if err != nil {
return err
}
defer db.Close()
rs := rd.OpenRefStore()
_, _, commit, err := ref.InterpretCommitName(db, rs, cStr, false)
if err != nil {
return err
}
tbl, err := objects.GetTable(db, commit.Table)
if err != nil {
return err
}
writer := csv.NewWriter(cmd.OutOrStdout())
err = writer.Write(tbl.Columns)
if err != nil {
return err
}
for _, sum := range tbl.Blocks {
blk, err := objects.GetBlock(db, sum)
if err != nil {
return err
}
for _, row := range blk {
err = writer.Write(row)
if err != nil {
return err
}
}
}
writer.Flush()
return writer.Error()
}