Skip to content

Commit

Permalink
Support moving a file into a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
GGP1 authored and GGP1 committed Jan 22, 2023
1 parent 31c6ea2 commit 7b2bde5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
18 changes: 14 additions & 4 deletions commands/file/mv/mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const example = `
kure file mv oldFile newFile
* Move a directory
kure file mv oldDir/ newDir/`
kure file mv oldDir/ newDir/
* Move a file into a directory
kure file mv oldDir/test.txt newDir/`

// NewCmd returns a new command.
func NewCmd(db *bolt.DB) *cobra.Command {
Expand Down Expand Up @@ -56,14 +59,21 @@ func runMv(db *bolt.DB) cmdutil.RunEFunc {

oldName = cmdutil.NormalizeName(oldName, true)
newName = cmdutil.NormalizeName(newName, true)
oldNameIsDir := strings.HasSuffix(oldName, "/")
newNameIsDir := strings.HasSuffix(newName, "/")

if strings.HasSuffix(oldName, "/") {
if !strings.HasSuffix(newName, "/") {
if oldNameIsDir {
if !newNameIsDir {
return errors.New("cannot move a directory into a file")
}
return mvDir(db, oldName, newName)
}

// Move file into directory
if !oldNameIsDir && newNameIsDir {
newName += filepath.Base(oldName)
}

if filepath.Ext(newName) == "" {
newName += filepath.Ext(oldName)
}
Expand All @@ -76,7 +86,7 @@ func runMv(db *bolt.DB) cmdutil.RunEFunc {
return err
}

fmt.Printf("\n%q renamed as %q\n", oldName, newName)
fmt.Printf("\n%q moved to %q\n", oldName, newName)
return nil
}
}
Expand Down
37 changes: 37 additions & 0 deletions commands/file/mv/mv_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mv

import (
"path/filepath"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -56,6 +57,42 @@ func TestMvDir(t *testing.T) {
}
}

func TestMvFileIntoDir(t *testing.T) {
db := cmdutil.SetContext(t, "../../../db/testdata/database")

cases := []struct {
desc string
filename string
newDir string
}{
{
desc: "File with extension",
filename: "directory/test.csv",
newDir: "folder/",
},
{
desc: "File without extension",
filename: "directory/test",
newDir: "folder/",
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
createFile(t, db, tc.filename)

cmd := NewCmd(db)
cmd.SetArgs([]string{tc.filename, tc.newDir})

err := cmd.Execute()
assert.NoError(t, err)

_, err = file.GetCheap(db, tc.newDir+filepath.Base(tc.filename))
assert.NoError(t, err, "Failed getting the renamed file")
})
}
}

func TestMvErrors(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("For some strange reason this test fails in unix systems because the database file throws \"bad file descriptor\"")
Expand Down
5 changes: 5 additions & 0 deletions docs/commands/file/subcommands/mv.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ kure file mv oldFile newFile
Move a directory:
```
kure file mv oldDir/ newDir/
```

Move a file into a directory:
```
kure file mv oldDir/test.txt newDir/
```

0 comments on commit 7b2bde5

Please sign in to comment.