diff --git a/plan/command/add.go b/plan/command/add.go new file mode 100644 index 0000000..3f483a9 --- /dev/null +++ b/plan/command/add.go @@ -0,0 +1,65 @@ +package command + +import ( + "fmt" + "time" + + "github.com/urfave/cli/v2" + "go-mod.ewintr.nl/planner/item" + "go-mod.ewintr.nl/planner/plan/storage" +) + +var AddCmd = &cli.Command{ + Name: "add", + Usage: "Add a new event", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "name", + Aliases: []string{"n"}, + Usage: "The event that will happen", + }, + &cli.StringFlag{ + Name: "on", + Aliases: []string{"o"}, + Usage: "The date, in YYYY-MM-DD format", + }, + &cli.StringFlag{ + Name: "at", + Aliases: []string{"a"}, + Usage: "The time, in HH:MM format. If omitted, the event will last the whole day", + }, + &cli.StringFlag{ + Name: "for", + Aliases: []string{"f"}, + Usage: "The duration, in show format (e.g. 1h30m)", + }, + }, +} + +func NewAddCmd(repo storage.EventRepo) *cli.Command { + AddCmd.Action = NewAddAction(repo) + return AddCmd +} + +func NewAddAction(repo storage.EventRepo) func(*cli.Context) error { + return func(cCtx *cli.Context) error { + desc := cCtx.String("name") + date, err := time.Parse("2006-01-02", cCtx.String("date")) + if err != nil { + return fmt.Errorf("could not parse date: %v", err) + } + + one := item.Event{ + ID: "a", + EventBody: item.EventBody{ + Title: desc, + Start: date, + }, + } + if err := repo.Store(one); err != nil { + return fmt.Errorf("could not store event: %v", err) + } + + return nil + } +} diff --git a/plan/command/list.go b/plan/command/list.go new file mode 100644 index 0000000..02b150f --- /dev/null +++ b/plan/command/list.go @@ -0,0 +1,34 @@ +package command + +import ( + "fmt" + "time" + + "github.com/urfave/cli/v2" + "go-mod.ewintr.nl/planner/plan/storage" +) + +var ListCmd = &cli.Command{ + Name: "list", + Usage: "List everything", +} + +func NewListCmd(repo storage.EventRepo) *cli.Command { + ListCmd.Action = NewListAction(repo) + return ListCmd +} + +func NewListAction(repo storage.EventRepo) func(*cli.Context) error { + return func(cCtx *cli.Context) error { + all, err := repo.FindAll() + if err != nil { + return err + } + for _, e := range all { + fmt.Printf("%s\t%s\t%s\t%s\n", e.ID, e.Title, e.Start.Format(time.DateTime), e.Duration.String()) + } + + return nil + } + +} diff --git a/plan/main.go b/plan/main.go index 8a956fa..6dadbc5 100644 --- a/plan/main.go +++ b/plan/main.go @@ -4,10 +4,10 @@ import ( "fmt" "os" "path/filepath" - "time" "github.com/urfave/cli/v2" - "go-mod.ewintr.nl/planner/item" + "go-mod.ewintr.nl/planner/plan/command" + "go-mod.ewintr.nl/planner/plan/storage" "gopkg.in/yaml.v3" ) @@ -23,7 +23,7 @@ func main() { os.Exit(1) } - repo, err := NewSqlite(conf.DBPath) + repo, err := storage.NewSqlite(conf.DBPath) if err != nil { fmt.Printf("could not open db file: %s\n", err) os.Exit(1) @@ -33,42 +33,8 @@ func main() { Name: "plan", Usage: "Plan your day with events", Commands: []*cli.Command{ - { - Name: "list", - Usage: "List everything", - Action: func(cCtx *cli.Context) error { - return List(repo) - }, - }, - { - Name: "add", - Usage: "Add a new event", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "name", - Aliases: []string{"n"}, - Usage: "The event that will happen", - }, - &cli.StringFlag{ - Name: "date", - Aliases: []string{"d"}, - Usage: "The date, in YYYY-MM-DD format", - }, - &cli.StringFlag{ - Name: "time", - Aliases: []string{"t"}, - Usage: "The time, in HH:MM format. If omitted, the event will last the whole day", - }, - &cli.StringFlag{ - Name: "for", - Aliases: []string{"f"}, - Usage: "The duration, in show format (e.g. 1h30m)", - }, - }, - Action: func(cCtx *cli.Context) error { - return Add(cCtx, repo) - }, - }, + command.NewAddCmd(repo), + command.NewListCmd(repo), }, } @@ -114,39 +80,6 @@ func main() { // fmt.Printf("%+v\n", items) } -func List(repo EventRepo) error { - all, err := repo.FindAll() - if err != nil { - return err - } - for _, e := range all { - fmt.Printf("%s\t%s\t%s\t%s\n", e.ID, e.Title, e.Start.Format(time.DateTime), e.Duration.String()) - } - - return nil -} - -func Add(cCtx *cli.Context, repo EventRepo) error { - desc := cCtx.String("name") - date, err := time.Parse("2006-01-02", cCtx.String("date")) - if err != nil { - return fmt.Errorf("could not parse date: %v", err) - } - - one := item.Event{ - ID: "a", - EventBody: item.EventBody{ - Title: desc, - Start: date, - }, - } - if err := repo.Store(one); err != nil { - return fmt.Errorf("could not store event: %v", err) - } - - return nil -} - type Configuration struct { DBPath string `yaml:"dbpath"` } diff --git a/plan/planner.go b/plan/planner.go deleted file mode 100644 index d9fcef7..0000000 --- a/plan/planner.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -import "go-mod.ewintr.nl/planner/item" - -// type Kind string - -// const ( -// KindTask Kind = "task" -// KindEvent Kind = "event" -// ) - -// var ( -// KnownKinds = []Kind{KindTask, KindEvent} -// ) - -// type Item struct { -// ID string `json:"id"` -// Kind Kind `json:"kind"` -// Updated time.Time `json:"updated"` -// Deleted bool `json:"deleted"` -// Body string `json:"body"` -// } - -// func NewItem(k Kind, body string) Item { -// return Item{ -// ID: uuid.New().String(), -// Kind: k, -// Updated: time.Now(), -// Body: body, -// } -// } - -type EventRepo interface { - Store(event item.Event) error - Find(id string) (item.Event, error) - FindAll() ([]item.Event, error) - Delete(id string) error -} diff --git a/plan/memory.go b/plan/storage/memory.go similarity index 98% rename from plan/memory.go rename to plan/storage/memory.go index 2656f25..2100eb7 100644 --- a/plan/memory.go +++ b/plan/storage/memory.go @@ -1,4 +1,4 @@ -package main +package storage import ( "errors" diff --git a/plan/memory_test.go b/plan/storage/memory_test.go similarity index 98% rename from plan/memory_test.go rename to plan/storage/memory_test.go index fcdd981..f9d7e22 100644 --- a/plan/memory_test.go +++ b/plan/storage/memory_test.go @@ -1,4 +1,4 @@ -package main +package storage import ( "testing" diff --git a/plan/sqlite.go b/plan/storage/sqlite.go similarity index 96% rename from plan/sqlite.go rename to plan/storage/sqlite.go index 45b4e18..86c13e5 100644 --- a/plan/sqlite.go +++ b/plan/storage/sqlite.go @@ -1,4 +1,4 @@ -package main +package storage import ( "database/sql" @@ -82,7 +82,7 @@ WHERE id = ?`, id).Scan(&event.ID, &event.Title, &event.Start, &durStr) } dur, err := time.ParseDuration(durStr) if err != nil { - return item.Event{}, fmt.Errorf("%w: %v", err) + return item.Event{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } event.Duration = dur @@ -107,7 +107,7 @@ FROM events`) } dur, err := time.ParseDuration(durStr) if err != nil { - return nil, fmt.Errorf("%w: %v", err) + return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } event.Duration = dur result = append(result, event) diff --git a/plan/storage/storage.go b/plan/storage/storage.go new file mode 100644 index 0000000..3e8369d --- /dev/null +++ b/plan/storage/storage.go @@ -0,0 +1,10 @@ +package storage + +import "go-mod.ewintr.nl/planner/item" + +type EventRepo interface { + Store(event item.Event) error + Find(id string) (item.Event, error) + FindAll() ([]item.Event, error) + Delete(id string) error +}