caldir-cli 0.7.0

CLI for interacting with your local caldir directory and syncing with external calendar providers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
mod commands;
mod render;
mod utils;

use anyhow::Result;
use caldir_core::caldir::Caldir;
use caldir_core::calendar::Calendar;
use caldir_core::date_range::DateRange;
use caldir_core::remote::provider::Provider;
use chrono::{Datelike, Local, Utc};
use clap::{Parser, Subcommand};
use utils::date::start_of_today;

#[derive(Parser)]
#[command(name = "caldir-cli")]
#[command(version)]
#[command(about = "Interact with your caldir events and sync to remote calendars")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    #[command(about = "Connect to a remote calendar provider (e.g., Google Calendar)")]
    Connect {
        /// Provider name (e.g. "google", "caldav", "icloud", "outlook")
        provider: Option<String>,

        /// Use hosted OAuth via caldir.org (default: true). Pass --hosted=false to use your own credentials.
        #[arg(long, default_value_t = true)]
        hosted: bool,
    },
    #[command(about = "Check if any events have changed (local and remote)")]
    Status {
        /// Only operate on this calendar (by slug)
        #[arg(short, long)]
        calendar: Option<String>,

        /// Show events from this date (YYYY-MM-DD, or "start" for all past events)
        #[arg(long)]
        from: Option<String>,

        /// Show events until this date (YYYY-MM-DD)
        #[arg(long)]
        to: Option<String>,

        /// Show all events (instead of compact view when >5 events)
        #[arg(short, long)]
        verbose: bool,
    },
    #[command(about = "Pull changes from remote calendars into local caldir")]
    Pull {
        /// Only operate on this calendar (by slug)
        #[arg(short, long)]
        calendar: Option<String>,

        /// Pull events from this date (YYYY-MM-DD, or "start" for all past events)
        #[arg(long)]
        from: Option<String>,

        /// Pull events until this date (YYYY-MM-DD)
        #[arg(long)]
        to: Option<String>,

        /// Show all events (instead of compact view when >5 events)
        #[arg(short, long)]
        verbose: bool,
    },
    #[command(about = "Push changes from local caldir to remote calendars")]
    Push {
        /// Only operate on this calendar (by slug)
        #[arg(short, long)]
        calendar: Option<String>,

        /// Show all events (instead of compact view when >5 events)
        #[arg(short, long)]
        verbose: bool,
    },
    #[command(about = "Sync changes between caldir and remote calendars (push + pull)")]
    Sync {
        /// Only operate on this calendar (by slug)
        #[arg(short, long)]
        calendar: Option<String>,

        /// Sync events from this date (YYYY-MM-DD, or "start" for all past events)
        #[arg(long)]
        from: Option<String>,

        /// Sync events until this date (YYYY-MM-DD)
        #[arg(long)]
        to: Option<String>,

        /// Show all events (instead of compact view when >5 events)
        #[arg(short, long)]
        verbose: bool,
    },
    #[command(about = "List upcoming events across all calendars")]
    Events {
        /// Only show events from this calendar (by slug)
        #[arg(short, long)]
        calendar: Option<String>,

        /// Show events from this date (YYYY-MM-DD)
        #[arg(long)]
        from: Option<String>,

        /// Show events until this date (YYYY-MM-DD)
        #[arg(long)]
        to: Option<String>,
    },
    #[command(about = "Show today's events")]
    Today {
        /// Only show events from this calendar (by slug)
        #[arg(short, long)]
        calendar: Option<String>,
    },
    #[command(about = "Show this week's events (through Sunday)")]
    Week {
        /// Only show events from this calendar (by slug)
        #[arg(short, long)]
        calendar: Option<String>,
    },
    #[command(about = "Create a new event in caldir")]
    New {
        /// Event title
        title: Option<String>,

        /// Start date/time (natural language, e.g. "tomorrow 6pm")
        #[arg(short, long)]
        start: Option<String>,

        /// End date/time (natural language)
        #[arg(short, long)]
        end: Option<String>,

        /// Duration (e.g. "30m", "2 hours")
        #[arg(short, long)]
        duration: Option<String>,

        /// Location
        #[arg(short, long)]
        location: Option<String>,

        /// Calendar slug (defaults to default_calendar from config)
        #[arg(short = 'C', long)]
        calendar: Option<String>,

        /// Reminder(s) before the event (e.g. "10m", "1h", "2 days"). Can be repeated.
        #[arg(short, long, conflicts_with = "no_reminders")]
        reminder: Vec<String>,

        /// Do not add any reminders (overrides default_reminders config)
        #[arg(long)]
        no_reminders: bool,
    },
    #[command(about = "Discard unpushed local changes (restore to remote state)")]
    Discard {
        /// Only operate on this calendar (by slug)
        #[arg(short, long)]
        calendar: Option<String>,

        /// Show all events (instead of compact view when >5 events)
        #[arg(short, long)]
        verbose: bool,

        /// Skip confirmation prompt
        #[arg(long)]
        force: bool,
    },
    #[command(about = "List pending invites across calendars")]
    Invites {
        /// Only show invites from this calendar (by slug)
        #[arg(short, long)]
        calendar: Option<String>,

        /// Include already-responded invites (not just pending)
        #[arg(short, long)]
        all: bool,
    },
    #[command(about = "Respond to a calendar invites")]
    Rsvp {
        /// Path to the .ics file (omit for interactive mode)
        path: Option<String>,

        /// Response: accept, decline, maybe
        response: Option<String>,
    },
    #[command(about = "Show configuration paths and calendar info")]
    Config,
    #[command(about = "Update caldir and installed providers to the latest version")]
    Update,
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    match cli.command {
        Commands::Connect { provider, hosted } => {
            let provider = validate_provider(provider)?;
            commands::connect::run(&provider, hosted).await
        }
        Commands::Status {
            calendar,
            from,
            to,
            verbose,
        } => {
            require_calendars()?;
            let calendars = resolve_calendars(calendar.as_deref())?;
            let range = DateRange::from_args(from.as_deref(), to.as_deref())
                .map_err(|e| anyhow::anyhow!(e))?;
            commands::status::run(calendars, range, verbose).await
        }
        Commands::Pull {
            calendar,
            from,
            to,
            verbose,
        } => {
            require_calendars()?;
            let calendars = resolve_calendars(calendar.as_deref())?;
            let range = DateRange::from_args(from.as_deref(), to.as_deref())
                .map_err(|e| anyhow::anyhow!(e))?;
            commands::pull::run(calendars, range, verbose).await
        }
        Commands::Push { calendar, verbose } => {
            require_calendars()?;
            let calendars = resolve_calendars(calendar.as_deref())?;
            commands::push::run(calendars, verbose).await
        }
        Commands::Sync {
            calendar,
            from,
            to,
            verbose,
        } => {
            require_calendars()?;
            let calendars = resolve_calendars(calendar.as_deref())?;
            let range = DateRange::from_args(from.as_deref(), to.as_deref())
                .map_err(|e| anyhow::anyhow!(e))?;
            commands::sync::run(calendars, range, verbose).await
        }
        Commands::Events { calendar, from, to } => {
            require_calendars()?;
            let calendars = resolve_calendars(calendar.as_deref())?;
            use caldir_core::date_range::{parse_date_end, parse_date_start};
            // Only parse dates if explicitly provided; events command has its own defaults
            let from_dt = from
                .as_deref()
                .map(parse_date_start)
                .transpose()
                .map_err(|e| anyhow::anyhow!(e))?;
            let to_dt = to
                .as_deref()
                .map(parse_date_end)
                .transpose()
                .map_err(|e| anyhow::anyhow!(e))?;
            commands::events::run(calendars, from_dt, to_dt)
        }
        Commands::Today { calendar } => {
            require_calendars()?;
            let calendars = resolve_calendars(calendar.as_deref())?;
            let today = Local::now().date_naive();
            let end_of_today = today
                .and_hms_opt(23, 59, 59)
                .unwrap()
                .and_local_timezone(Local)
                .unwrap()
                .with_timezone(&Utc);
            commands::events::run(calendars, Some(start_of_today()), Some(end_of_today))
        }
        Commands::Week { calendar } => {
            require_calendars()?;
            let calendars = resolve_calendars(calendar.as_deref())?;
            let today = Local::now().date_naive();
            // num_days_from_monday(): Mon=0, Tue=1, ..., Sun=6
            let days_until_sunday = (6 - today.weekday().num_days_from_monday()) % 7;
            // If today is Sunday, show through next Sunday
            let days_until_sunday = if days_until_sunday == 0 {
                7
            } else {
                days_until_sunday
            };
            let end_of_sunday = (today + chrono::Duration::days(days_until_sunday as i64))
                .and_hms_opt(23, 59, 59)
                .unwrap()
                .and_local_timezone(Local)
                .unwrap()
                .with_timezone(&Utc);
            commands::events::run(calendars, Some(start_of_today()), Some(end_of_sunday))
        }
        Commands::New {
            title,
            start,
            end,
            duration,
            location,
            calendar,
            reminder,
            no_reminders,
        } => {
            require_calendars()?;
            let calendars = resolve_calendars(None)?;
            commands::new::run(
                title,
                start,
                end,
                duration,
                location,
                calendar,
                reminder,
                no_reminders,
                calendars,
            )
        }
        Commands::Discard {
            calendar,
            verbose,
            force,
        } => {
            require_calendars()?;
            let calendars = resolve_calendars(calendar.as_deref())?;
            commands::discard::run(calendars, verbose, force).await
        }
        Commands::Invites { calendar, all } => {
            require_calendars()?;
            let calendars = resolve_calendars(calendar.as_deref())?;
            commands::invites::run(calendars, all)
        }
        Commands::Rsvp { path, response } => {
            require_calendars()?;
            commands::rsvp::run(path, response)
        }
        Commands::Config => commands::config::run(),
        Commands::Update => commands::update::run().await,
    }
}

fn validate_provider(provider: Option<String>) -> Result<String> {
    let name = match provider {
        Some(name) => name,
        None => {
            let installed = Provider::discover_installed();
            if installed.is_empty() {
                anyhow::bail!(
                    "Missing provider argument.\n\n\
                    Usage: caldir connect <provider>\n\n\
                    No providers detected. Install one with:\n  \
                    cargo install caldir-provider-google"
                );
            } else {
                anyhow::bail!(
                    "Missing provider argument.\n\n\
                    Usage: caldir connect <provider>\n\n\
                    Detected providers: {}",
                    installed
                        .iter()
                        .map(|p| format!("\"{}\"", p))
                        .collect::<Vec<_>>()
                        .join(", ")
                );
            }
        }
    };

    // Check the provider binary exists
    let installed = Provider::discover_installed();
    if !installed.contains(&name) {
        if installed.is_empty() {
            anyhow::bail!(
                "Unknown provider \"{name}\".\n\n\
                No providers detected. Install one with:\n  \
                cargo install caldir-provider-{name}"
            );
        } else {
            anyhow::bail!(
                "Unknown provider \"{name}\".\n\n\
                Detected providers: {}",
                installed
                    .iter()
                    .map(|p| format!("\"{}\"", p))
                    .collect::<Vec<_>>()
                    .join(", ")
            );
        }
    }

    Ok(name)
}

fn require_calendars() -> Result<()> {
    let caldir = Caldir::load()?;

    if caldir.calendars().is_empty() {
        anyhow::bail!(
            "No calendars found.\n\n\
            Connect your first calendar with:\n  \
            caldir connect <provider>\n\n\
            Example:\n  \
            caldir connect google"
        );
    }

    Ok(())
}

fn resolve_calendars(calendar_filter: Option<&str>) -> Result<Vec<Calendar>> {
    let caldir = Caldir::load()?;
    let all_calendars = caldir.calendars();

    match calendar_filter {
        Some(slug) => match all_calendars.into_iter().find(|c| c.slug == slug) {
            Some(cal) => Ok(vec![cal]),
            None => {
                let available: Vec<_> = caldir.calendars().iter().map(|c| c.slug.clone()).collect();
                anyhow::bail!(
                    "Calendar '{}' not found. Available: {}",
                    slug,
                    available.join(", ")
                );
            }
        },
        None => Ok(all_calendars),
    }
}