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
use base64::{
alphabet::URL_SAFE,
engine::{general_purpose::NO_PAD, Engine, GeneralPurpose},
};
use seaplane::api::locks::v1::LockId;
use crate::{cli::cmds::locks::SeaplaneLocksCommonArgMatches, error::Result, ops::locks::LockName};
#[derive(Debug, Default, Clone)]
pub struct LocksCtx {
pub lock_name: Option<LockName>,
pub ttl: Option<u32>,
pub client_id: Option<String>,
pub lock_id: Option<LockId>,
pub base64: bool,
pub decode: bool,
pub no_header: bool,
}
impl LocksCtx {
pub fn from_locks_common(matches: &SeaplaneLocksCommonArgMatches) -> Result<LocksCtx> {
let matches = matches.0;
let base64 = matches.get_flag("base64");
let raw_lock_name = matches.get_one::<String>("lock_name");
let lock_name: Option<LockName> = if base64 {
let res: Option<Result<LockName>> = raw_lock_name.map(|name| {
let engine = GeneralPurpose::new(&URL_SAFE, NO_PAD);
let _unused = engine.decode(name)?;
Ok::<LockName, _>(LockName::new(name))
});
res.transpose()?
} else {
raw_lock_name.map(LockName::from_name_unencoded)
};
Ok(LocksCtx {
lock_name,
base64: true, ..LocksCtx::default()
})
}
}