diff --git a/CHANGELOG.md b/CHANGELOG.md index 30be73c..7591dc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Sheets: add `sheets insert` to insert rows/columns into a sheet. (#203) — thanks @andybergon. - Sheets: add `sheets links` (alias `hyperlinks`) to list cell links from ranges, including rich-text links. (#374) — thanks @omothm. - Sheets: add `sheets create --parent` to place new spreadsheets in a Drive folder. (#424) — thanks @ManManavadaria. +- Calendar: add `calendar subscribe` (aliases `sub`, `add-calendar`) to add a shared calendar to the current account’s calendar list. (#327) — thanks @cdthompson. - Gmail: add `watch serve --history-types` filtering (`messageAdded|messageDeleted|labelAdded|labelRemoved`) and include `deletedMessageIds` in webhook payloads. (#168) — thanks @salmonumbrella. - Contacts: support `--org`, `--title`, `--url`, `--note`, and `--custom` on create/update; include custom fields in get output with deterministic ordering. (#199) — thanks @phuctm97. - Drive: add `drive ls --all` (alias `--global`) to list across all accessible files; make `--all` and `--parent` mutually exclusive. (#107) — thanks @struong. diff --git a/internal/cmd/calendar.go b/internal/cmd/calendar.go index d15e810..e20c7b7 100644 --- a/internal/cmd/calendar.go +++ b/internal/cmd/calendar.go @@ -125,6 +125,10 @@ func (c *CalendarSubscribeCmd) Run(ctx context.Context, flags *RootFlags) error if calendarID == "" { return usage("calendarId required") } + colorID, err := validateCalendarColorId(c.ColorID) + if err != nil { + return err + } svc, err := newCalendarService(ctx, account) if err != nil { @@ -136,8 +140,8 @@ func (c *CalendarSubscribeCmd) Run(ctx context.Context, flags *RootFlags) error Hidden: c.Hidden, Selected: c.Selected, } - if c.ColorID != "" { - entry.ColorId = c.ColorID + if colorID != "" { + entry.ColorId = colorID } added, err := svc.CalendarList.Insert(entry).Do() diff --git a/internal/cmd/calendar_validate.go b/internal/cmd/calendar_validate.go index 6fa9811..b7cd456 100644 --- a/internal/cmd/calendar_validate.go +++ b/internal/cmd/calendar_validate.go @@ -27,6 +27,21 @@ func validateColorId(s string) (string, error) { return s, nil } +func validateCalendarColorId(s string) (string, error) { + s = strings.TrimSpace(s) + if s == "" { + return "", nil + } + id, err := strconv.Atoi(s) + if err != nil { + return "", fmt.Errorf("invalid calendar color ID: %q (must be 1-24)", s) + } + if id < 1 || id > 24 { + return "", fmt.Errorf("calendar color ID must be 1-24 (got %d)", id) + } + return s, nil +} + func validateVisibility(s string) (string, error) { s = strings.TrimSpace(strings.ToLower(s)) if s == "" { diff --git a/internal/cmd/calendar_validate_test.go b/internal/cmd/calendar_validate_test.go index e3faa7f..1e510ac 100644 --- a/internal/cmd/calendar_validate_test.go +++ b/internal/cmd/calendar_validate_test.go @@ -17,6 +17,21 @@ func TestValidateColorID(t *testing.T) { } } +func TestValidateCalendarColorID(t *testing.T) { + if got, err := validateCalendarColorId(""); err != nil || got != "" { + t.Fatalf("expected empty ok, got %q %v", got, err) + } + if got, err := validateCalendarColorId("24"); err != nil || got != "24" { + t.Fatalf("expected valid id, got %q %v", got, err) + } + if _, err := validateCalendarColorId("25"); err == nil { + t.Fatalf("expected error for out of range") + } + if _, err := validateCalendarColorId("nope"); err == nil { + t.Fatalf("expected error for non-numeric") + } +} + func TestValidateVisibilityMore(t *testing.T) { if got, err := validateVisibility(""); err != nil || got != "" { t.Fatalf("expected empty ok, got %q %v", got, err) diff --git a/internal/cmd/execute_calendar_test.go b/internal/cmd/execute_calendar_test.go index ac7f24f..59e7806 100644 --- a/internal/cmd/execute_calendar_test.go +++ b/internal/cmd/execute_calendar_test.go @@ -124,6 +124,63 @@ func TestExecute_CalendarSubscribe_JSON(t *testing.T) { } } +func TestExecute_CalendarSubscribe_Flags(t *testing.T) { + origNew := newCalendarService + t.Cleanup(func() { newCalendarService = origNew }) + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !(strings.Contains(r.URL.Path, "calendarList") && r.Method == http.MethodPost) { + http.NotFound(w, r) + return + } + + var req struct { + ID string `json:"id"` + ColorID string `json:"colorId"` + Hidden bool `json:"hidden"` + Selected bool `json:"selected"` + } + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + if req.ID != "team@example.com" { + t.Fatalf("unexpected calendar id: %q", req.ID) + } + if req.ColorID != "24" { + t.Fatalf("unexpected color id: %q", req.ColorID) + } + if !req.Hidden { + t.Fatalf("expected hidden=true") + } + if req.Selected { + t.Fatalf("expected selected=false") + } + + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{ + "id": req.ID, + "summary": "Team Calendar", + "accessRole": "reader", + }) + })) + defer srv.Close() + + svc, err := calendar.NewService(context.Background(), + option.WithoutAuthentication(), + option.WithHTTPClient(srv.Client()), + option.WithEndpoint(srv.URL+"/"), + ) + if err != nil { + t.Fatalf("NewService: %v", err) + } + newCalendarService = func(context.Context, string) (*calendar.Service, error) { return svc, nil } + + if err := Execute([]string{"--account", "a@b.com", "calendar", "subscribe", "--color-id", "24", "--hidden", "--no-selected", "team@example.com"}); err != nil { + t.Fatalf("Execute: %v", err) + } +} + func TestExecute_CalendarSubscribe_Text(t *testing.T) { origNew := newCalendarService t.Cleanup(func() { newCalendarService = origNew })