-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlogout.rs
More file actions
89 lines (78 loc) · 2.73 KB
/
logout.rs
File metadata and controls
89 lines (78 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use crate::cli::CONFIRM_FLAG;
use crate::utils::{get_api_access_url, user_confirm, warning_message, API_KEY_PAGE, SEPARATOR};
use clap::ArgMatches;
use cloudtruth_config::Config;
use color_eyre::eyre::Result;
use indoc::printdoc;
pub fn process_logout_command(subcmd_args: &ArgMatches, config: &Config) -> Result<()> {
let confirmed = subcmd_args.is_present(CONFIRM_FLAG);
let api_url = &config.server_url;
let profile_name = &config.profile_name;
if config.api_key.is_empty() {
warning_message(format!("No API key is set for profile '{profile_name}'"));
return Ok(());
}
if !confirmed {
printdoc!(
r#"
{}
This removes the API key from profile '{}'.
However, it does NOT remove access for that API key.
"#,
SEPARATOR,
profile_name,
);
let msg = format!("Do you want to remove the API key from profile '{profile_name}'");
if !user_confirm(msg, Some(false)) {
warning_message(format!("Leaving API key in profile '{profile_name}'"));
return Ok(());
}
}
// TODO: instead of using web application, find way to delete API key...
if let Ok(api_key_url) = get_api_access_url(api_url) {
let mut open_page = true;
if !confirmed {
printdoc!(
r#"
{}
Logout does NOT remove access for that API key.
You can visit the {} page ({}) to update access.
"#,
SEPARATOR,
API_KEY_PAGE,
api_key_url,
);
if !user_confirm(format!("Open the {API_KEY_PAGE} page"), Some(false)) {
open_page = false;
}
} else {
warning_message(format!(
"Opening {API_KEY_PAGE} page ({api_key_url}) in browser"
));
}
if open_page {
let open_result = webbrowser::open(&api_key_url);
if open_result.is_err() {
printdoc!(
r#"
"Failed to open browser:
{}
You can manually open '{}' to delete an API key/token.
"#,
open_result.unwrap_err().to_string(),
api_key_url,
);
}
}
} else {
warning_message(format!("Unable to determine {API_KEY_PAGE} page URL"));
}
// NOTE: setting api_key to an empty string forces it to be removed.
Config::update_profile(profile_name, Some(""), None, None, None, None)?;
println!(
"Updated profile '{}' in {}",
profile_name,
Config::filename()
);
Ok(())
}