auths_cli/commands/
whoami.rs1use anyhow::{Result, anyhow};
2use auths_id::storage::identity::IdentityStorage;
3use auths_id::storage::layout;
4use auths_storage::git::RegistryIdentityStorage;
5use clap::Parser;
6use serde::Serialize;
7
8use crate::config::CliConfig;
9use crate::ux::format::{JsonResponse, Output, is_json_mode};
10
11#[derive(Parser, Debug, Clone)]
13#[command(name = "whoami", about = "Show the current identity on this machine")]
14pub struct WhoamiCommand {}
15
16#[derive(Debug, Serialize)]
17struct WhoamiResponse {
18 identity_did: String,
19 #[serde(skip_serializing_if = "Option::is_none")]
20 label: Option<String>,
21}
22
23pub fn handle_whoami(_cmd: WhoamiCommand, repo: Option<std::path::PathBuf>) -> Result<()> {
24 let repo_path = layout::resolve_repo_path(repo).map_err(|e| anyhow!(e))?;
25
26 if crate::factories::storage::open_git_repo(&repo_path).is_err() {
27 if is_json_mode() {
28 JsonResponse::<()>::error(
29 "whoami",
30 "No identity found. Run `auths init` to get started.",
31 )
32 .print()?;
33 } else {
34 let out = Output::new();
35 out.print_error("No identity found. Run `auths init` to get started.");
36 }
37 return Ok(());
38 }
39
40 let storage = RegistryIdentityStorage::new(&repo_path);
41 match storage.load_identity() {
42 Ok(identity) => {
43 let response = WhoamiResponse {
44 identity_did: identity.controller_did.to_string(),
45 label: None,
46 };
47
48 if is_json_mode() {
49 JsonResponse::success("whoami", &response).print()?;
50 } else {
51 let out = Output::new();
52 out.println(&format!("Identity: {}", out.info(&response.identity_did)));
53 }
54 }
55 Err(_) => {
56 if is_json_mode() {
57 JsonResponse::<()>::error(
58 "whoami",
59 "No identity found. Run `auths init` to get started.",
60 )
61 .print()?;
62 } else {
63 let out = Output::new();
64 out.print_error("No identity found. Run `auths init` to get started.");
65 }
66 }
67 }
68
69 Ok(())
70}
71
72impl crate::commands::executable::ExecutableCommand for WhoamiCommand {
73 fn execute(&self, ctx: &CliConfig) -> Result<()> {
74 handle_whoami(self.clone(), ctx.repo_path.clone())
75 }
76}