Skip to content
Open
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions src/commands/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ export function registerCalendarCommands(program: Command): void {
calendar
.command('list')
.description('List all calendars')
.action(async (options: GlobalOptions) => {
.action(async (options: GlobalOptions, command: Command) => {
try {
const profileName = getActiveProfile(options.profile);
const globalOpts = command.parent?.optsWithGlobals() as GlobalOptions;
const profileName = getActiveProfile(globalOpts?.profile);
const auth = await getAuthenticatedClient(profileName);
const client = new CalendarClient(auth);

const calendars = await client.listCalendars();
const format = options.format || 'table';
const format = globalOpts?.format || 'table';
const output = formatCalendarList(calendars, format);
print(output);
} catch (error) {
Expand All @@ -46,23 +47,26 @@ export function registerCalendarCommands(program: Command): void {
calendar
.command('events')
.description('List upcoming events')
.option('--days <number>', 'Number of days to look ahead', '7')
.option('--days <number>', 'Number of days to look ahead (or back, with --past)', '7')
.option('--calendar <id>', 'Calendar ID to query', 'primary')
.option('--limit <number>', 'Maximum number of events to return', '10')
.option('--past', 'Look back over the last N days (window end = now) instead of ahead')
.action(
async (options: GlobalOptions & { days: string; calendar: string; limit: string }) => {
async (options: GlobalOptions & { days: string; calendar: string; limit: string; past?: boolean }, command: Command) => {
try {
const profileName = getActiveProfile(options.profile);
const globalOpts = command.parent?.optsWithGlobals() as GlobalOptions;
const profileName = getActiveProfile(globalOpts?.profile);
const auth = await getAuthenticatedClient(profileName);
const client = new CalendarClient(auth);

const events = await client.listEvents({
calendarId: options.calendar,
days: parseInt(options.days, 10),
maxResults: parseInt(options.limit, 10),
direction: options.past ? 'past' : 'future',
});

const format = options.format || 'table';
const format = globalOpts?.format || 'table';
const output = formatEventList(events, format);
print(output);
} catch (error) {
Expand All @@ -84,10 +88,12 @@ export function registerCalendarCommands(program: Command): void {
.action(
async (
query: string,
options: GlobalOptions & { days: string; calendar: string }
options: GlobalOptions & { days: string; calendar: string },
command: Command
) => {
try {
const profileName = getActiveProfile(options.profile);
const globalOpts = command.parent?.optsWithGlobals() as GlobalOptions;
const profileName = getActiveProfile(globalOpts?.profile);
const auth = await getAuthenticatedClient(profileName);
const client = new CalendarClient(auth);

Expand All @@ -96,7 +102,7 @@ export function registerCalendarCommands(program: Command): void {
days: parseInt(options.days, 10),
});

const format = options.format || 'table';
const format = globalOpts?.format || 'table';
const output = formatEventList(events, format);
print(output);
} catch (error) {
Expand Down
13 changes: 10 additions & 3 deletions src/lib/calendar-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ export class CalendarClient {
calendarId?: string;
days?: number;
maxResults?: number;
direction?: 'past' | 'future';
} = {}): Promise<CalendarEvent[]> {
const calendarId = options.calendarId || 'primary';
const days = options.days || 7;
const maxResults = options.maxResults || 10;

const timeMin = new Date().toISOString();
const timeMax = new Date(Date.now() + days * 24 * 60 * 60 * 1000).toISOString();
const direction = options.direction || 'future';

const now = Date.now();
const timeMin = direction === 'past'
? new Date(now - days * 24 * 60 * 60 * 1000).toISOString()
: new Date(now).toISOString();
const timeMax = direction === 'past'
? new Date(now).toISOString()
: new Date(now + days * 24 * 60 * 60 * 1000).toISOString();

const response = await this.calendar.events.list({
calendarId,
Expand Down