Skip to content

Commit

Permalink
feature: 增加对move和copy的支持 (#93)
Browse files Browse the repository at this point in the history
issue #89
  • Loading branch information
arrebole authored May 19, 2023
1 parent 26b7c21 commit 33b2b3a
Show file tree
Hide file tree
Showing 7 changed files with 411 additions and 0 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ docker run --rm upx upx -v
| [get](#get) | 下载一个文件或目录 |
| [put](#put) | 上传一个文件或目录 |
| [upload](#upload) | 上传多个文件或目录或 http(s) 文件, 支持 Glob 模式过滤上传文件|
| [mv](#mv) | 在同一 bucket 内移动文件|
| [cp](#cp) | 在同一 bucket 内复制文件 |
| [rm](#rm) | 删除目录或文件 |
| [sync](#sync) | 目录增量同步,类似 rsync |
| [auth](#auth) | 生成包含空间名操作员密码信息的 auth 字符串 |
Expand Down Expand Up @@ -442,6 +444,64 @@ upx rm -d /www
upx rm /aaa.png
```

## mv

> `bucket` 内部移动文件
| args | 说明 |
| --------- | ---- |
| source-file | 需要移动的源文件 |
| dest-file | 需要移动到的目标文件 |

| options | 说明 |
| --------- | ---- |
| -f | 允许覆盖目标文件 |

#### 语法
```bash
upx mv [options] <source-file> <dest-file>
```

#### 示例
移动文件
```bash
upx mv /aaa.mp4 /abc/aaa.mp4
```

移动文件,如果目标存在则强制覆盖
```bash
upx mv -f /aaa.mp4 /abc/aaa.mp4
```

## cp

> `bucket` 内部拷贝文件
| args | 说明 |
| --------- | ---- |
| source-file | 需要复制的源文件 |
| dest-file | 需要复制到的目标文件 |

| options | 说明 |
| --------- | ---- |
| -f | 允许覆盖目标文件 |

#### 语法
```bash
upx mv [options] <source-file> <dest-file>
```

#### 示例
移动文件
```bash
upx cp /aaa.mp4 /abc/aaa.mp4
```

复制文件,如果目标存在则强制覆盖
```bash
upx cp -f /aaa.mp4 /abc/aaa.mp4
```

## sync

> sync 本地路径 存储路径
Expand Down
42 changes: 42 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,45 @@ func NewUpgradeCommand() cli.Command {
},
}
}

func NewCopyCommand() cli.Command {
return cli.Command{
Name: "cp",
Usage: "copy files inside cloud storage",
ArgsUsage: "[remote-source-path] [remote-target-path]",
Before: CreateInitCheckFunc(LOGIN, CHECK),
Action: func(c *cli.Context) error {
if c.NArg() != 2 {
PrintErrorAndExit("invalid command args")
}
if err := session.Copy(c.Args()[0], c.Args()[1], c.Bool("f")); err != nil {
PrintErrorAndExit(err.Error())
}
return nil
},
Flags: []cli.Flag{
cli.BoolFlag{Name: "f", Usage: "Force overwrite existing files"},
},
}
}

func NewMoveCommand() cli.Command {
return cli.Command{
Name: "mv",
Usage: "move files inside cloud storage",
ArgsUsage: "[remote-source-path] [remote-target-path]",
Before: CreateInitCheckFunc(LOGIN, CHECK),
Action: func(c *cli.Context) error {
if c.NArg() != 2 {
PrintErrorAndExit("invalid command args")
}
if err := session.Move(c.Args()[0], c.Args()[1], c.Bool("f")); err != nil {
PrintErrorAndExit(err.Error())
}
return nil
},
Flags: []cli.Flag{
cli.BoolFlag{Name: "f", Usage: "Force overwrite existing files"},
},
}
}
106 changes: 106 additions & 0 deletions copy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package upx

import (
"fmt"
"io/ioutil"
"path"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestCopy(t *testing.T) {
SetUp()
defer TearDown()

upRootPath := path.Join(ROOT, "copy")
Upx("mkdir", upRootPath)

localRootPath, err := ioutil.TempDir("", "test")
assert.NoError(t, err)
localRootName := filepath.Base(localRootPath)

CreateFile(path.Join(localRootPath, "FILE1"))
CreateFile(path.Join(localRootPath, "FILE2"))

// 上传文件
_, err = Upx("put", localRootPath, upRootPath)
assert.NoError(t, err)

files, err := Ls(path.Join(upRootPath, localRootName))
assert.NoError(t, err)
assert.Len(t, files, 2)
assert.ElementsMatch(
t,
files,
[]string{"FILE1", "FILE2"},
)

time.Sleep(time.Second)

// 正常复制文件
_, err = Upx(
"cp",
path.Join(upRootPath, localRootName, "FILE1"),
path.Join(upRootPath, localRootName, "FILE3"),
)
assert.NoError(t, err)

files, err = Ls(path.Join(upRootPath, localRootName))
assert.NoError(t, err)
assert.Len(t, files, 3)
assert.ElementsMatch(
t,
files,
[]string{"FILE1", "FILE2", "FILE3"},
)

time.Sleep(time.Second)

// 目标文件已存在
_, err = Upx(
"cp",
path.Join(upRootPath, localRootName, "FILE1"),
path.Join(upRootPath, localRootName, "FILE2"),
)
assert.Error(t, err)
assert.Equal(
t,
err.Error(),
fmt.Sprintf(
"target path %s already exists use -f to force overwrite\n",
path.Join(upRootPath, localRootName, "FILE2"),
),
)

files, err = Ls(path.Join(upRootPath, localRootName))
assert.NoError(t, err)
assert.Len(t, files, 3)
assert.ElementsMatch(
t,
files,
[]string{"FILE1", "FILE2", "FILE3"},
)

time.Sleep(time.Second)

// 目标文件已存在, 强制覆盖
_, err = Upx(
"cp",
"-f",
path.Join(upRootPath, localRootName, "FILE1"),
path.Join(upRootPath, localRootName, "FILE2"),
)
assert.NoError(t, err)

files, err = Ls(path.Join(upRootPath, localRootName))
assert.NoError(t, err)
assert.Len(t, files, 3)
assert.ElementsMatch(
t,
files,
[]string{"FILE1", "FILE2", "FILE3"},
)
}
104 changes: 104 additions & 0 deletions move_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package upx

import (
"fmt"
"io/ioutil"
"path"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestMove(t *testing.T) {
SetUp()
defer TearDown()

upRootPath := path.Join(ROOT, "move")
Upx("mkdir", upRootPath)

localRootPath, err := ioutil.TempDir("", "test")
assert.NoError(t, err)
localRootName := filepath.Base(localRootPath)

CreateFile(path.Join(localRootPath, "FILE1"))
CreateFile(path.Join(localRootPath, "FILE2"))

// 上传文件
Upx("put", localRootPath, upRootPath)
files, err := Ls(path.Join(upRootPath, localRootName))

assert.NoError(t, err)
assert.Len(t, files, 2)
assert.ElementsMatch(
t,
files,
[]string{"FILE1", "FILE2"},
)

time.Sleep(time.Second)

// 正常移动文件
_, err = Upx(
"mv",
path.Join(upRootPath, localRootName, "FILE1"),
path.Join(upRootPath, localRootName, "FILE3"),
)
assert.NoError(t, err)

files, err = Ls(path.Join(upRootPath, localRootName))
assert.NoError(t, err)
assert.Len(t, files, 2)
assert.ElementsMatch(
t,
files,
[]string{"FILE2", "FILE3"},
)

time.Sleep(time.Second)

// 目标文件已存在
_, err = Upx(
"mv",
path.Join(upRootPath, localRootName, "FILE2"),
path.Join(upRootPath, localRootName, "FILE3"),
)
assert.Equal(
t,
err.Error(),
fmt.Sprintf(
"target path %s already exists use -f to force overwrite\n",
path.Join(upRootPath, localRootName, "FILE3"),
),
)

files, err = Ls(path.Join(upRootPath, localRootName))
assert.NoError(t, err)
assert.Len(t, files, 2)
assert.ElementsMatch(
t,
files,
[]string{"FILE2", "FILE3"},
)

time.Sleep(time.Second)

// 目标文件已存在, 强制覆盖
_, err = Upx(
"mv",
"-f",
path.Join(upRootPath, localRootName, "FILE2"),
path.Join(upRootPath, localRootName, "FILE3"),
)
assert.NoError(t, err)

files, err = Ls(path.Join(upRootPath, localRootName))
assert.NoError(t, err)
assert.Len(t, files, 1)
assert.ElementsMatch(
t,
files,
[]string{"FILE3"},
)
}
7 changes: 7 additions & 0 deletions putiginore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -54,6 +55,8 @@ func TestPutIgnore(t *testing.T) {
[]string{"FILE1", "FILE2"},
)

time.Sleep(time.Second)

// 上传隐藏的文件夹, 无all,上传失效
Upx(
"put",
Expand All @@ -70,6 +73,8 @@ func TestPutIgnore(t *testing.T) {
[]string{"FILE1", "FILE2"},
)

time.Sleep(time.Second)

// 上传隐藏的文件夹, 有all,上传成功
Upx(
"put",
Expand All @@ -86,6 +91,8 @@ func TestPutIgnore(t *testing.T) {
[]string{"FILE1", "FILE2", ".FILES"},
)

time.Sleep(time.Second)

// 上传所有文件
Upx("put", "-all", localRootPath, upRootPath)
files, err = Ls(path.Join(upRootPath, localRootName))
Expand Down
Loading

0 comments on commit 33b2b3a

Please sign in to comment.