split commands

This commit is contained in:
Erik Winter 2024-09-30 07:34:40 +02:00
parent 66f93d2bd5
commit de7f5b8c74
8 changed files with 119 additions and 115 deletions

65
plan/command/add.go Normal file
View File

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

34
plan/command/list.go Normal file
View File

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

View File

@ -4,10 +4,10 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"time"
"github.com/urfave/cli/v2" "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" "gopkg.in/yaml.v3"
) )
@ -23,7 +23,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
repo, err := NewSqlite(conf.DBPath) repo, err := storage.NewSqlite(conf.DBPath)
if err != nil { if err != nil {
fmt.Printf("could not open db file: %s\n", err) fmt.Printf("could not open db file: %s\n", err)
os.Exit(1) os.Exit(1)
@ -33,42 +33,8 @@ func main() {
Name: "plan", Name: "plan",
Usage: "Plan your day with events", Usage: "Plan your day with events",
Commands: []*cli.Command{ Commands: []*cli.Command{
{ command.NewAddCmd(repo),
Name: "list", command.NewListCmd(repo),
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)
},
},
}, },
} }
@ -114,39 +80,6 @@ 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"`
} }

View File

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

View File

@ -1,4 +1,4 @@
package main package storage
import ( import (
"errors" "errors"

View File

@ -1,4 +1,4 @@
package main package storage
import ( import (
"testing" "testing"

View File

@ -1,4 +1,4 @@
package main package storage
import ( import (
"database/sql" "database/sql"
@ -82,7 +82,7 @@ WHERE id = ?`, id).Scan(&event.ID, &event.Title, &event.Start, &durStr)
} }
dur, err := time.ParseDuration(durStr) dur, err := time.ParseDuration(durStr)
if err != nil { if err != nil {
return item.Event{}, fmt.Errorf("%w: %v", err) return item.Event{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
} }
event.Duration = dur event.Duration = dur
@ -107,7 +107,7 @@ FROM events`)
} }
dur, err := time.ParseDuration(durStr) dur, err := time.ParseDuration(durStr)
if err != nil { if err != nil {
return nil, fmt.Errorf("%w: %v", err) return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
} }
event.Duration = dur event.Duration = dur
result = append(result, event) result = append(result, event)

10
plan/storage/storage.go Normal file
View File

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