diff --git a/cal/planner.go b/cal/planner.go deleted file mode 100644 index 903562a..0000000 --- a/cal/planner.go +++ /dev/null @@ -1,35 +0,0 @@ -package main - -import ( - "time" - - "github.com/google/uuid" -) - -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, - } -} diff --git a/cal/main.go b/plan/main.go similarity index 52% rename from cal/main.go rename to plan/main.go index 48ba4cc..361ffb6 100644 --- a/cal/main.go +++ b/plan/main.go @@ -7,6 +7,7 @@ import ( "time" "github.com/urfave/cli/v2" + "go-mod.ewintr.nl/planner/item" "gopkg.in/yaml.v3" ) @@ -29,18 +30,45 @@ func main() { } app := &cli.App{ - Name: "list", - Usage: "list all events", - Action: func(*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 + 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) + }, + }, }, } @@ -49,19 +77,6 @@ func main() { os.Exit(1) } - // one := item.Event{ - // ID: "a", - // EventBody: item.EventBody{ - // Title: "title", - // Start: time.Now(), - // End: time.Now().Add(-5 * time.Second), - // }, - // } - // if err := repo.Store(one); err != nil { - // fmt.Println(err) - // os.Exit(1) - // } - // all, err := repo.FindAll() // if err != nil { // fmt.Println(err) @@ -99,6 +114,39 @@ 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/cal/memory.go b/plan/memory.go similarity index 100% rename from cal/memory.go rename to plan/memory.go diff --git a/cal/memory_test.go b/plan/memory_test.go similarity index 100% rename from cal/memory_test.go rename to plan/memory_test.go diff --git a/plan/planner.go b/plan/planner.go new file mode 100644 index 0000000..d9fcef7 --- /dev/null +++ b/plan/planner.go @@ -0,0 +1,38 @@ +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/cal/sqlite.go b/plan/sqlite.go similarity index 100% rename from cal/sqlite.go rename to plan/sqlite.go