parent
7a86bee9fa
commit
c822f8ec91
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
|
||||||
|
|
||||||
|
*/
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// addCmd represents the add command
|
||||||
|
var addCmd = &cobra.Command{
|
||||||
|
Use: "add [entry title]",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
Short: "Add a new diary entry with the given title",
|
||||||
|
Long: `Add a new diary entry, for example
|
||||||
|
and usage of using your command. For example:
|
||||||
|
|
||||||
|
$ diary add "Issue when fixing bad data in prod"
|
||||||
|
1134
|
||||||
|
|
||||||
|
The returned number is the ID of the added entry.
|
||||||
|
This ID can be used in other commands.
|
||||||
|
`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
var entry = App.NewDiaryEntry(args[0])
|
||||||
|
fmt.Println(entry.Id)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(addCmd)
|
||||||
|
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
|
// and all subcommands, e.g.:
|
||||||
|
// addCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||||
|
|
||||||
|
// Cobra supports local flags which will only run when this command
|
||||||
|
// is called directly, e.g.:
|
||||||
|
// addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
|
||||||
|
|
||||||
|
*/
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// deleteCmd represents the delete command
|
||||||
|
var deleteCmd = &cobra.Command{
|
||||||
|
Use: "delete [Id]",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
Short: "Delete the given entry. Will ask for confirmation",
|
||||||
|
Long: `We do not recommend using delete, unless something was truly added by mistake.
|
||||||
|
Delete is a hard delete. There is no recovery.`,
|
||||||
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
id, err := strconv.Atoi(args[0])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
entry, err := App.GetDiaryEntry(id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println("Are you sure you want to delete the following entry? (y/n)")
|
||||||
|
fmt.Println(entry.Title)
|
||||||
|
var response string
|
||||||
|
fmt.Scanln(&response)
|
||||||
|
if response == "y" {
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
fmt.Println("Will not delete entry " + args[0])
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
id, err := strconv.Atoi(args[0])
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
_, err = App.DeleteDiaryEntry(id)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(deleteCmd)
|
||||||
|
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
|
// and all subcommands, e.g.:
|
||||||
|
// deleteCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||||
|
|
||||||
|
// Cobra supports local flags which will only run when this command
|
||||||
|
// is called directly, e.g.:
|
||||||
|
// deleteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type NotFoundError struct {
|
||||||
|
given_id int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *NotFoundError) Error() string {
|
||||||
|
return fmt.Sprintf("Could not find id %d", e.given_id)
|
||||||
|
}
|
Loading…
Reference in new issue