This commit is contained in:
Erik Winter 2024-09-27 07:38:50 +02:00
parent 1f158877de
commit 7ae20b5727
6 changed files with 111 additions and 60 deletions

View File

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

View File

@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go-mod.ewintr.nl/planner/item"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -29,18 +30,45 @@ func main() {
} }
app := &cli.App{ app := &cli.App{
Name: "list", Name: "plan",
Usage: "list all events", Usage: "Plan your day with events",
Action: func(*cli.Context) error { Commands: []*cli.Command{
all, err := repo.FindAll() {
if err != nil { Name: "list",
return err Usage: "List everything",
} Action: func(cCtx *cli.Context) error {
for _, e := range all { return List(repo)
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: "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) 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() // all, err := repo.FindAll()
// if err != nil { // if err != nil {
// fmt.Println(err) // fmt.Println(err)
@ -99,6 +114,39 @@ func main() {
// fmt.Printf("%+v\n", items) // 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 { type Configuration struct {
DBPath string `yaml:"dbpath"` DBPath string `yaml:"dbpath"`
} }

38
plan/planner.go Normal file
View File

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