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-12 17:27:39 +01:00
|
|
|
"fmt"
|
2021-03-31 16:56:34 +01:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/zeebo/clingy"
|
2021-05-12 17:27:39 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2022-01-06 19:55:46 +00:00
|
|
|
"storj.io/storj/cmd/uplink/ulext"
|
|
|
|
"storj.io/storj/cmd/uplink/ulloc"
|
2021-03-31 16:56:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type cmdRb 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
|
|
|
|
force bool
|
2021-03-31 16:56:34 +01:00
|
|
|
|
2021-05-12 17:27:39 +01:00
|
|
|
loc ulloc.Location
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
2021-05-26 21:19:29 +01:00
|
|
|
func newCmdRb(ex ulext.External) *cmdRb {
|
|
|
|
return &cmdRb{ex: ex}
|
|
|
|
}
|
2021-03-31 16:56:34 +01:00
|
|
|
|
2021-05-26 21:19:29 +01:00
|
|
|
func (c *cmdRb) 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.force = params.Flag("force", "Deletes any objects in bucket first", 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-05-25 00:11:50 +01:00
|
|
|
c.loc = params.Arg("name", "Bucket name (sj://BUCKET)",
|
2021-05-12 17:27:39 +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 *cmdRb) Execute(ctx context.Context) error {
|
2021-05-26 21:19:29 +01:00
|
|
|
project, err := c.ex.OpenProject(ctx, c.access)
|
2021-05-12 17:27:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() { _ = project.Close() }()
|
|
|
|
|
|
|
|
bucket, key, ok := c.loc.RemoteParts()
|
|
|
|
if !ok {
|
|
|
|
return errs.New("location must be remote")
|
|
|
|
}
|
|
|
|
if key != "" {
|
|
|
|
return errs.New("key must not be specified: %q", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.force {
|
|
|
|
_, err = project.DeleteBucketWithObjects(ctx, bucket)
|
|
|
|
} else {
|
|
|
|
_, err = project.DeleteBucket(ctx, bucket)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
fmt.Fprintf(clingy.Stdout(ctx), "Bucket %q has been deleted.\n", bucket)
|
2021-03-31 16:56:34 +01:00
|
|
|
return nil
|
|
|
|
}
|