-
Notifications
You must be signed in to change notification settings - Fork 386
/
cmd_rm.go
146 lines (121 loc) · 3.02 KB
/
cmd_rm.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"fmt"
"io"
"strconv"
"sync"
"github.com/zeebo/clingy"
"github.com/zeebo/errs"
"storj.io/common/sync2"
"storj.io/storj/cmd/uplink/ulext"
"storj.io/storj/cmd/uplink/ulfs"
"storj.io/storj/cmd/uplink/ulloc"
)
type cmdRm struct {
ex ulext.External
access string
recursive bool
parallelism int
encrypted bool
pending bool
location ulloc.Location
}
func newCmdRm(ex ulext.External) *cmdRm {
return &cmdRm{ex: ex}
}
func (c *cmdRm) Setup(params clingy.Parameters) {
c.access = params.Flag("access", "Access name or value to use", "").(string)
c.recursive = params.Flag("recursive", "Remove recursively", false,
clingy.Short('r'),
clingy.Transform(strconv.ParseBool), clingy.Boolean,
).(bool)
c.parallelism = params.Flag("parallelism", "Controls how many removes to perform in parallel", 1,
clingy.Short('p'),
clingy.Transform(strconv.Atoi),
clingy.Transform(func(n int) (int, error) {
if n <= 0 {
return 0, errs.New("parallelism must be at least 1")
}
return n, nil
}),
).(int)
c.encrypted = params.Flag("encrypted", "Interprets keys base64 encoded without decrypting", false,
clingy.Transform(strconv.ParseBool), clingy.Boolean,
).(bool)
c.pending = params.Flag("pending", "Remove pending object uploads instead", false,
clingy.Transform(strconv.ParseBool), clingy.Boolean,
).(bool)
c.location = params.Arg("location", "Location to remove (sj://BUCKET[/KEY])",
clingy.Transform(ulloc.Parse),
).(ulloc.Location)
}
func (c *cmdRm) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
if c.location.Local() {
return errs.New("remove %v skipped: local delete", c.location)
}
fs, err := c.ex.OpenFilesystem(ctx, c.access, ulext.BypassEncryption(c.encrypted))
if err != nil {
return err
}
defer func() { _ = fs.Close() }()
if !c.recursive {
err := fs.Remove(ctx, c.location, &ulfs.RemoveOptions{
Pending: c.pending,
})
if err != nil {
return err
}
fmt.Fprintln(clingy.Stdout(ctx), "removed", c.location)
return nil
}
iter, err := fs.List(ctx, c.location, &ulfs.ListOptions{
Recursive: true,
Pending: c.pending,
})
if err != nil {
return err
}
var (
limiter = sync2.NewLimiter(c.parallelism)
es errs.Group
mu sync.Mutex
)
fprintln := func(w io.Writer, args ...interface{}) {
mu.Lock()
defer mu.Unlock()
fmt.Fprintln(w, args...)
}
addError := func(err error) {
mu.Lock()
defer mu.Unlock()
es.Add(err)
}
for iter.Next() {
loc := iter.Item().Loc
ok := limiter.Go(ctx, func() {
err := fs.Remove(ctx, loc, &ulfs.RemoveOptions{
Pending: c.pending,
})
if err != nil {
fprintln(clingy.Stderr(ctx), "remove", loc, "failed:", err.Error())
addError(err)
} else {
fprintln(clingy.Stdout(ctx), "removed", loc)
}
})
if !ok {
break
}
}
limiter.Wait()
if err := iter.Err(); err != nil {
return errs.Wrap(err)
} else if len(es) > 0 {
return es.Err()
}
return nil
}