add
This commit is contained in:
parent
1f158877de
commit
7ae20b5727
|
@ -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,
|
||||
}
|
||||
}
|
|
@ -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"`
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue