forked from erdos-one/r2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcp.go
54 lines (48 loc) · 1.44 KB
/
cp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package cmd
import (
"log"
"github.com/boatkit-io/r2/pkg"
"github.com/spf13/cobra"
)
// cpCmd represents the cp command
var cpCmd = &cobra.Command{
Use: "cp",
Short: "Copy an object from one R2 path to another",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
// Get profile client
profileName, err := cmd.Flags().GetString("profile")
if err != nil {
log.Fatal(err)
}
c := pkg.Client(getProfile(profileName))
// If a bucket name is provided, create the bucket
if len(args) == 2 {
sourcePath := args[0]
destinationPath := args[1]
if !pkg.IsR2URI(sourcePath) && pkg.IsR2URI(destinationPath) {
// Copy local file to R2
destURI := pkg.ParseR2URI(destinationPath)
b := c.Bucket(destURI.Bucket)
b.Upload(sourcePath, destURI.Path)
} else if pkg.IsR2URI(sourcePath) && !pkg.IsR2URI(destinationPath) {
// Copy R2 object to local file
sourceURI := pkg.ParseR2URI(sourcePath)
b := c.Bucket(sourceURI.Bucket)
b.Download(sourceURI.Path, destinationPath)
} else if pkg.IsR2URI(sourcePath) && pkg.IsR2URI(destinationPath) {
// Copy R2 object to R2 object
sourceURI := pkg.ParseR2URI(sourcePath)
destURI := pkg.ParseR2URI(destinationPath)
b := c.Bucket(sourceURI.Bucket)
b.Copy(sourceURI.Path, destURI)
}
} else {
log.Fatal("Please provide both a source and destination path.")
}
},
}
func init() {
// Add the cp command to the root command
rootCmd.AddCommand(cpCmd)
}