This commit is contained in:
Erik Winter 2024-12-05 07:28:31 +01:00
parent 45125bd02d
commit 4bb7640e84
5 changed files with 70 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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")