diff --git a/plan/command/add.go b/plan/command/add.go index 7274159..e8e9875 100644 --- a/plan/command/add.go +++ b/plan/command/add.go @@ -24,9 +24,11 @@ func NewAdd(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage syncRepo: syncRepo, argSet: &ArgSet{ Flags: map[string]Flag{ - FlagOn: &FlagDate{}, - FlagAt: &FlagTime{}, - FlagFor: &FlagDuration{}, + FlagOn: &FlagDate{}, + FlagAt: &FlagTime{}, + FlagFor: &FlagDuration{}, + FlagRecStart: &FlagDate{}, + FlagRecPeriod: &FlagPeriod{}, }, }, } @@ -68,6 +70,9 @@ func (add *Add) Execute(main []string, flags map[string]string) error { return fmt.Errorf("could not set duration to 24 hours") } } + if as.IsSet(FlagRecStart) != as.IsSet(FlagRecPeriod) { + return fmt.Errorf("rec-start required rec-period and vice versa") + } return add.do() } @@ -93,6 +98,13 @@ func (add *Add) do() error { if as.IsSet(FlagFor) { e.Duration = as.GetDuration(FlagFor) } + if as.IsSet(FlagRecStart) { + e.Recurrer = &item.Recur{ + Start: as.GetTime(FlagRecStart), + Period: as.GetRecurPeriod(FlagRecPeriod), + } + } + if err := add.eventRepo.Store(e); err != nil { return fmt.Errorf("could not store event: %v", err) } diff --git a/plan/command/add_test.go b/plan/command/add_test.go index 924821f..d82ef81 100644 --- a/plan/command/add_test.go +++ b/plan/command/add_test.go @@ -102,6 +102,46 @@ func TestAdd(t *testing.T) { }, expErr: true, }, + { + name: "rec-start without rec-period", + main: []string{"add", "title"}, + flags: map[string]string{ + command.FlagOn: aDateStr, + command.FlagRecStart: "2024-12-08", + }, + expErr: true, + }, + { + name: "rec-period without rec-start", + main: []string{"add", "title"}, + flags: map[string]string{ + command.FlagOn: aDateStr, + command.FlagRecPeriod: "day", + }, + expErr: true, + }, + { + name: "rec-start with rec-period", + main: []string{"add", "title"}, + flags: map[string]string{ + command.FlagOn: aDateStr, + command.FlagRecStart: "2024-12-08", + command.FlagRecPeriod: "day", + }, + expEvent: item.Event{ + ID: "title", + Recurrer: &item.Recur{ + Start: time.Date(2024, 12, 8, 0, 0, 0, 0, time.UTC), + Period: item.PeriodDay, + }, + RecurNext: time.Time{}, + EventBody: item.EventBody{ + Title: "title", + Start: aDate, + Duration: aDay, + }, + }, + }, } { t.Run(tc.name, func(t *testing.T) { eventRepo := memory.NewEvent() @@ -140,7 +180,7 @@ func TestAdd(t *testing.T) { } tc.expEvent.ID = actEvents[0].ID if diff := cmp.Diff(tc.expEvent, actEvents[0]); diff != "" { - t.Errorf("(exp +, got -)\n%s", diff) + t.Errorf("(exp -, got +)\n%s", diff) } updated, err := syncRepo.FindAll()