garmin-cli 2.0.2

CLI for Garmin Connect API - activities, health metrics, and more
Documentation
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
use clap::{Parser, Subcommand};
use garmin_cli::cli::commands;

#[derive(Parser)]
#[command(name = "garmin")]
#[command(author, version, about = "CLI for Garmin Connect API", long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,

    /// Profile to use
    #[arg(short, long, global = true, env = "GARMIN_PROFILE")]
    profile: Option<String>,
}

#[derive(Subcommand)]
enum Commands {
    /// Authentication commands
    Auth {
        #[command(subcommand)]
        command: AuthCommands,
    },
    /// Activity commands
    Activities {
        #[command(subcommand)]
        command: ActivityCommands,
    },
    /// Health metrics commands (including weight)
    Health {
        #[command(subcommand)]
        command: HealthCommands,
    },
    /// Device commands
    Devices {
        #[command(subcommand)]
        command: DeviceCommands,
    },
    /// User profile commands
    Profile {
        #[command(subcommand)]
        command: ProfileCommands,
    },
    /// Sync data to local database
    Sync {
        #[command(subcommand)]
        command: SyncCommands,
    },
}

#[derive(Subcommand)]
enum AuthCommands {
    /// Login to Garmin Connect
    Login {
        /// Email address
        #[arg(short, long, env = "GARMIN_EMAIL")]
        email: Option<String>,
    },
    /// Logout and clear credentials
    Logout,
    /// Show authentication status
    Status,
}

#[derive(Subcommand)]
enum ActivityCommands {
    /// List activities
    List {
        /// Number of activities to show
        #[arg(short, long, default_value = "20")]
        limit: u32,
        /// Starting offset
        #[arg(short, long, default_value = "0")]
        start: u32,
    },
    /// Get activity details
    Get {
        /// Activity ID
        id: u64,
    },
    /// Download activity file
    Download {
        /// Activity ID
        id: u64,
        /// File format (fit, gpx, tcx, kml)
        #[arg(short = 't', long = "type", default_value = "fit")]
        file_type: String,
        /// Output file path
        #[arg(short, long)]
        output: Option<String>,
    },
    /// Upload activity file
    Upload {
        /// File path to upload
        file: String,
    },
}

#[derive(Subcommand)]
enum HealthCommands {
    /// Get daily summary
    Summary {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
    },
    /// Get sleep data
    Sleep {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
        /// Number of days to show (overrides date)
        #[arg(long)]
        days: Option<u32>,
    },
    /// Get stress data
    Stress {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
        /// Number of days to show (overrides date)
        #[arg(long)]
        days: Option<u32>,
    },
    /// Get body battery data
    BodyBattery {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
        /// Number of days to show (overrides date)
        #[arg(long)]
        days: Option<u32>,
    },
    /// Get heart rate data
    HeartRate {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
    },
    /// Get daily step counts
    Steps {
        /// Number of days to show (default: 10)
        #[arg(long, default_value = "10")]
        days: u32,
    },
    /// Get calorie data
    Calories {
        /// Number of days to show (default: 10)
        #[arg(long, default_value = "10")]
        days: u32,
    },
    /// Get VO2 max and performance metrics
    Vo2max {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
    },
    /// Get training readiness score
    TrainingReadiness {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
        /// Number of days to show (overrides date)
        #[arg(long)]
        days: Option<u32>,
    },
    /// Get training status
    TrainingStatus {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
        /// Number of days to show (overrides date)
        #[arg(long)]
        days: Option<u32>,
    },
    /// Get HRV (heart rate variability) data
    Hrv {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
    },
    /// Get fitness age
    FitnessAge {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
    },
    /// List weight entries
    Weight {
        /// Start date (YYYY-MM-DD)
        #[arg(long)]
        from: Option<String>,
        /// End date (YYYY-MM-DD)
        #[arg(long)]
        to: Option<String>,
    },
    /// Add weight entry
    WeightAdd {
        /// Weight value
        weight: f64,
        /// Unit (kg or lbs)
        #[arg(short, long, default_value = "kg")]
        unit: String,
    },
    /// Get lactate threshold
    LactateThreshold {
        /// Number of days to show (default: 90)
        #[arg(long, default_value = "90")]
        days: u32,
    },
    /// Get race predictions
    RacePredictions {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
    },
    /// Get endurance score
    EnduranceScore {
        /// Number of days to show (default: 30)
        #[arg(long, default_value = "30")]
        days: u32,
    },
    /// Get hill score
    HillScore {
        /// Number of days to show (default: 30)
        #[arg(long, default_value = "30")]
        days: u32,
    },
    /// Get SpO2 (blood oxygen) data
    Spo2 {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
    },
    /// Get respiration data
    Respiration {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
    },
    /// Get intensity minutes
    IntensityMinutes {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
    },
    /// Get blood pressure data
    BloodPressure {
        /// Start date (YYYY-MM-DD)
        #[arg(long)]
        from: Option<String>,
        /// End date (YYYY-MM-DD)
        #[arg(long)]
        to: Option<String>,
    },
    /// Get hydration data
    Hydration {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
    },
    /// Get personal records
    PersonalRecords,
    /// Get performance summary (all performance metrics)
    PerformanceSummary {
        /// Date (YYYY-MM-DD), defaults to today
        #[arg(short, long)]
        date: Option<String>,
    },
    /// Get health insights (sleep/stress correlations)
    Insights {
        /// Number of days to analyze (default: 28)
        #[arg(long, default_value = "28")]
        days: u32,
    },
}

#[derive(Subcommand)]
enum DeviceCommands {
    /// List registered devices
    List,
    /// Get device info
    Get {
        /// Device ID
        id: String,
    },
    /// Show device history from synced activities
    History {
        /// Storage directory path
        #[arg(long = "storage", alias = "db")]
        storage: Option<String>,
    },
}

#[derive(Subcommand)]
enum ProfileCommands {
    /// Show user profile
    Show,
    /// Show user settings
    Settings,
}

#[derive(Subcommand)]
enum SyncCommands {
    /// Run sync operation
    Run {
        /// Storage directory path
        #[arg(long = "storage", alias = "db")]
        storage: Option<String>,
        /// Sync activities only
        #[arg(long)]
        activities: bool,
        /// Sync health data only
        #[arg(long)]
        health: bool,
        /// Sync performance metrics only
        #[arg(long)]
        performance: bool,
        /// Start date (YYYY-MM-DD)
        #[arg(long)]
        from: Option<String>,
        /// End date (YYYY-MM-DD)
        #[arg(long)]
        to: Option<String>,
        /// Dry run (plan only, don't execute)
        #[arg(long)]
        dry_run: bool,
        /// Run backfill to sync historical data (instead of latest)
        #[arg(long)]
        backfill: bool,
        /// Force re-sync (ignore existing data)
        #[arg(long)]
        force: bool,
    },
    /// Show sync status
    Status {
        /// Storage directory path
        #[arg(long = "storage", alias = "db")]
        storage: Option<String>,
    },
    /// Reset failed tasks to pending
    Reset {
        /// Storage directory path
        #[arg(long = "storage", alias = "db")]
        storage: Option<String>,
    },
    /// Clear all pending tasks
    Clear {
        /// Storage directory path
        #[arg(long = "storage", alias = "db")]
        storage: Option<String>,
    },
}

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

    let result = match cli.command {
        Commands::Auth { command } => match command {
            AuthCommands::Login { email } => commands::login(email, cli.profile).await,
            AuthCommands::Logout => commands::logout(cli.profile).await,
            AuthCommands::Status => commands::status(cli.profile).await,
        },
        Commands::Activities { command } => match command {
            ActivityCommands::List { limit, start } => {
                commands::list_activities(limit, start, cli.profile).await
            }
            ActivityCommands::Get { id } => commands::get_activity(id, cli.profile).await,
            ActivityCommands::Download {
                id,
                file_type,
                output,
            } => commands::download_activity(id, &file_type, output, cli.profile).await,
            ActivityCommands::Upload { file } => {
                commands::upload_activity(&file, cli.profile).await
            }
        },
        Commands::Health { command } => match command {
            HealthCommands::Summary { date } => commands::summary(date, cli.profile).await,
            HealthCommands::Sleep { date, days } => {
                if let Some(d) = days {
                    commands::sleep_range(d, cli.profile).await
                } else {
                    commands::sleep(date, cli.profile).await
                }
            }
            HealthCommands::Stress { date, days } => {
                if let Some(d) = days {
                    commands::stress_range(d, cli.profile).await
                } else {
                    commands::stress(date, cli.profile).await
                }
            }
            HealthCommands::BodyBattery { date, days } => {
                if let Some(d) = days {
                    commands::body_battery_range(d, cli.profile).await
                } else {
                    commands::body_battery(date, cli.profile).await
                }
            }
            HealthCommands::HeartRate { date } => commands::heart_rate(date, cli.profile).await,
            HealthCommands::Steps { days } => commands::steps(Some(days), cli.profile).await,
            HealthCommands::Calories { days } => commands::calories(Some(days), cli.profile).await,
            HealthCommands::Vo2max { date } => commands::vo2max(date, cli.profile).await,
            HealthCommands::TrainingReadiness { date, days } => {
                if let Some(d) = days {
                    commands::training_readiness_range(d, cli.profile).await
                } else {
                    commands::training_readiness(date, cli.profile).await
                }
            }
            HealthCommands::TrainingStatus { date, days } => {
                if let Some(d) = days {
                    commands::training_status_range(d, cli.profile).await
                } else {
                    commands::training_status(date, cli.profile).await
                }
            }
            HealthCommands::Hrv { date } => commands::hrv(date, cli.profile).await,
            HealthCommands::FitnessAge { date } => commands::fitness_age(date, cli.profile).await,
            HealthCommands::Weight { from, to } => {
                commands::list_weight(from, to, cli.profile).await
            }
            HealthCommands::WeightAdd { weight, unit } => {
                commands::add_weight(weight, &unit, cli.profile).await
            }
            HealthCommands::LactateThreshold { days } => {
                commands::lactate_threshold(Some(days), cli.profile).await
            }
            HealthCommands::RacePredictions { date } => {
                commands::race_predictions(date, cli.profile).await
            }
            HealthCommands::EnduranceScore { days } => {
                commands::endurance_score(Some(days), cli.profile).await
            }
            HealthCommands::HillScore { days } => {
                commands::hill_score(Some(days), cli.profile).await
            }
            HealthCommands::Spo2 { date } => commands::spo2(date, cli.profile).await,
            HealthCommands::Respiration { date } => commands::respiration(date, cli.profile).await,
            HealthCommands::IntensityMinutes { date } => {
                commands::intensity_minutes(date, cli.profile).await
            }
            HealthCommands::BloodPressure { from, to } => {
                commands::blood_pressure(from, to, cli.profile).await
            }
            HealthCommands::Hydration { date } => commands::hydration(date, cli.profile).await,
            HealthCommands::PersonalRecords => commands::personal_records(cli.profile).await,
            HealthCommands::PerformanceSummary { date } => {
                commands::performance_summary(date, cli.profile).await
            }
            HealthCommands::Insights { days } => commands::insights(days, cli.profile).await,
        },
        Commands::Devices { command } => match command {
            DeviceCommands::List => commands::list_devices(cli.profile).await,
            DeviceCommands::Get { id } => commands::get_device(&id, cli.profile).await,
            DeviceCommands::History { storage } => commands::device_history(storage).await,
        },
        Commands::Profile { command } => match command {
            ProfileCommands::Show => commands::show_profile(cli.profile).await,
            ProfileCommands::Settings => commands::show_settings(cli.profile).await,
        },
        Commands::Sync { command } => match command {
            SyncCommands::Run {
                storage,
                activities,
                health,
                performance,
                from,
                to,
                dry_run,
                backfill,
                force,
            } => {
                commands::sync_run(
                    cli.profile,
                    storage,
                    activities,
                    health,
                    performance,
                    from,
                    to,
                    dry_run,
                    backfill,
                    force,
                )
                .await
            }
            SyncCommands::Status { storage } => commands::sync_status(cli.profile, storage).await,
            SyncCommands::Reset { storage } => commands::sync_reset(storage).await,
            SyncCommands::Clear { storage } => commands::sync_clear(storage).await,
        },
    };

    if let Err(e) = result {
        eprintln!("Error: {}", garmin_cli::error::format_user_error(&e));
        std::process::exit(1);
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn rejects_removed_format_flag() {
        assert!(Cli::try_parse_from(["garmin", "--format", "json", "auth", "status"]).is_err());
    }

    #[test]
    fn rejects_removed_watch_command() {
        assert!(Cli::try_parse_from(["garmin", "watch"]).is_err());
    }

    #[test]
    fn rejects_removed_simple_flag() {
        assert!(Cli::try_parse_from(["garmin", "sync", "run", "--simple"]).is_err());
    }

    #[test]
    fn accepts_storage_alias_for_sync() {
        assert!(Cli::try_parse_from(["garmin", "sync", "status", "--storage", "/tmp"]).is_ok());
        assert!(Cli::try_parse_from(["garmin", "sync", "status", "--db", "/tmp"]).is_ok());
    }
}