diff --git a/item/event.go b/item/event.go index 5faafa1..9ca7a47 100644 --- a/item/event.go +++ b/item/event.go @@ -103,6 +103,9 @@ func (e Event) Valid() bool { if e.Duration.Seconds() < 1 { return false } + if e.Recur != nil && !e.Recur.Valid() { + return false + } return true } diff --git a/plan/command/argset.go b/plan/command/argset.go index 138b80d..7e28c81 100644 --- a/plan/command/argset.go +++ b/plan/command/argset.go @@ -61,3 +61,15 @@ func (as *ArgSet) GetDuration(name string) time.Duration { } return val } + +func (as *ArgSet) GetPeriod(name string) Period { + flag, ok := as.Flags[name] + if !ok { + return Period("") + } + val, ok := flag.Get().(Period) + if !ok { + return Period("") + } + return val +} diff --git a/plan/command/command.go b/plan/command/command.go index 5e74178..56b2fcd 100644 --- a/plan/command/command.go +++ b/plan/command/command.go @@ -7,10 +7,12 @@ import ( ) const ( - FlagTitle = "title" - FlagOn = "on" - FlagAt = "at" - FlagFor = "for" + FlagTitle = "title" + FlagOn = "on" + FlagAt = "at" + FlagFor = "for" + FlagRecStart = "rec-start" + FlagRecPeriod = "rec-period" ) type Command interface { diff --git a/plan/command/flag.go b/plan/command/flag.go index 979a1a8..a85d79f 100644 --- a/plan/command/flag.go +++ b/plan/command/flag.go @@ -3,6 +3,7 @@ package command import ( "errors" "fmt" + "slices" "time" ) @@ -107,3 +108,33 @@ func (fd *FlagDuration) IsSet() bool { func (fs *FlagDuration) Get() any { return fs.Value } + +type Period string + +const ( + PeriodDay = "day" + PeriodMonth = "month" +) + +var validPeriods = []Period{PeriodDay, PeriodMonth} + +type FlagPeriod struct { + Name string + Value Period +} + +func (fp *FlagPeriod) Set(val string) error { + if !slices.Contains(validPeriods, Period(val)) { + return fmt.Errorf("not a valid period: %v", val) + } + fp.Value = Period(val) + return nil +} + +func (fp *FlagPeriod) IsSet() bool { + return fp.Value != "" +} + +func (fp *FlagPeriod) Get() any { + return fp.Value +} diff --git a/plan/command/update.go b/plan/command/update.go index 689159c..73fc2d2 100644 --- a/plan/command/update.go +++ b/plan/command/update.go @@ -6,6 +6,7 @@ import ( "strings" "time" + "go-mod.ewintr.nl/planner/item" "go-mod.ewintr.nl/planner/plan/storage" ) @@ -24,10 +25,12 @@ func NewUpdate(localIDRepo storage.LocalID, eventRepo storage.Event, syncRepo st syncRepo: syncRepo, argSet: &ArgSet{ Flags: map[string]Flag{ - FlagTitle: &FlagString{}, - FlagOn: &FlagDate{}, - FlagAt: &FlagTime{}, - FlagFor: &FlagDuration{}, + FlagTitle: &FlagString{}, + FlagOn: &FlagDate{}, + FlagAt: &FlagTime{}, + FlagFor: &FlagDuration{}, + FlagRecStart: &FlagDate{}, + FlagRecPeriod: &FlagPeriod{}, }, }, } @@ -103,6 +106,17 @@ func (update *Update) do() error { if as.IsSet(FlagFor) { e.Duration = as.GetDuration(FlagFor) } + if as.IsSet(FlagRecStart) || as.IsSet(FlagRecPeriod) { + if e.Recurrer == nil { + e.Recurrer = *item.Recur{} + } + if as.IsSet(FlagRecStart) { + e.Recurrer.Start = as.GetTime(FlagRecStart) + } + if as.IsSet(FlagRecPeriod) { + e.Recurrer.Period = as.GetPeriod(FlagRecPeriod) + } + } if !e.Valid() { return fmt.Errorf("event is unvalid")