-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathclean.go
83 lines (67 loc) · 1.72 KB
/
clean.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
74
75
76
77
78
79
80
81
82
83
package clean
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/itchio/butler/comm"
"github.com/itchio/butler/mansion"
"github.com/pkg/errors"
)
var args = struct {
plan *string
}{}
func Register(ctx *mansion.Context) {
cmd := ctx.App.Command("clean", "Remove a bunch of files").Hidden()
args.plan = cmd.Arg("plan", "A .json plan containing a list of entries to remove").Required().String()
ctx.Register(cmd, do)
}
func do(ctx *mansion.Context) {
ctx.Must(Do(*args.plan))
}
func Do(planPath string) error {
startTime := time.Now()
contents, err := ioutil.ReadFile(planPath)
if err != nil {
return errors.WithStack(err)
}
plan := CleanPlan{}
err = json.Unmarshal(contents, &plan)
if err != nil {
return errors.WithStack(err)
}
comm.Logf("Cleaning %d entries from %s", len(plan.Entries), plan.BasePath)
for _, entry := range plan.Entries {
fullPath := filepath.Join(plan.BasePath, entry)
stat, err := os.Lstat(fullPath)
if err != nil {
if os.IsNotExist(err) {
// good, it's already gone!
continue
} else {
return errors.WithStack(err)
}
}
if stat.IsDir() {
// it's expected that we won't be able
// to remove all directories, ignore errors
os.Remove(fullPath)
} else {
// files on the other hand, we really do want to remove
err := os.Remove(fullPath)
if err != nil {
return errors.WithStack(err)
}
}
}
duration := time.Since(startTime)
entriesPerSec := float64(len(plan.Entries)) / duration.Seconds()
comm.Statf("Done in %s (%.2f entries/s)", duration, entriesPerSec)
return nil
}
// CleanPlan describes which files exactly to wipe
type CleanPlan struct {
BasePath string `json:"basePath"`
Entries []string `json:"entries"`
}