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
use clap::{Arg, ArgAction, ArgMatches, Command};
use config::Config;
use crud_auth::CrudAuth;
use owo_colors::OwoColorize;
#[derive(Default, Debug)]
pub struct Auth {
header: (String, String),
}
const AUTH_TOKEN_ARG: &str = "auth_token";
const AUTH_TOKEN_SETTING: &str = "auth_token";
impl CrudAuth for Auth {
fn clap_auth(&self, app: Command) -> Command {
let app = app.arg(
Arg::new(AUTH_TOKEN_ARG)
.short('t')
.long("auth-token")
.action(ArgAction::Set)
.help("Authorization token")
.help_heading("Configuration"),
);
app
}
fn clap_matches(&mut self, matches: &ArgMatches, _app: &mut Command, settings: &Config) {
if let Some(token) = matches.get_one::<String>(AUTH_TOKEN_ARG).cloned() {
self.header = ("Authorization".to_string(), "Bearer ".to_string() + &token);
} else if let Ok(token) = settings.get_string(AUTH_TOKEN_SETTING) {
self.header = ("Authorization".to_string(), "Bearer ".to_string() + &token);
}
}
fn auth_header(&self) -> (String, String) {
self.header.clone()
}
fn error_help_message(&self) -> String {
format!("Use or check the `{}` argument.", "--auth-token".yellow())
}
}