complete flag for new subcommand

This commit is contained in:
fiplox 2022-09-10 20:34:52 +02:00
parent 427c863d1f
commit 127e9fb72d
2 changed files with 7 additions and 8 deletions

View File

@ -50,18 +50,17 @@ to quickly create a Cobra application.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
dateString, err := cmd.Flags().GetString("due")
if err != nil {
log.Fatal(err)
}
createTodo(strings.Join(args, " "), dateString)
// fmt.Println(time.Parse("02/01/2006T15:04", t))
// fmt.Println(time.Parse("02/01/2006", t))
errPanic(err, "")
comp, err := cmd.Flags().GetBool("complete")
errPanic(err, "")
createTodo(strings.Join(args, " "), dateString, comp)
},
}
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.

View File

@ -48,13 +48,13 @@ func openDB() *gorm.DB {
return db
}
func createTodo(desc string, date string) {
func createTodo(desc string, date string, comp bool) {
db := openDB()
err := db.AutoMigrate(&Todo{})
errPanic(err, "failed to migrate database")
d := parseDateString(date)
t := Todo{Desc: desc, DueDate: d, CreatedAt: time.Now(), IsDone: false}
t := Todo{Desc: desc, DueDate: d, CreatedAt: time.Now(), IsDone: comp}
db.Create(&t)
}