split commands
This commit is contained in:
parent
66f93d2bd5
commit
de7f5b8c74
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
77
plan/main.go
77
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"`
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package storage
|
||||
|
||||
import (
|
||||
"errors"
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package storage
|
||||
|
||||
import (
|
||||
"testing"
|
|
@ -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)
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue