cleanup, add some comments

This commit is contained in:
fiplox 2022-09-10 20:42:36 +02:00
parent 127e9fb72d
commit 019fc93c06
6 changed files with 8 additions and 49 deletions

View File

@ -63,13 +63,4 @@ func init() {
edCmd.Flags().StringP("due", "d", "", "due date") edCmd.Flags().StringP("due", "d", "", "due date")
edCmd.Flags().BoolP("complete", "c", false, "toggle task as completed/uncompleted") 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")
} }

View File

@ -80,14 +80,4 @@ func insertNth(s string, n int) string {
func init() { func init() {
rootCmd.AddCommand(lsCmd) 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")
} }

View File

@ -17,7 +17,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd package cmd
import ( import (
"log"
"strconv" "strconv"
"strings" "strings"
@ -61,14 +60,4 @@ func init() {
rootCmd.AddCommand(newCmd) rootCmd.AddCommand(newCmd)
newCmd.Flags().StringP("due", "d", "", "due date") newCmd.Flags().StringP("due", "d", "", "due date")
newCmd.Flags().BoolP("complete", "c", false, "mark todo as completed") 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")
} }

View File

@ -25,7 +25,7 @@ import (
// rmCmd represents the rm command // rmCmd represents the rm command
var rmCmd = &cobra.Command{ var rmCmd = &cobra.Command{
Use: "rm", 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 Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example: and usage of using your command. For example:
@ -43,13 +43,4 @@ to quickly create a Cobra application.`,
func init() { func init() {
rootCmd.AddCommand(rmCmd) 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")
} }

View File

@ -47,13 +47,4 @@ func Execute() {
} }
func init() { 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")
} }

View File

@ -16,12 +16,14 @@ type Todo struct {
IsDone bool IsDone bool
} }
// errPanic exits program with msg and error details if there is an error.
func errPanic(err error, msg string) { func errPanic(err error, msg string) {
if err != nil { if err != nil {
log.Fatalf(msg, err) log.Fatalf(msg, err)
} }
} }
// parseDateString returns parsed time from string or time.Time{}.
func parseDateString(date string) time.Time { func parseDateString(date string) time.Time {
var ( var (
t time.Time t time.Time
@ -41,6 +43,7 @@ func parseDateString(date string) time.Time {
return time.Time{} return time.Time{}
} }
// openDB returns gorm database.
func openDB() *gorm.DB { func openDB() *gorm.DB {
db, err := gorm.Open(sqlite.Open("todo.db"), &gorm.Config{}) db, err := gorm.Open(sqlite.Open("todo.db"), &gorm.Config{})
errPanic(err, "failed to connect database") errPanic(err, "failed to connect database")
@ -48,6 +51,7 @@ func openDB() *gorm.DB {
return db return db
} }
// createTodo creates todo entry in the database.
func createTodo(desc string, date string, comp bool) { func createTodo(desc string, date string, comp bool) {
db := openDB() db := openDB()
err := db.AutoMigrate(&Todo{}) err := db.AutoMigrate(&Todo{})
@ -58,6 +62,7 @@ func createTodo(desc string, date string, comp bool) {
db.Create(&t) db.Create(&t)
} }
// updateTodo updates an entry of id todo in the database.
func updateTodo(id uint64, desc string, date string, comp bool) { func updateTodo(id uint64, desc string, date string, comp bool) {
db := openDB() db := openDB()
var t Todo var t Todo
@ -76,11 +81,13 @@ func updateTodo(id uint64, desc string, date string, comp bool) {
db.Model(&t).Updates(t) db.Model(&t).Updates(t)
} }
// deleteTodo deletes an entry of id todo in the database.
func deleteTodo(id uint64) { func deleteTodo(id uint64) {
db := openDB() db := openDB()
db.Delete(&Todo{}, id) db.Delete(&Todo{}, id)
} }
// getAllTodos return all todos from the database.
func getAllTodos() []Todo { func getAllTodos() []Todo {
db := openDB() db := openDB()
var todos []Todo var todos []Todo