2021-03-31 16:56:34 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/zeebo/clingy"
|
2021-04-06 20:19:11 +01:00
|
|
|
"github.com/zeebo/errs"
|
2021-03-31 16:56:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type cmdRm struct {
|
|
|
|
projectProvider
|
|
|
|
|
|
|
|
recursive bool
|
|
|
|
encrypted bool
|
|
|
|
|
2021-04-06 20:19:11 +01:00
|
|
|
location string
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmdRm) Setup(a clingy.Arguments, f clingy.Flags) {
|
|
|
|
c.projectProvider.Setup(a, f)
|
|
|
|
|
2021-04-06 20:19:11 +01:00
|
|
|
c.recursive = f.New("recursive", "Remove recursively", false,
|
2021-03-31 16:56:34 +01:00
|
|
|
clingy.Short('r'),
|
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
2021-04-06 20:19:11 +01:00
|
|
|
c.encrypted = f.New("encrypted", "Interprets keys base64 encoded without decrypting", false,
|
2021-03-31 16:56:34 +01:00
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
|
|
|
|
2021-04-06 20:19:11 +01:00
|
|
|
c.location = a.New("location", "Location to remove (sj://BUCKET[/KEY])").(string)
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmdRm) Execute(ctx clingy.Context) error {
|
2021-04-06 20:19:11 +01:00
|
|
|
project, err := c.OpenProject(ctx, bypassEncryption(c.encrypted))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() { _ = project.Close() }()
|
|
|
|
|
|
|
|
// TODO: use the filesystem interface
|
|
|
|
// TODO: recursive remove
|
|
|
|
|
|
|
|
p, err := parseLocation(c.location)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !p.remote {
|
|
|
|
return errs.New("can only delete remote objects")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = project.DeleteObject(ctx, p.bucket, p.key)
|
|
|
|
return err
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|