2021-03-31 16:56:34 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-08-30 10:51:31 +01:00
|
|
|
"context"
|
2021-05-14 20:20:21 +01:00
|
|
|
"fmt"
|
2021-08-13 20:33:27 +01:00
|
|
|
"io"
|
2021-03-31 16:56:34 +01:00
|
|
|
"strconv"
|
2021-08-13 20:33:27 +01:00
|
|
|
"sync"
|
2021-03-31 16:56:34 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/clingy"
|
2021-05-14 20:20:21 +01:00
|
|
|
"github.com/zeebo/errs"
|
2021-05-06 17:56:57 +01:00
|
|
|
|
2021-08-13 20:33:27 +01:00
|
|
|
"storj.io/common/sync2"
|
2022-01-06 19:55:46 +00:00
|
|
|
"storj.io/storj/cmd/uplink/ulext"
|
|
|
|
"storj.io/storj/cmd/uplink/ulfs"
|
|
|
|
"storj.io/storj/cmd/uplink/ulloc"
|
2021-03-31 16:56:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type cmdRm struct {
|
2021-05-26 21:19:29 +01:00
|
|
|
ex ulext.External
|
2021-03-31 16:56:34 +01:00
|
|
|
|
2021-08-13 20:33:27 +01:00
|
|
|
access string
|
|
|
|
recursive bool
|
|
|
|
parallelism int
|
|
|
|
encrypted bool
|
2021-10-02 00:47:53 +01:00
|
|
|
pending bool
|
2021-03-31 16:56:34 +01:00
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
location ulloc.Location
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
2021-05-26 21:19:29 +01:00
|
|
|
func newCmdRm(ex ulext.External) *cmdRm {
|
|
|
|
return &cmdRm{ex: ex}
|
|
|
|
}
|
2021-03-31 16:56:34 +01:00
|
|
|
|
2021-05-26 21:19:29 +01:00
|
|
|
func (c *cmdRm) Setup(params clingy.Parameters) {
|
2021-06-25 17:51:05 +01:00
|
|
|
c.access = params.Flag("access", "Access name or value to use", "").(string)
|
2021-05-25 00:11:50 +01:00
|
|
|
c.recursive = params.Flag("recursive", "Remove recursively", false,
|
2021-03-31 16:56:34 +01:00
|
|
|
clingy.Short('r'),
|
2021-12-09 19:21:52 +00:00
|
|
|
clingy.Transform(strconv.ParseBool), clingy.Boolean,
|
2021-03-31 16:56:34 +01:00
|
|
|
).(bool)
|
2022-01-04 14:50:14 +00:00
|
|
|
c.parallelism = params.Flag("parallelism", "Controls how many removes to perform in parallel", 1,
|
2021-08-13 20:33:27 +01:00
|
|
|
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)
|
2021-05-25 00:11:50 +01:00
|
|
|
c.encrypted = params.Flag("encrypted", "Interprets keys base64 encoded without decrypting", false,
|
2021-12-09 19:21:52 +00:00
|
|
|
clingy.Transform(strconv.ParseBool), clingy.Boolean,
|
2021-03-31 16:56:34 +01:00
|
|
|
).(bool)
|
2021-10-02 00:47:53 +01:00
|
|
|
c.pending = params.Flag("pending", "Remove pending object uploads instead", false,
|
2021-12-09 19:21:52 +00:00
|
|
|
clingy.Transform(strconv.ParseBool), clingy.Boolean,
|
2021-10-02 00:47:53 +01:00
|
|
|
).(bool)
|
2021-03-31 16:56:34 +01:00
|
|
|
|
2021-05-25 00:11:50 +01:00
|
|
|
c.location = params.Arg("location", "Location to remove (sj://BUCKET[/KEY])",
|
2021-05-06 17:56:57 +01:00
|
|
|
clingy.Transform(ulloc.Parse),
|
|
|
|
).(ulloc.Location)
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
func (c *cmdRm) Execute(ctx context.Context) error {
|
2022-05-11 20:17:13 +01:00
|
|
|
if c.location.Local() {
|
|
|
|
return errs.New("remove %v skipped: local delete", c.location)
|
|
|
|
}
|
|
|
|
|
2021-05-26 21:19:29 +01:00
|
|
|
fs, err := c.ex.OpenFilesystem(ctx, c.access, ulext.BypassEncryption(c.encrypted))
|
2021-04-06 20:19:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-05 22:53:08 +01:00
|
|
|
defer func() { _ = fs.Close() }()
|
2021-04-06 20:19:11 +01:00
|
|
|
|
2021-05-14 20:20:21 +01:00
|
|
|
if !c.recursive {
|
2021-10-02 00:47:53 +01:00
|
|
|
err := fs.Remove(ctx, c.location, &ulfs.RemoveOptions{
|
|
|
|
Pending: c.pending,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-05-14 20:20:21 +01:00
|
|
|
return err
|
|
|
|
}
|
2021-04-06 20:19:11 +01:00
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
fmt.Fprintln(clingy.Stdout(ctx), "removed", c.location)
|
2021-05-14 20:20:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-02 00:47:53 +01:00
|
|
|
iter, err := fs.List(ctx, c.location, &ulfs.ListOptions{
|
|
|
|
Recursive: true,
|
|
|
|
Pending: c.pending,
|
|
|
|
})
|
2021-05-14 20:20:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-13 20:33:27 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-05-14 20:20:21 +01:00
|
|
|
for iter.Next() {
|
|
|
|
loc := iter.Item().Loc
|
|
|
|
|
2021-08-13 20:33:27 +01:00
|
|
|
ok := limiter.Go(ctx, func() {
|
2021-10-02 00:47:53 +01:00
|
|
|
err := fs.Remove(ctx, loc, &ulfs.RemoveOptions{
|
|
|
|
Pending: c.pending,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-08-30 10:51:31 +01:00
|
|
|
fprintln(clingy.Stderr(ctx), "remove", loc, "failed:", err.Error())
|
2021-08-13 20:33:27 +01:00
|
|
|
addError(err)
|
|
|
|
} else {
|
2022-08-30 10:51:31 +01:00
|
|
|
fprintln(clingy.Stdout(ctx), "removed", loc)
|
2021-08-13 20:33:27 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
if !ok {
|
|
|
|
break
|
2021-05-14 20:20:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 20:33:27 +01:00
|
|
|
limiter.Wait()
|
|
|
|
|
2021-05-14 20:20:21 +01:00
|
|
|
if err := iter.Err(); err != nil {
|
|
|
|
return errs.Wrap(err)
|
2021-08-13 20:33:27 +01:00
|
|
|
} else if len(es) > 0 {
|
|
|
|
return es.Err()
|
2021-05-14 20:20:21 +01:00
|
|
|
}
|
2021-05-05 22:53:08 +01:00
|
|
|
return nil
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|