feat(calendar): add subscribe command (#327) (thanks @cdthompson)
Some checks failed
ci / test (push) Has been cancelled
ci / worker (push) Has been cancelled
ci / windows (push) Has been cancelled
ci / darwin-cgo-build (push) Has been cancelled

This commit is contained in:
Peter Steinberger 2026-03-07 16:54:48 +00:00
parent 2f1b1bfa61
commit 0ad479ce67
5 changed files with 94 additions and 2 deletions

View File

@ -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 accounts 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.

View File

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

View File

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

View File

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

View File

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