2021-03-31 16:56:34 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-05-14 20:20:21 +01:00
|
|
|
"fmt"
|
2021-03-31 16:56:34 +01:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"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-05-26 21:19:29 +01:00
|
|
|
"storj.io/storj/cmd/uplinkng/ulext"
|
2021-05-06 17:56:57 +01:00
|
|
|
"storj.io/storj/cmd/uplinkng/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-05-26 21:19:29 +01:00
|
|
|
access string
|
2021-03-31 16:56:34 +01:00
|
|
|
recursive bool
|
|
|
|
encrypted bool
|
|
|
|
|
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'),
|
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
2021-05-25 00:11:50 +01:00
|
|
|
c.encrypted = params.Flag("encrypted", "Interprets keys base64 encoded without decrypting", false,
|
2021-03-31 16:56:34 +01:00
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmdRm) Execute(ctx clingy.Context) error {
|
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 {
|
|
|
|
if err := fs.Remove(ctx, c.location); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-06 20:19:11 +01:00
|
|
|
|
2021-05-14 20:20:21 +01:00
|
|
|
fmt.Fprintln(ctx.Stdout(), "removed", c.location)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
iter, err := fs.ListObjects(ctx, c.location, c.recursive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
anyFailed := false
|
|
|
|
for iter.Next() {
|
|
|
|
loc := iter.Item().Loc
|
|
|
|
|
|
|
|
if err := fs.Remove(ctx, loc); err != nil {
|
|
|
|
fmt.Fprintln(ctx.Stderr(), "remove", loc, "failed:", err.Error())
|
|
|
|
anyFailed = true
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(ctx.Stdout(), "removed", loc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := iter.Err(); err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
} else if anyFailed {
|
|
|
|
return errs.New("some removals failed")
|
|
|
|
}
|
2021-05-05 22:53:08 +01:00
|
|
|
return nil
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|