Skip to content

Commit

Permalink
Add command for renaming a file
Browse files Browse the repository at this point in the history
  • Loading branch information
mdcurran committed Mar 18, 2022
1 parent 65b02cf commit 7f53c87
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
18 changes: 17 additions & 1 deletion cmd/mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@ package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

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
}
},
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7f53c87

Please sign in to comment.