Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,53 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
```bash
go build -o gsuite . # Build binary
./gsuite --help # Run locally
go test ./... -race # Run all tests
```

Version is injected at build time via ldflags targeting `cmd.Version`. GoReleaser handles this for releases; for local dev builds, version will show "dev".

No tests exist yet. No linter is configured.

## Architecture

This is a Go CLI tool using Cobra for Gmail operations via Google service account with domain-wide delegation.
This is a Go CLI tool using Cobra for Google Workspace operations (Gmail and Calendar) via OAuth2 PKCE authentication.

**Entry point:** `main.go` → `cmd.Execute()`

**Three packages:**
- `main` — just calls `cmd.Execute()`
- `cmd` — all Cobra commands and CLI logic
- `internal/auth` — service account authentication and Gmail service creation
- `internal/auth` — OAuth2 PKCE authentication, Gmail and Calendar service creation

### Command Structure

Root command defines persistent flags (`--credentials-file`, `--user`, `--format`, `--verbose`) and exposes getter functions (`GetCredentialsFile()`, `GetUserEmail()`, `GetOutputFormat()`, `GetVerbose()`) used by all subcommands.
Root command defines persistent flags (`--account`, `--format`, `--verbose`) and exposes getter functions (`GetAccountEmail()`, `GetOutputFormat()`, `GetVerbose()`) used by all subcommands.

Subcommands: `messages`, `threads`, `labels`, `drafts`, `send`, `search`, `calendar`, `whoami`, `version`, `login`, `logout`, `accounts`. Parent commands like `messages` and `calendar` have nested subcommands.

Subcommands: `messages`, `threads`, `labels`, `drafts`, `send`, `search`, `whoami`, `version`. Parent commands like `messages` have nested subcommands (`list`, `get`, `modify`, `get-attachment`).
Calendar commands: `list`, `get`, `create`, `update`, `delete`, `respond`, `today`, `week`, `calendars`.

### Command Pattern

Every command function follows this flow:
1. Get global flags via getter functions from `root.go`
2. Build `auth.Config` struct
3. Call `auth.NewGmailService()` to get authenticated client
4. Execute Gmail API calls
5. Output as text (default) or JSON based on `--format` flag
2. Call `auth.NewGmailService()` or `auth.NewCalendarService()` to get authenticated client
3. Execute API calls
4. Output as text (default) or JSON based on `--format` flag

### Authentication Flow

`internal/auth` handles credential loading with this priority:
1. `--credentials-file` flag
2. `GOOGLE_CREDENTIALS` env var (raw JSON)
3. `GOOGLE_APPLICATION_CREDENTIALS` env var (file path)
1. `GOOGLE_CREDENTIALS` env var (raw JSON)
2. `GOOGLE_APPLICATION_CREDENTIALS` env var (file path)

Uses JWT config with `gmail.GmailModifyScope`, sets `Subject` to the `--user` email for domain-wide delegation. All API calls use "me" which resolves to the impersonated user.
Uses OAuth2 PKCE flow with scopes: `gmail.GmailModifyScope`, `calendar.CalendarEventsScope`, `calendar.CalendarReadonlyScope`. Shared auth logic is in `newAuthenticatedClient()` which both `NewGmailService()` and `NewCalendarService()` use.

### Output Formatting

All commands support `--format text` (default) and `--format json`. JSON output uses inline struct types with `json` tags defined within each command's run function. The shared `outputJSON()` helper in `root.go` handles marshaling.
All commands support `--format text` (default) and `--format json`. JSON output uses inline struct types with `json` tags defined within each command's run function (never `omitempty`). The shared `outputJSON()` helper in `root.go` handles marshaling.

### Calendar Date/Time Parsing

`cmd/calendar_time.go` provides flexible date/time parsing: RFC3339, date-only, date+time, time-only, relative (today/tomorrow/+Nd/weekday names). All functions accept `now time.Time` and `loc *time.Location` for testability — never call `time.Now()` directly.

### Email Encoding

Expand Down
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# gsuite - Gmail CLI Tool
# gsuite - Google Workspace CLI Tool

A command-line interface for Gmail mailbox management. Authenticate with your Gmail account via OAuth2 and manage messages, threads, labels, and drafts from the terminal.
A command-line interface for Google Workspace management. Authenticate with your account via OAuth2 and manage Gmail messages, threads, labels, drafts, and Google Calendar events from the terminal.

## Installation

Expand All @@ -22,7 +22,7 @@ go build -o gsuite .

## Prerequisites

1. **Google Cloud Project** with Gmail API enabled
1. **Google Cloud Project** with Gmail API and Calendar API enabled
2. **OAuth2 Client Credentials** (Desktop or Web application type)
3. Set credentials via environment variable:
- `GOOGLE_CREDENTIALS` — raw JSON content
Expand All @@ -45,6 +45,12 @@ gsuite send --to "user@example.com" --subject "Hello" --body "Message content"

# Search messages
gsuite search "from:user@example.com is:unread"

# View today's calendar events
gsuite calendar today

# Create a meeting
gsuite calendar create --summary "Team Standup" --start "2026-03-15 09:00" --duration 30m
```

## Multi-Account Support
Expand Down Expand Up @@ -104,6 +110,15 @@ The `--account` flag (or `GSUITE_ACCOUNT` env var) can be passed to any command
| `drafts delete <id>` | Delete a draft |
| `send` | Send an email (supports markdown, attachments) |
| `search <query>` | Search messages using Gmail query syntax |
| `calendar list` | List upcoming calendar events |
| `calendar get <id>` | Get event details including attendees |
| `calendar create` | Create a calendar event |
| `calendar update <id>` | Update an existing event |
| `calendar delete <id>` | Delete a calendar event |
| `calendar respond <id>` | RSVP to an event invitation |
| `calendar today` | Show today's events |
| `calendar week` | Show this week's events (Mon-Sun) |
| `calendar calendars` | List available calendars |
| `version` | Show version information |
| `install-skill` | Install the Claude Code skill for Gmail management |

Expand Down Expand Up @@ -150,6 +165,20 @@ gsuite labels create -n "My Label"
# JSON output for scripting
gsuite messages list -f json
gsuite search "is:unread" -f json

# Calendar: list upcoming events
gsuite calendar list --after today --before +7d

# Calendar: create a recurring meeting with attendees
gsuite calendar create --summary "Weekly 1:1" --start "2026-03-15 10:00" \
--duration 30m --rrule "FREQ=WEEKLY;BYDAY=MO" \
--attendees "alice@example.com" --send-updates all

# Calendar: RSVP to an event
gsuite calendar respond abc123def456 --status accepted

# Calendar: delete a recurring event (all instances)
gsuite calendar delete abc123def456 --recurring-scope all --yes
```

## License
Expand Down
Loading
Loading