Skip to content

Commit

Permalink
Add command for removing note
Browse files Browse the repository at this point in the history
  • Loading branch information
mdcurran committed Mar 18, 2022
1 parent 3339318 commit 65b02cf
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
19 changes: 19 additions & 0 deletions cmd/mv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var mvCmd = &cobra.Command{
Use: "mv",
Short: "Rename a note",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("not implemented\n")
},
}

func init() {
rootCmd.AddCommand(mvCmd)
}
2 changes: 1 addition & 1 deletion cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var newCmd = &cobra.Command{

_, err = os.Create(fullPath)
if err != nil {
fmt.Printf("error creating %s: %s", filename, err)
fmt.Printf("error creating %s: %s\n", filename, err)
return
}

Expand Down
60 changes: 60 additions & 0 deletions cmd/rm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cmd

import (
"bufio"
"errors"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
)

var rmCmd = &cobra.Command{
Use: "rm",
Short: "Delete a note permanently",
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("delete? [Y/n]")

reader := bufio.NewReader(os.Stdin)
for {
s, _ := reader.ReadString('\n')
s = strings.TrimSuffix(s, "\n")
s = strings.ToLower(s)
if len(s) > 1 {
fmt.Fprintln(os.Stderr, "Please enter Y or N")
continue
}
if strings.Compare(s, "n") == 0 {
return fmt.Errorf("cancelling...")
} else if strings.Compare(s, "y") == 0 {
break
} else {
continue
}
}

return nil
},
Run: func(cmd *cobra.Command, args []string) {
filename := args[0]
fullPath := getPath(filename)

_, err := os.Stat(fullPath)
if errors.Is(err, os.ErrNotExist) {
fmt.Printf("%s does not exist\n", filename)
return
}

err = os.Remove(fullPath)
if err != nil {
fmt.Printf("error deleting %s: %s\n", filename, err)
return
}
},
}

func init() {
rootCmd.AddCommand(rmCmd)
}

0 comments on commit 65b02cf

Please sign in to comment.