This commit is contained in:
Kevin Schoon 2018-01-20 18:01:53 +08:00
parent b6c74bb61a
commit 6dab576f27
3 changed files with 15 additions and 7 deletions

View File

@ -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,
}

View File

@ -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,

View File

@ -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
}