Skip to content

Commit

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

var (
errUnsupportedMove = errors.New("Moving between buckets is unsupported")
errMissingPath = errors.New("You must specify an object path")
)

func Run(ctx context.Context, src, dst string, recursive bool, fsys afero.Fs) error {
srcParsed, err := ls.ParseStorageURL(src)
if err != nil {
Expand All @@ -29,8 +34,11 @@ func Run(ctx context.Context, src, dst string, recursive bool, fsys afero.Fs) er
}
srcBucket, srcPrefix := ls.SplitBucketPrefix(srcParsed)
dstBucket, dstPrefix := ls.SplitBucketPrefix(dstParsed)
if len(srcPrefix) == 0 && len(dstPrefix) == 0 {
return errMissingPath
}
if srcBucket != dstBucket {
return errors.New("Moving between buckets is unsupported")
return errUnsupportedMove
}
fmt.Fprintln(os.Stderr, "Moving object:", srcParsed, "=>", dstParsed)
data, err := client.MoveStorageObject(ctx, projectRef, srcBucket, srcPrefix, dstPrefix)
Expand All @@ -45,6 +53,8 @@ func Run(ctx context.Context, src, dst string, recursive bool, fsys afero.Fs) er
// Expects srcPath to be terminated by "/"
func MoveStorageObjectAll(ctx context.Context, projectRef, srcPath, dstPath string) error {
_, dstPrefix := ls.SplitBucketPrefix(dstPath)
// Cannot iterate because pagination result may be updated during move
count := 0
queue := make([]string, 0)
queue = append(queue, srcPath)
for len(queue) > 0 {
Expand All @@ -60,6 +70,7 @@ func MoveStorageObjectAll(ctx context.Context, projectRef, srcPath, dstPath stri
queue = append(queue, objectPath)
continue
}
count++
relPath := strings.TrimPrefix(objectPath, srcPath)
srcBucket, srcPrefix := ls.SplitBucketPrefix(objectPath)
absPath := path.Join(dstPrefix, relPath)
Expand All @@ -69,5 +80,8 @@ func MoveStorageObjectAll(ctx context.Context, projectRef, srcPath, dstPath stri
}
}
}
if count == 0 {
return errors.New("Object not found: " + srcPath)
}
return nil
}
Loading

0 comments on commit 9b14b1f

Please sign in to comment.