-
Notifications
You must be signed in to change notification settings - Fork 3
/
dump.go
73 lines (57 loc) · 1.67 KB
/
dump.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
73
package main
import (
"fmt"
"github.com/alecthomas/kong"
"github.com/falzm/fsdiff/internal/snapshot"
)
type dumpCmdOutput struct {
filesByChecksum []*snapshot.FileInfo
filesByPath []*snapshot.FileInfo
metadata *snapshot.Metadata
}
type dumpCmd struct {
SnapshotFile string `arg:"" name:"snapshot" type:"existingfile" help:"Path to snapshot file."`
MetadataOnly bool `name:"metadata" help:"Only dump snapshot metadata."`
}
func (c *dumpCmd) run() (dumpCmdOutput, error) {
var out dumpCmdOutput
snap, err := snapshot.Open(c.SnapshotFile)
if err != nil {
return dumpCmdOutput{}, fmt.Errorf("unable to open snapshot file: %w", err)
}
defer snap.Close()
if out.filesByChecksum, err = snap.FilesByChecksum(); err != nil {
return dumpCmdOutput{}, err
}
if out.filesByPath, err = snap.FilesByPath(); err != nil {
return dumpCmdOutput{}, err
}
out.metadata = snap.Metadata()
return out, nil
}
func (c *dumpCmd) Run(ctx kong.Context) error {
out, err := c.run()
if err != nil {
return err
}
if !c.MetadataOnly {
_, _ = fmt.Fprintf(ctx.Stdout, "## by_path (%d)\n", len(out.filesByPath))
for _, fi := range out.filesByPath {
_, _ = fmt.Fprintf(ctx.Stdout, "%s %s\n", fi.Path, fi.String())
}
_, _ = fmt.Fprintf(ctx.Stdout, "## by_cs (%d)\n", len(out.filesByChecksum))
for _, fi := range out.filesByChecksum {
_, _ = fmt.Fprintf(ctx.Stdout, "%s %s\n", fi.Path, fi.String())
}
}
_, _ = fmt.Fprintf(
ctx.Stdout,
"## metadata\nformat version: %d\nfsdiff version: %s\ndate: %s\nroot: %s\nshallow: %t\n",
out.metadata.FormatVersion,
out.metadata.FsdiffVersion,
out.metadata.Date,
out.metadata.RootDir,
out.metadata.Shallow,
)
return nil
}