Compare commits

..

No commits in common. "ea3f62cd45759055181335b687b003d2e8a8d77e" and "2a62e6c335e1a6431b280fff7b45fe9fe89ff505" have entirely different histories.

6 changed files with 152 additions and 148 deletions

View File

@ -3,59 +3,111 @@ package command
import (
"fmt"
"strings"
"time"
"github.com/google/uuid"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/storage"
)
func NewAdd(main []string, fields map[string]string) (Command, error) {
if len(main) == 0 || main[0] != "add" {
return nil, ErrWrongCommand
}
main = main[1:]
if len(main) == 0 {
return nil, fmt.Errorf("%w: title is required for add", ErrInvalidArg)
type Add struct {
localIDRepo storage.LocalID
taskRepo storage.Task
syncRepo storage.Sync
argSet *ArgSet
}
tsk := item.Task{
ID: uuid.New().String(),
TaskBody: item.TaskBody{
Title: strings.Join(main, ","),
func NewAdd(localRepo storage.LocalID, taskRepo storage.Task, syncRepo storage.Sync) Command {
return &Add{
localIDRepo: localRepo,
taskRepo: taskRepo,
syncRepo: syncRepo,
argSet: &ArgSet{
Flags: map[string]Flag{
FlagOn: &FlagDate{},
FlagAt: &FlagTime{},
FlagFor: &FlagDuration{},
FlagRec: &FlagRecurrer{},
},
},
}
}
if val, ok := fields[FieldDate]; ok {
d := item.NewDateFromString(val)
if d.IsZero() {
return nil, fmt.Errorf("%w: could not parse date", ErrInvalidArg)
func (add *Add) Execute(main []string, flags map[string]string) error {
if len(main) == 0 || main[0] != "add" {
return ErrWrongCommand
}
tsk.Date = d
as := add.argSet
if len(main) > 1 {
as.Main = strings.Join(main[1:], " ")
}
if val, ok := fields[FieldTime]; ok {
t := item.NewTimeFromString(val)
if t.IsZero() {
return nil, fmt.Errorf("%w: could not parse time", ErrInvalidArg)
for k := range as.Flags {
v, ok := flags[k]
if !ok {
continue
}
tsk.Time = t
if err := as.Set(k, v); err != nil {
return fmt.Errorf("could not set %s: %v", k, err)
}
if val, ok := fields[FieldDuration]; ok {
d, err := time.ParseDuration(val)
}
if as.Main == "" {
return fmt.Errorf("%w: title is required", ErrInvalidArg)
}
if !as.IsSet(FlagOn) {
return fmt.Errorf("%w: date is required", ErrInvalidArg)
}
if !as.IsSet(FlagAt) && as.IsSet(FlagFor) {
return fmt.Errorf("%w: can not have duration without start time", ErrInvalidArg)
}
if as.IsSet(FlagAt) && !as.IsSet(FlagFor) {
if err := as.Flags[FlagFor].Set("1h"); err != nil {
return fmt.Errorf("could not set duration to one hour")
}
}
if !as.IsSet(FlagAt) && !as.IsSet(FlagFor) {
if err := as.Flags[FlagFor].Set("24h"); err != nil {
return fmt.Errorf("could not set duration to 24 hours")
}
}
return add.do()
}
func (add *Add) do() error {
as := add.argSet
rec := as.GetRecurrer(FlagRec)
tsk := item.Task{
ID: uuid.New().String(),
Date: as.GetDate(FlagOn),
Recurrer: rec,
TaskBody: item.TaskBody{
Title: as.Main,
Time: as.GetTime(FlagAt),
Duration: as.GetDuration(FlagFor),
},
}
if rec != nil {
tsk.RecurNext = rec.First()
}
if err := add.taskRepo.Store(tsk); err != nil {
return fmt.Errorf("could not store event: %v", err)
}
localID, err := add.localIDRepo.Next()
if err != nil {
return nil, fmt.Errorf("%w: could not parse duration", ErrInvalidArg)
return fmt.Errorf("could not create next local id: %v", err)
}
tsk.Duration = d
}
if val, ok := fields[FieldRecurrer]; ok {
rec := item.NewRecurrer(val)
if rec == nil {
return nil, fmt.Errorf("%w: could not parse recurrer", ErrInvalidArg)
}
tsk.Recurrer = rec
tsk.RecurNext = tsk.Recurrer.First()
if err := add.localIDRepo.Store(tsk.ID, localID); err != nil {
return fmt.Errorf("could not store local id: %v", err)
}
return &Store{
task: tsk,
}, nil
it, err := tsk.Item()
if err != nil {
return fmt.Errorf("could not convert event to sync item: %v", err)
}
if err := add.syncRepo.Store(it); err != nil {
return fmt.Errorf("could not store sync item: %v", err)
}
return nil
}

View File

@ -33,7 +33,7 @@ func TestAdd(t *testing.T) {
name: "title missing",
main: []string{"add"},
flags: map[string]string{
command.FieldDate: aDate.String(),
command.FlagOn: aDate.String(),
},
expErr: true,
},
@ -46,7 +46,7 @@ func TestAdd(t *testing.T) {
name: "only date",
main: []string{"add", "title"},
flags: map[string]string{
command.FieldDate: aDate.String(),
command.FlagOn: aDate.String(),
},
expTask: item.Task{
ID: "title",
@ -61,9 +61,9 @@ func TestAdd(t *testing.T) {
name: "date, time and duration",
main: []string{"add", "title"},
flags: map[string]string{
command.FieldDate: aDate.String(),
command.FieldTime: aTime.String(),
command.FieldDuration: anHourStr,
command.FlagOn: aDate.String(),
command.FlagAt: aTime.String(),
command.FlagFor: anHourStr,
},
expTask: item.Task{
ID: "title",
@ -79,8 +79,8 @@ func TestAdd(t *testing.T) {
name: "date and duration",
main: []string{"add", "title"},
flags: map[string]string{
command.FieldDate: aDate.String(),
command.FieldDuration: anHourStr,
command.FlagOn: aDate.String(),
command.FlagFor: anHourStr,
},
expErr: true,
},

View File

@ -1,28 +1,21 @@
package command
import (
"errors"
"fmt"
"strings"
"go-mod.ewintr.nl/planner/plan/storage"
)
const (
FieldTitle = "title"
FieldDate = "date"
FieldTime = "time"
FieldDuration = "duration"
FieldRecurrer = "recurrer"
FlagTitle = "title"
FlagOn = "on"
FlagAt = "at"
FlagFor = "for"
FlagRec = "rec"
)
type Dependencies struct {
LocalIDRepo storage.LocalID
TaskRepo storage.Task
SyncRepo storage.Sync
}
type Command interface {
Do(deps Dependencies) error
Execute([]string, map[string]string) error
}
type CLI struct {
@ -30,21 +23,21 @@ type CLI struct {
}
func (cli *CLI) Run(args []string) error {
// main, flags, err := ParseFlags(args)
// if err != nil {
// return err
// }
// for _, c := range cli.Commands {
// err := c.Execute(main, flags)
// switch {
// case errors.Is(err, ErrWrongCommand):
// continue
// case err != nil:
// return err
// default:
// return nil
// }
// }
main, flags, err := ParseFlags(args)
if err != nil {
return err
}
for _, c := range cli.Commands {
err := c.Execute(main, flags)
switch {
case errors.Is(err, ErrWrongCommand):
continue
case err != nil:
return err
default:
return nil
}
}
return fmt.Errorf("could not find matching command")
}

View File

@ -1,50 +0,0 @@
package command
import (
"fmt"
"time"
"go-mod.ewintr.nl/planner/item"
)
type StoreParams struct {
Title string
Date item.Date
Time item.Time
Duration time.Duration
Recurrer item.Recurrer
}
type Store struct {
task item.Task
}
func NewStore(task item.Task) *Store {
return &Store{
task: task,
}
}
func (store *Store) Do(deps Dependencies) error {
if err := deps.TaskRepo.Store(store.task); err != nil {
return fmt.Errorf("could not store event: %v", err)
}
localID, err := deps.LocalIDRepo.Next()
if err != nil {
return fmt.Errorf("could not create next local id: %v", err)
}
if err := deps.LocalIDRepo.Store(store.task.ID, localID); err != nil {
return fmt.Errorf("could not store local id: %v", err)
}
it, err := store.task.Item()
if err != nil {
return fmt.Errorf("could not convert event to sync item: %v", err)
}
if err := deps.SyncRepo.Store(it); err != nil {
return fmt.Errorf("could not store sync item: %v", err)
}
return nil
}

View File

@ -4,20 +4,30 @@ import (
"fmt"
"strconv"
"strings"
"go-mod.ewintr.nl/planner/plan/storage"
)
func NewUpdate(main []string, fields map[string]string) (Command, error) {
type Update struct {
localIDRepo storage.LocalID
taskRepo storage.Task
syncRepo storage.Sync
argSet *ArgSet
localID int
}
func NewUpdate(localIDRepo storage.LocalID, taskRepo storage.Task, syncRepo storage.Sync) Command {
return &Update{
localIDRepo: localIDRepo,
taskRepo: taskRepo,
syncRepo: syncRepo,
argSet: &ArgSet{
Flags: map[string]Flag{
FieldTitle: &FlagString{},
FieldDate: &FlagDate{},
FieldTime: &FlagTime{},
FieldDuration: &FlagDuration{},
FieldRecurrer: &FlagRecurrer{},
FlagTitle: &FlagString{},
FlagOn: &FlagDate{},
FlagAt: &FlagTime{},
FlagFor: &FlagDuration{},
FlagRec: &FlagRecurrer{},
},
},
}
@ -74,17 +84,17 @@ func (update *Update) do() error {
if as.Main != "" {
tsk.Title = as.Main
}
if as.IsSet(FieldDate) {
tsk.Date = as.GetDate(FieldDate)
if as.IsSet(FlagOn) {
tsk.Date = as.GetDate(FlagOn)
}
if as.IsSet(FieldTime) {
tsk.Time = as.GetTime(FieldTime)
if as.IsSet(FlagAt) {
tsk.Time = as.GetTime(FlagAt)
}
if as.IsSet(FieldDuration) {
tsk.Duration = as.GetDuration(FieldDuration)
if as.IsSet(FlagFor) {
tsk.Duration = as.GetDuration(FlagFor)
}
if as.IsSet(FieldRecurrer) {
tsk.Recurrer = as.GetRecurrer(FieldRecurrer)
if as.IsSet(FlagRec) {
tsk.Recurrer = as.GetRecurrer(FlagRec)
}
if !tsk.Valid() {

View File

@ -1 +0,0 @@
package service