clickup_cli/commands/
auth.rs1use clap::Subcommand;
2use crate::client::ClickUpClient;
3use crate::config::Config;
4use crate::error::CliError;
5use crate::output::OutputConfig;
6use crate::Cli;
7
8#[derive(Subcommand)]
9pub enum AuthCommands {
10 Whoami,
12 Check,
14}
15
16pub async fn execute(command: AuthCommands, cli: &Cli) -> Result<(), CliError> {
17 let token = resolve_token(cli)?;
18 let client = ClickUpClient::new(&token, cli.timeout)?;
19
20 match command {
21 AuthCommands::Whoami => {
22 let resp = client.get("/v2/user").await?;
23 let output = OutputConfig::from_cli(&cli.output, &cli.fields, cli.no_header, cli.quiet);
24 if let Some(user) = resp.get("user") {
25 output.print_single(user, &["id", "username", "email"], "id");
26 }
27 Ok(())
28 }
29 AuthCommands::Check => {
30 client.get("/v2/user").await?;
32 Ok(())
33 }
34 }
35}
36
37pub fn resolve_token(cli: &Cli) -> Result<String, CliError> {
38 if let Some(token) = &cli.token {
40 return Ok(token.clone());
41 }
42 if let Ok(token) = std::env::var("CLICKUP_TOKEN") {
44 if !token.is_empty() {
45 return Ok(token);
46 }
47 }
48 let config = Config::load()?;
50 if config.auth.token.is_empty() {
51 return Err(CliError::ConfigError("Not configured".into()));
52 }
53 Ok(config.auth.token)
54}