diff --git a/cmd/mv.go b/cmd/mv.go index 7fc7374..1f25c7b 100644 --- a/cmd/mv.go +++ b/cmd/mv.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "os" "github.com/spf13/cobra" ) @@ -9,8 +10,23 @@ import ( var mvCmd = &cobra.Command{ Use: "mv", Short: "Rename a note", + Args: cobra.ExactArgs(2), Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("not implemented\n") + oldpath := getPath(args[0]) + newfile := args[1] + newpath := getPath(newfile) + + _, err := os.Stat(newpath) + if err == nil { + fmt.Printf("cannot rename as '%s' already exists\n", newfile) + return + } + + err = os.Rename(oldpath, newpath) + if err != nil { + fmt.Println(err) + return + } }, } diff --git a/cmd/new.go b/cmd/new.go index e1c3e51..a9dbdaa 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -13,21 +13,21 @@ var newCmd = &cobra.Command{ Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { filename := args[0] - fullPath := getPath(filename) + path := getPath(filename) - _, err := os.Stat(fullPath) + _, err := os.Stat(path) if err == nil { fmt.Printf("%s already exists\n", filename) return } - _, err = os.Create(fullPath) + _, err = os.Create(path) if err != nil { fmt.Printf("error creating %s: %s\n", filename, err) return } - err = openFile(fullPath) + err = openFile(path) if err != nil { fmt.Println(err) return diff --git a/cmd/open.go b/cmd/open.go index dc93144..b3c519e 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -11,9 +11,9 @@ var openCmd = &cobra.Command{ Short: "Open an existing note", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - fullPath := getPath(args[0]) + path := getPath(args[0]) - err := openFile(fullPath) + err := openFile(path) if err != nil { fmt.Println(err) return diff --git a/cmd/rm.go b/cmd/rm.go index af02a48..eba0b97 100644 --- a/cmd/rm.go +++ b/cmd/rm.go @@ -39,15 +39,15 @@ var rmCmd = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { filename := args[0] - fullPath := getPath(filename) + path := getPath(filename) - _, err := os.Stat(fullPath) + _, err := os.Stat(path) if errors.Is(err, os.ErrNotExist) { fmt.Printf("%s does not exist\n", filename) return } - err = os.Remove(fullPath) + err = os.Remove(path) if err != nil { fmt.Printf("error deleting %s: %s\n", filename, err) return