add edit, add completed field
This commit is contained in:
parent
2568342714
commit
2068f76c21
27
cmd/ed.go
27
cmd/ed.go
@ -17,7 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@ -32,14 +33,36 @@ and usage of using your command. For example:
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
Args: func(cmd *cobra.Command, args []string) error {
|
||||
if cmd.Flags().NFlag() >= 1 {
|
||||
if err := cobra.MinimumNArgs(1)(cmd, args); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
if err := cobra.MinimumNArgs(2)(cmd, args); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("ed called")
|
||||
dateString, err := cmd.Flags().GetString("due")
|
||||
errPanic(err, "")
|
||||
comp, err := cmd.Flags().GetBool("complete")
|
||||
errPanic(err, "")
|
||||
id, err := strconv.ParseUint(args[0], 10, 0)
|
||||
errPanic(err, "failed to parse uint")
|
||||
updateTodo(id, strings.Join(args[1:], " "), dateString, comp)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(edCmd)
|
||||
|
||||
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
|
||||
|
12
cmd/ls.go
12
cmd/ls.go
@ -21,6 +21,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/jedib0t/go-pretty/v6/table"
|
||||
"github.com/jedib0t/go-pretty/v6/text"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
@ -42,7 +43,7 @@ to quickly create a Cobra application.`,
|
||||
cols := int(ws.Col)
|
||||
t := table.NewWriter()
|
||||
t.SetOutputMirror(os.Stdout)
|
||||
t.AppendHeader(table.Row{"#", "Description", "Due date"})
|
||||
t.AppendHeader(table.Row{"#", "Description", "Due date", "Completed"})
|
||||
for _, todo := range todos {
|
||||
date := todo.DueDate.Format("02/01/2006 15:04")
|
||||
d := time.Time{}
|
||||
@ -53,11 +54,18 @@ to quickly create a Cobra application.`,
|
||||
if len(desc) > (cols - 20) {
|
||||
desc = insertNth(desc, cols-20)
|
||||
}
|
||||
t.AppendRow([]interface{}{todo.ID, desc, date})
|
||||
comp := '✗'
|
||||
if todo.IsDone {
|
||||
comp = '✓'
|
||||
}
|
||||
t.AppendRow([]interface{}{todo.ID, desc, date, string(comp)})
|
||||
// data[i] = []string{strconv.FormatUint(todo.ID, 10), todo.Desc, todo.DueDate.Format("")}
|
||||
}
|
||||
t.SetStyle(table.StyleLight)
|
||||
t.SetAllowedRowLength(cols)
|
||||
t.SetColumnConfigs([]table.ColumnConfig{
|
||||
{Number: 4, Align: text.AlignCenter},
|
||||
})
|
||||
t.Render()
|
||||
},
|
||||
}
|
||||
|
41
cmd/todo.go
41
cmd/todo.go
@ -1,6 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
@ -12,11 +13,12 @@ type Todo struct {
|
||||
Desc string
|
||||
DueDate time.Time
|
||||
CreatedAt time.Time
|
||||
IsDone bool
|
||||
}
|
||||
|
||||
func errPanic(err error, msg string) {
|
||||
if err != nil {
|
||||
panic(msg)
|
||||
log.Fatalf(msg, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,6 +37,7 @@ func parseDateString(date string) time.Time {
|
||||
return t
|
||||
}
|
||||
}
|
||||
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
@ -51,17 +54,26 @@ func createTodo(desc string, date string) {
|
||||
errPanic(err, "failed to migrate database")
|
||||
d := parseDateString(date)
|
||||
|
||||
t := Todo{Desc: desc, DueDate: d, CreatedAt: time.Now()}
|
||||
t := Todo{Desc: desc, DueDate: d, CreatedAt: time.Now(), IsDone: false}
|
||||
db.Create(&t)
|
||||
}
|
||||
|
||||
func updateTodo(id uint64, new Todo) {
|
||||
func updateTodo(id uint64, desc string, date string, comp bool) {
|
||||
db := openDB()
|
||||
var t Todo
|
||||
res := db.First(&t, id)
|
||||
errPanic(res.Error, "failed to find id")
|
||||
new.ID = t.ID
|
||||
db.Model(&t).Updates(new)
|
||||
if desc != "" {
|
||||
t.Desc = desc
|
||||
}
|
||||
if date != "" {
|
||||
d := parseDateString(date)
|
||||
t.DueDate = d
|
||||
}
|
||||
if comp {
|
||||
t.IsDone = !t.IsDone
|
||||
}
|
||||
db.Model(&t).Updates(t)
|
||||
}
|
||||
|
||||
func deleteTodo(id uint64) {
|
||||
@ -77,3 +89,22 @@ func getAllTodos() []Todo {
|
||||
|
||||
return todos
|
||||
}
|
||||
|
||||
func getDoneTodos() []Todo {
|
||||
db := openDB()
|
||||
var todos []Todo
|
||||
res := db.Where(&Todo{IsDone: true}).Find(&todos)
|
||||
errPanic(res.Error, "failed to get all todos")
|
||||
|
||||
return todos
|
||||
}
|
||||
|
||||
|
||||
func getUnDoneTodos() []Todo {
|
||||
db := openDB()
|
||||
var todos []Todo
|
||||
res := db.Where(&Todo{IsDone: false}).Find(&todos)
|
||||
errPanic(res.Error, "failed to get all todos")
|
||||
|
||||
return todos
|
||||
}
|
||||
|
4
go.mod
4
go.mod
@ -5,6 +5,7 @@ go 1.19
|
||||
require (
|
||||
github.com/jedib0t/go-pretty/v6 v6.3.8
|
||||
github.com/spf13/cobra v1.5.0
|
||||
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2
|
||||
gorm.io/driver/sqlite v1.3.6
|
||||
gorm.io/gorm v1.23.8
|
||||
)
|
||||
@ -15,7 +16,6 @@ require (
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.13 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.12 // indirect
|
||||
github.com/rivo/uniseg v0.2.0 // indirect
|
||||
github.com/rivo/uniseg v0.3.4 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user