Skip to main content

singularity_cli/commands/
setup.rs

1use std::io::{self, Write};
2
3use anyhow::Result;
4
5pub fn run() -> Result<()> {
6    let config = crate::config::load_config()?;
7    let path = crate::config::config_path()?;
8
9    // Step 1: Token
10    let current_token_hint = config.token.as_ref().map(|t| {
11        if t.len() > 8 {
12            format!("{}...{}", &t[..4], &t[t.len() - 4..])
13        } else {
14            "****".to_string()
15        }
16    });
17    if let Some(ref hint) = current_token_hint {
18        print!("API token [current: {}]: ", hint);
19    } else {
20        print!("API token: ");
21    }
22    io::stdout().flush()?;
23    let mut token_input = String::new();
24    io::stdin().read_line(&mut token_input)?;
25    let token_input = token_input.trim();
26
27    // Step 2: Timezone
28    let current_tz = config.timezone.as_deref().unwrap_or("UTC");
29    print!("Timezone [current: {}]: ", current_tz);
30    io::stdout().flush()?;
31    let mut tz_input = String::new();
32    io::stdin().read_line(&mut tz_input)?;
33    let tz_input = tz_input.trim();
34
35    // Apply
36    let mut new_config = config;
37
38    if !token_input.is_empty() {
39        new_config.token = Some(token_input.to_string());
40    }
41
42    if !tz_input.is_empty() {
43        tz_input
44            .parse::<chrono_tz::Tz>()
45            .map_err(|_| anyhow::anyhow!("invalid timezone: {}", tz_input))?;
46        new_config.timezone = Some(tz_input.to_string());
47    }
48
49    crate::config::save_config(&new_config)?;
50    println!("Configuration saved to {}", path.display());
51    Ok(())
52}