From 019fc93c06e1dd56711f1e930fcc9305b1d35a13 Mon Sep 17 00:00:00 2001 From: fiplox Date: Sat, 10 Sep 2022 20:42:36 +0200 Subject: [PATCH] cleanup, add some comments --- cmd/ed.go | 9 --------- cmd/ls.go | 10 ---------- cmd/new.go | 11 ----------- cmd/rm.go | 11 +---------- cmd/root.go | 9 --------- cmd/todo.go | 7 +++++++ 6 files changed, 8 insertions(+), 49 deletions(-) diff --git a/cmd/ed.go b/cmd/ed.go index f2d0e00..b5c98ea 100644 --- a/cmd/ed.go +++ b/cmd/ed.go @@ -63,13 +63,4 @@ func init() { edCmd.Flags().StringP("due", "d", "", "due date") edCmd.Flags().BoolP("complete", "c", false, "toggle task as completed/uncompleted") - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // edCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // edCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/ls.go b/cmd/ls.go index e991d3b..739bfc6 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -80,14 +80,4 @@ func insertNth(s string, n int) string { func init() { rootCmd.AddCommand(lsCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // lsCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // lsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/new.go b/cmd/new.go index 50bfbda..f4cc0c5 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -17,7 +17,6 @@ along with this program. If not, see . package cmd import ( - "log" "strconv" "strings" @@ -61,14 +60,4 @@ func init() { rootCmd.AddCommand(newCmd) newCmd.Flags().StringP("due", "d", "", "due date") newCmd.Flags().BoolP("complete", "c", false, "mark todo as completed") - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // newCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // newCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/rm.go b/cmd/rm.go index ba0b494..8cf6daa 100644 --- a/cmd/rm.go +++ b/cmd/rm.go @@ -25,7 +25,7 @@ import ( // rmCmd represents the rm command var rmCmd = &cobra.Command{ Use: "rm", - Short: "A brief description of your command", + Short: "Remove a todo", Long: `A longer description that spans multiple lines and likely contains examples and usage of using your command. For example: @@ -43,13 +43,4 @@ to quickly create a Cobra application.`, func init() { rootCmd.AddCommand(rmCmd) - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // rmCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // rmCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/root.go b/cmd/root.go index b62af03..e2309fc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -47,13 +47,4 @@ func Execute() { } func init() { - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application. - - // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.todo.yaml)") - - // Cobra also supports local flags, which will only run - // when this action is called directly. - // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/todo.go b/cmd/todo.go index b5bf6cd..a93ce8d 100644 --- a/cmd/todo.go +++ b/cmd/todo.go @@ -16,12 +16,14 @@ type Todo struct { IsDone bool } +// errPanic exits program with msg and error details if there is an error. func errPanic(err error, msg string) { if err != nil { log.Fatalf(msg, err) } } +// parseDateString returns parsed time from string or time.Time{}. func parseDateString(date string) time.Time { var ( t time.Time @@ -41,6 +43,7 @@ func parseDateString(date string) time.Time { return time.Time{} } +// openDB returns gorm database. func openDB() *gorm.DB { db, err := gorm.Open(sqlite.Open("todo.db"), &gorm.Config{}) errPanic(err, "failed to connect database") @@ -48,6 +51,7 @@ func openDB() *gorm.DB { return db } +// createTodo creates todo entry in the database. func createTodo(desc string, date string, comp bool) { db := openDB() err := db.AutoMigrate(&Todo{}) @@ -58,6 +62,7 @@ func createTodo(desc string, date string, comp bool) { db.Create(&t) } +// updateTodo updates an entry of id todo in the database. func updateTodo(id uint64, desc string, date string, comp bool) { db := openDB() var t Todo @@ -76,11 +81,13 @@ func updateTodo(id uint64, desc string, date string, comp bool) { db.Model(&t).Updates(t) } +// deleteTodo deletes an entry of id todo in the database. func deleteTodo(id uint64) { db := openDB() db.Delete(&Todo{}, id) } +// getAllTodos return all todos from the database. func getAllTodos() []Todo { db := openDB() var todos []Todo