Skip to content

Commit

Permalink
fix: add storage rm tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Oct 20, 2023
1 parent 9b14b1f commit ccc4373
Show file tree
Hide file tree
Showing 2 changed files with 343 additions and 6 deletions.
20 changes: 14 additions & 6 deletions internal/storage/rm/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import (
"github.com/supabase/cli/internal/utils"
)

var (
errMissingObject = errors.New("Object not found")
errMissingBucket = errors.New("You must specify a bucket to delete.")
errMissingFlag = errors.New("You must specify -r flag to delete directories.")
)

type PrefixGroup struct {
Bucket string
Prefixes []string
Expand All @@ -30,10 +36,10 @@ func Run(ctx context.Context, paths []string, recursive bool, fsys afero.Fs) err
bucket, prefix := ls.SplitBucketPrefix(remotePath)
// Ignore attempts to delete all buckets
if len(bucket) == 0 {
return errors.New("You must specify a bucket to delete.")
return errMissingBucket
}
if cp.IsDir(prefix) && !recursive {
return errors.New("You must specify -r flag to delete directories.")
return errMissingFlag
}
groups[bucket] = append(groups[bucket], prefix)
}
Expand All @@ -44,18 +50,19 @@ func Run(ctx context.Context, paths []string, recursive bool, fsys afero.Fs) err
for bucket, prefixes := range groups {
if utils.SliceContains(prefixes, "") {
fmt.Fprintln(os.Stderr, "Deleting bucket:", bucket)
if err := RemoveStoragePathAll(ctx, projectRef, bucket, ""); err != nil {
if err := RemoveStoragePathAll(ctx, projectRef, bucket, ""); err != nil && !errors.Is(err, errMissingObject) {
return err
}
if data, err := client.DeleteStorageBucket(ctx, projectRef, bucket); err == nil {
fmt.Fprintln(os.Stderr, data.Message)
} else if !strings.Contains(err.Error(), `"error":"Bucket not found"`) {
return err
} else {
fmt.Fprintln(os.Stderr, "Bucket not found")
fmt.Fprintln(os.Stderr, "Bucket not found:", bucket)
}
continue
}
// Always try deleting first in case the paths resolve to extensionless files
fmt.Fprintln(os.Stderr, "Deleting objects:", prefixes)
removed, err := client.DeleteStorageObjects(ctx, projectRef, bucket, prefixes)
if err != nil {
Expand All @@ -64,7 +71,7 @@ func Run(ctx context.Context, paths []string, recursive bool, fsys afero.Fs) err
if !recursive {
if len(removed) == 0 {
utils.CmdSuggestion = "You must specify -r flag to delete directories."
return errors.New("Object not found")
return fmt.Errorf("%w: %s/%v", errMissingObject, bucket, prefixes)
}
continue
}
Expand All @@ -85,6 +92,7 @@ func Run(ctx context.Context, paths []string, recursive bool, fsys afero.Fs) err

// Expects prefix to be terminated by "/"
func RemoveStoragePathAll(ctx context.Context, projectRef, bucket, prefix string) error {
// We must remove one directory at a time to avoid breaking pagination result
queue := make([]string, 0)
queue = append(queue, prefix)
for len(queue) > 0 {
Expand All @@ -95,7 +103,7 @@ func RemoveStoragePathAll(ctx context.Context, projectRef, bucket, prefix string
return err
}
if len(paths) == 0 {
return errors.New("Object not found")
return fmt.Errorf("%w: %s/%s", errMissingObject, bucket, prefix)
}
var files []string
for _, objectName := range paths {
Expand Down
Loading

0 comments on commit ccc4373

Please sign in to comment.