-
Notifications
You must be signed in to change notification settings - Fork 3
/
snapshot.go
51 lines (40 loc) · 1.35 KB
/
snapshot.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 main
import (
"os"
"strings"
"time"
"github.com/falzm/fsdiff/internal/snapshot"
)
type snapshotCmd struct {
Root string `arg:"" type:"existingdir" default:"." help:"Path to root directory."`
CarryOn bool `help:"Continue on filesystem error."`
Exclude []string `placeholder:"PATTERN" help:"gitignore-compatible exclusion pattern (see https://git-scm.com/docs/gitignore)."`
ExcludeFrom string `type:"existingfile" help:"File path to read gitignore-compatible patterns from (see https://git-scm.com/docs/gitignore)."`
OutputFile string `short:"o" help:"File path to write snapshot to (default: <YYYYMMDDhhmmss>.snap)."`
Shallow bool `help:"Don't compute files checksum."`
}
func (c *snapshotCmd) Run() error {
opts := make([]snapshot.CreateOpt, 0)
if c.CarryOn {
opts = append(opts, snapshot.CreateOptCarryOn())
}
if c.ExcludeFrom != "" {
data, err := os.ReadFile(c.ExcludeFrom)
if err != nil {
return err
}
c.Exclude = append(c.Exclude, strings.Split(string(data), "\n")...)
}
opts = append(opts, snapshot.CreateOptExclude(c.Exclude))
if c.Shallow {
opts = append(opts, snapshot.CreateOptShallow())
}
if c.OutputFile == "" {
c.OutputFile = time.Now().Format("20060102150405.snap")
}
snap, err := snapshot.Create(c.OutputFile, c.Root, opts...)
if err != nil {
return err
}
return snap.Close()
}