-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
slurp.go
51 lines (46 loc) · 1.05 KB
/
slurp.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
package gotcmd
import (
"context"
"os"
"github.com/gotvc/got/pkg/gotfs"
"github.com/gotvc/got/pkg/gotrepo"
"github.com/gotvc/got/pkg/serde"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
func newSlurpCmd(open func() (*gotrepo.Repo, error)) *cobra.Command {
var repo *gotrepo.Repo
return &cobra.Command{
Use: "slurp",
Short: "imports a file or directory and returns a ref",
PreRunE: loadRepo(&repo, open),
PostRunE: closeRepo(repo),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.Errorf("must provide target to ingest")
}
p := args[0]
f, err := os.Open(p)
if err != nil {
return err
}
defer f.Close()
st, err := repo.GetImportStores(context.Background(), "")
if err != nil {
return err
}
fsop := gotfs.NewOperator()
root, err := fsop.CreateFileRoot(ctx, st.FS, st.Raw, f)
if err != nil {
return err
}
w := cmd.OutOrStdout()
data, err := serde.MarshalPEM(root)
if err != nil {
return err
}
w.Write(data)
return nil
},
}
}