2021-03-31 16:56:34 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
|
|
|
|
"storj.io/storj/cmd/uplinkng/ulloc"
|
2021-03-31 16:56:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type cmdRb struct {
|
|
|
|
projectProvider
|
|
|
|
|
|
|
|
force bool
|
|
|
|
|
2021-05-12 17:27:39 +01:00
|
|
|
loc ulloc.Location
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmdRb) Setup(a clingy.Arguments, f clingy.Flags) {
|
|
|
|
c.projectProvider.Setup(a, f)
|
|
|
|
|
|
|
|
c.force = f.New("force", "Deletes any objects in bucket first", false,
|
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
|
|
|
|
2021-05-12 17:27:39 +01:00
|
|
|
c.loc = a.New("name", "Bucket name (sj://BUCKET)",
|
|
|
|
clingy.Transform(ulloc.Parse),
|
|
|
|
).(ulloc.Location)
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmdRb) Execute(ctx clingy.Context) error {
|
2021-05-12 17:27:39 +01:00
|
|
|
project, err := c.OpenProject(ctx)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(ctx.Stdout(), "Bucket %q has been deleted.\n", bucket)
|
2021-03-31 16:56:34 +01:00
|
|
|
return nil
|
|
|
|
}
|