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), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
dateString, err := cmd.Flags().GetString("due") dateString, err := cmd.Flags().GetString("due")
if err != nil { errPanic(err, "")
log.Fatal(err) comp, err := cmd.Flags().GetBool("complete")
} errPanic(err, "")
createTodo(strings.Join(args, " "), dateString) createTodo(strings.Join(args, " "), dateString, comp)
// fmt.Println(time.Parse("02/01/2006T15:04", t))
// fmt.Println(time.Parse("02/01/2006", t))
}, },
} }
func init() { 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")
// Here you will define your flags and configuration settings. // Here you will define your flags and configuration settings.

View File

@ -48,13 +48,13 @@ func openDB() *gorm.DB {
return db return db
} }
func createTodo(desc string, date string) { func createTodo(desc string, date string, comp bool) {
db := openDB() db := openDB()
err := db.AutoMigrate(&Todo{}) err := db.AutoMigrate(&Todo{})
errPanic(err, "failed to migrate database") errPanic(err, "failed to migrate database")
d := parseDateString(date) 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) db.Create(&t)
} }