From 6dab576f271ec9a485da6df8efcc75eb49436cca Mon Sep 17 00:00:00 2001 From: Kevin Schoon Date: Sat, 20 Jan 2018 18:01:53 +0800 Subject: [PATCH] add tags --- main.go | 2 ++ store.go | 12 ++++++++---- types.go | 8 +++++--- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index b30c964..aff5eeb 100644 --- a/main.go +++ b/main.go @@ -43,6 +43,7 @@ func start(path *string) func(*cli.Cmd) { duration = cmd.StringOpt("d duration", "25m", "duration of each stent") count = cmd.IntOpt("c count", 4, "number of working stents") message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task") + tags = cmd.StringsOpt("t tag", []string{}, "tags associated with this task") ) cmd.Action = func() { parsed, err := time.ParseDuration(*duration) @@ -52,6 +53,7 @@ func start(path *string) func(*cli.Cmd) { defer db.Close() task := Task{ Message: *message, + Tags: *tags, count: *count, duration: parsed, } diff --git a/store.go b/store.go index b40fd03..6dbd65e 100644 --- a/store.go +++ b/store.go @@ -5,6 +5,7 @@ import ( _ "github.com/mattn/go-sqlite3" "os" "os/user" + "strings" "time" ) @@ -36,7 +37,7 @@ func (s Store) CreateTask(task Task) (int, error) { if err != nil { return -1, err } - _, err = tx.Exec("INSERT INTO task (message) VALUES ($1)", task.Message) + _, err = tx.Exec("INSERT INTO task (message,tags) VALUES ($1,$2)", task.Message, strings.Join(task.Tags, ",")) if err != nil { tx.Rollback() return -1, err @@ -60,17 +61,19 @@ func (s Store) CreateRecord(taskID int, record Record) error { } func (s Store) ReadTasks() ([]*Task, error) { - rows, err := s.db.Query(`SELECT rowid,message FROM task`) + rows, err := s.db.Query(`SELECT rowid,message,tags FROM task`) if err != nil { return nil, err } tasks := []*Task{} for rows.Next() { + var tags string task := &Task{Records: []*Record{}} - err = rows.Scan(&task.ID, &task.Message) + err = rows.Scan(&task.ID, &task.Message, &tags) if err != nil { return nil, err } + task.Tags = strings.Split(tags, ",") records, err := s.ReadRecords(task.ID) if err != nil { return nil, err @@ -131,7 +134,8 @@ func (s Store) Close() error { return s.db.Close() } func initDB(db *Store) error { stmt := ` CREATE TABLE task ( - message TEXT + message TEXT, + tags TEXT ); CREATE TABLE record ( task_id INTEGER, diff --git a/types.go b/types.go index 1763c21..86eda07 100644 --- a/types.go +++ b/types.go @@ -7,9 +7,11 @@ import ( // Task describes some activity type Task struct { - ID int `json:"id"` - Message string `json:"message"` - Records []*Record `json:"records"` + ID int `json:"id"` + Message string `json:"message"` + Records []*Record `json:"records"` + // Free-form tags associated with this task + Tags []string `json:"tags"` count int duration time.Duration }