Skip to main content

garmin_cli/cli/commands/
profile.rs

1//! Profile commands for garmin-cli
2
3use crate::client::GarminClient;
4use crate::config::CredentialStore;
5use crate::error::Result;
6
7use super::auth::refresh_token;
8
9/// Show user profile
10pub async fn show(profile: Option<String>) -> Result<()> {
11    let store = CredentialStore::new(profile)?;
12    let (oauth1, oauth2) = refresh_token(&store).await?;
13
14    let client = GarminClient::new(&oauth1.domain);
15
16    let social: serde_json::Value = client
17        .get_json(&oauth2, "/userprofile-service/socialProfile")
18        .await?;
19
20    println!("User Profile");
21    println!("{}", "-".repeat(40));
22
23    if let Some(name) = social.get("displayName").and_then(|v| v.as_str()) {
24        println!("Display Name: {}", name);
25    }
26
27    if let Some(name) = social.get("fullName").and_then(|v| v.as_str()) {
28        println!("Full Name:    {}", name);
29    }
30
31    if let Some(location) = social.get("location").and_then(|v| v.as_str()) {
32        if !location.is_empty() {
33            println!("Location:     {}", location);
34        }
35    }
36
37    if let Some(id) = social.get("userProfilePK").and_then(|v| v.as_i64()) {
38        println!("Profile ID:   {}", id);
39    }
40
41    Ok(())
42}
43
44/// Show user settings
45pub async fn settings(profile: Option<String>) -> Result<()> {
46    let store = CredentialStore::new(profile)?;
47    let (oauth1, oauth2) = refresh_token(&store).await?;
48
49    let client = GarminClient::new(&oauth1.domain);
50
51    let settings: serde_json::Value = client
52        .get_json(&oauth2, "/userprofile-service/userprofile/user-settings")
53        .await?;
54
55    println!("User Settings");
56    println!("{}", "-".repeat(40));
57
58    if let Some(units) = settings.get("measurementSystem").and_then(|v| v.as_str()) {
59        println!("Units:        {}", units);
60    }
61
62    if let Some(format) = settings.get("dateFormat").and_then(|v| v.as_str()) {
63        println!("Date Format:  {}", format);
64    }
65
66    if let Some(format) = settings.get("timeFormat").and_then(|v| v.as_str()) {
67        println!("Time Format:  {}", format);
68    }
69
70    if let Some(tz) = settings.get("timezone").and_then(|v| v.as_str()) {
71        println!("Timezone:     {}", tz);
72    }
73
74    if let Some(start) = settings.get("firstDayOfWeek").and_then(|v| v.as_str()) {
75        println!("Week Starts:  {}", start);
76    }
77
78    // Physical info
79    println!();
80    println!("Physical");
81    println!("{}", "-".repeat(40));
82
83    if let Some(height) = settings.get("height").and_then(|v| v.as_f64()) {
84        println!("Height:       {:.0} cm", height);
85    }
86
87    if let Some(weight) = settings.get("weight").and_then(|v| v.as_f64()) {
88        println!("Weight:       {:.1} kg", weight / 1000.0);
89    }
90
91    if let Some(gender) = settings.get("gender").and_then(|v| v.as_str()) {
92        println!("Gender:       {}", gender);
93    }
94
95    if let Some(dob) = settings.get("birthDate").and_then(|v| v.as_str()) {
96        println!("Birth Date:   {}", dob);
97    }
98
99    Ok(())
100}