warg_cli/commands/
logout.rs1use anyhow::{bail, Result};
2use clap::Args;
3use warg_client::{keyring::Keyring, Config, RegistryUrl};
4
5use super::CommonOptions;
6
7#[derive(Args)]
9pub struct LogoutCommand {
10 #[clap(flatten)]
12 pub common: CommonOptions,
13
14 #[clap(value_name = "URL")]
16 #[arg(hide = true)]
17 pub registry_url: Option<String>,
18}
19
20impl LogoutCommand {
21 pub async fn exec(mut self) -> Result<()> {
23 if self.registry_url.is_some() {
24 if self.common.registry.is_some() {
25 bail!("Registry URL provided in two different arguments. Use only one.");
26 }
27 self.common.registry = self.registry_url;
28 }
29 let mut config = self.common.read_config()?;
30 let registry_url = &self
31 .common
32 .registry
33 .as_deref()
34 .or(config.home_url.as_deref())
35 .map(RegistryUrl::new)
36 .transpose()?
37 .ok_or(anyhow::anyhow!(
38 "Registry is not specified, so nothing to logout."
39 ))?;
40 let keyring = Keyring::from_config(&config)?;
41 keyring.delete_auth_token(registry_url)?;
42 let registry_url_str = registry_url.to_string();
43 if config
44 .home_url
45 .as_deref()
46 .is_some_and(|home_url| home_url == registry_url_str)
47 {
48 config.keyring_auth = false;
49 config.write_to_file(&Config::default_config_path()?)?;
50 }
51 println!(
52 "Logged out of registry: {registry}",
53 registry = registry_url.registry_domain(),
54 );
55 Ok(())
56 }
57}