add edit, add completed field

This commit is contained in:
fiplox 2022-09-10 18:54:17 +02:00
parent 2568342714
commit 2068f76c21
4 changed files with 73 additions and 11 deletions

View File

@ -17,7 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd package cmd
import ( import (
"fmt" "strconv"
"strings"
"github.com/spf13/cobra" "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. Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files This application is a tool to generate the needed files
to quickly create a Cobra application.`, 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) { 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() { func init() {
rootCmd.AddCommand(edCmd) 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. // Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command // Cobra supports Persistent Flags which will work for this command

View File

@ -21,6 +21,7 @@ import (
"time" "time"
"github.com/jedib0t/go-pretty/v6/table" "github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -42,7 +43,7 @@ to quickly create a Cobra application.`,
cols := int(ws.Col) cols := int(ws.Col)
t := table.NewWriter() t := table.NewWriter()
t.SetOutputMirror(os.Stdout) t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Description", "Due date"}) t.AppendHeader(table.Row{"#", "Description", "Due date", "Completed"})
for _, todo := range todos { for _, todo := range todos {
date := todo.DueDate.Format("02/01/2006 15:04") date := todo.DueDate.Format("02/01/2006 15:04")
d := time.Time{} d := time.Time{}
@ -53,11 +54,18 @@ to quickly create a Cobra application.`,
if len(desc) > (cols - 20) { if len(desc) > (cols - 20) {
desc = insertNth(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("")} // data[i] = []string{strconv.FormatUint(todo.ID, 10), todo.Desc, todo.DueDate.Format("")}
} }
t.SetStyle(table.StyleLight) t.SetStyle(table.StyleLight)
t.SetAllowedRowLength(cols) t.SetAllowedRowLength(cols)
t.SetColumnConfigs([]table.ColumnConfig{
{Number: 4, Align: text.AlignCenter},
})
t.Render() t.Render()
}, },
} }

View File

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"log"
"time" "time"
"gorm.io/driver/sqlite" "gorm.io/driver/sqlite"
@ -12,11 +13,12 @@ type Todo struct {
Desc string Desc string
DueDate time.Time DueDate time.Time
CreatedAt time.Time CreatedAt time.Time
IsDone bool
} }
func errPanic(err error, msg string) { func errPanic(err error, msg string) {
if err != nil { if err != nil {
panic(msg) log.Fatalf(msg, err)
} }
} }
@ -35,6 +37,7 @@ func parseDateString(date string) time.Time {
return t return t
} }
} }
return time.Time{} return time.Time{}
} }
@ -51,17 +54,26 @@ func createTodo(desc string, date string) {
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()} t := Todo{Desc: desc, DueDate: d, CreatedAt: time.Now(), IsDone: false}
db.Create(&t) db.Create(&t)
} }
func updateTodo(id uint64, new Todo) { func updateTodo(id uint64, desc string, date string, comp bool) {
db := openDB() db := openDB()
var t Todo var t Todo
res := db.First(&t, id) res := db.First(&t, id)
errPanic(res.Error, "failed to find id") errPanic(res.Error, "failed to find id")
new.ID = t.ID if desc != "" {
db.Model(&t).Updates(new) 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) { func deleteTodo(id uint64) {
@ -77,3 +89,22 @@ func getAllTodos() []Todo {
return todos 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
View File

@ -5,6 +5,7 @@ go 1.19
require ( require (
github.com/jedib0t/go-pretty/v6 v6.3.8 github.com/jedib0t/go-pretty/v6 v6.3.8
github.com/spf13/cobra v1.5.0 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/driver/sqlite v1.3.6
gorm.io/gorm v1.23.8 gorm.io/gorm v1.23.8
) )
@ -15,7 +16,6 @@ require (
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mattn/go-sqlite3 v1.14.12 // 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 github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect
) )