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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use std::{
fs::File,
io::{self, Read},
};
use seaplane::api::{metadata::v1::Key, shared::v1::Directory};
use crate::{
cli::cmds::metadata::{SeaplaneMetadataCommonArgMatches, SeaplaneMetadataSetArgMatches},
error::{CliError, Context, Result},
ops::metadata::{KeyValue, KeyValues},
printer::Color,
};
#[derive(Debug, Default, Clone)]
pub struct MetadataCtx {
pub kvs: KeyValues,
pub directory: Option<Directory>,
pub base64: bool,
pub decode: bool,
pub decode_safe: bool,
pub no_decode: bool,
pub from: Option<Key>,
pub no_header: bool,
pub no_keys: bool,
pub no_values: bool,
pub keys_width_limit: usize,
pub values_width_limit: usize,
}
impl MetadataCtx {
pub fn from_md_common(matches: &SeaplaneMetadataCommonArgMatches) -> Result<MetadataCtx> {
let matches = matches.0;
let base64 = matches.get_flag("base64");
let raw_keys: Vec<_> = matches.get_many::<String>("key").unwrap().collect();
let mut kvs = KeyValues::default();
for key in raw_keys {
if base64 {
let engine = ::base64::engine::fast_portable::FastPortable::from(
&::base64::alphabet::URL_SAFE,
::base64::engine::fast_portable::NO_PAD,
);
let _ = base64::decode_engine(key, &engine)?;
kvs.push(KeyValue::from_key(key));
} else {
kvs.push(KeyValue::from_key_unencoded(key));
}
}
Ok(MetadataCtx {
kvs,
base64: true, ..MetadataCtx::default()
})
}
pub fn from_md_set(matches: &SeaplaneMetadataSetArgMatches) -> Result<MetadataCtx> {
let matches = matches.0;
let base64 = matches.get_flag("base64");
let raw_key = matches.get_one::<String>("key").unwrap();
let raw_value = matches.get_one::<String>("value").unwrap();
let value = if let Some(val) = raw_value.strip_prefix('@') {
if val == "-" {
let mut buf: Vec<u8> = Vec::new();
let stdin = io::stdin();
let mut stdin_lock = stdin.lock();
stdin_lock.read_to_end(&mut buf)?;
buf
} else {
let mut f = File::open(val)
.map_err(CliError::from)
.context("\n\tpath: ")
.with_color_context(|| (Color::Yellow, val))?;
let mut buf = Vec::new();
f.read_to_end(&mut buf)?;
buf
}
} else {
raw_value.as_bytes().to_vec()
};
let engine = ::base64::engine::fast_portable::FastPortable::from(
&::base64::alphabet::URL_SAFE,
::base64::engine::fast_portable::NO_PAD,
);
let kv = if base64 {
let _ = base64::decode_engine(raw_key, &engine)?;
let _ = base64::decode_engine(&value, &engine)?;
KeyValue::new(raw_key, &String::from_utf8(value)?)
} else {
KeyValue::new(
base64::encode_engine(raw_key, &engine),
base64::encode_engine(value, &engine),
)
};
let mut kvs = KeyValues::default();
kvs.push(kv);
Ok(MetadataCtx { kvs, base64: true, ..MetadataCtx::default() })
}
}