-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.rs
More file actions
300 lines (266 loc) · 10.4 KB
/
utils.rs
File metadata and controls
300 lines (266 loc) · 10.4 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
use color_eyre::eyre::Result;
use color_eyre::Report;
use std::collections::HashMap;
use std::error;
use std::fmt;
use std::fmt::Formatter;
use std::io::{stdin, stdout, Write};
use std::str;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
// The `DEL_CONFIRM` is the default value for delete confirmation across different types
pub const DEL_CONFIRM: Option<bool> = Some(false);
pub const REDACTED: &str = "*****";
pub const FILE_READ_ERR: &str = "Failed to read value from file.";
// old format but server is not accepting it now
// pub const ISO8601: &str = "%Y-%m-%dT%H:%M:%S%.fZ";
pub const ISO8601: &str = "%Y-%m-%dT%H:%M:%S%.6fZ";
pub const SEPARATOR: &str = "=========================";
pub const API_KEY_PAGE: &str = "\"API Access\"";
#[derive(Clone, Debug)]
pub enum ApplicationError {
InvalidApiUrl(String),
}
impl fmt::Display for ApplicationError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
ApplicationError::InvalidApiUrl(api_url) => {
write!(f, "No equivalent application URL for API: {api_url}")
}
}
}
}
impl error::Error for ApplicationError {}
/// Print a message to stderr in the specified color.
pub fn stderr_message(message: String, color: Color) {
let mut stderr = StandardStream::stderr(ColorChoice::Auto);
let mut color_spec = ColorSpec::new();
color_spec.set_fg(Some(color));
stderr.set_color(&color_spec).unwrap_or_default();
writeln!(&mut stderr, "{message}").unwrap_or_default();
stderr.reset().unwrap_or_default();
}
/// Print the provided message to stderr in 'Yellow'.
pub fn warning_message<S: Into<String>>(message: S) {
stderr_message(message.into(), Color::Yellow);
}
/// Print the provided message to stderr in 'Red'.
pub fn error_message<S: Into<String>>(message: S) {
stderr_message(message.into(), Color::Red);
}
/// Print the provided message to stderr in 'Cyan'.
pub fn help_message<S: Into<String>>(message: S) {
stderr_message(message.into(), Color::Cyan);
}
pub fn error_no_environment_message(env_name: &str) {
error_message(format!(
"The '{env_name}' environment could not be found in your account.",
));
}
/// Add "WARN:" prefix to the message, and print it to stderr
pub fn warn_user<S: Into<String>>(message: S) {
warning_message(format!("WARN: {}", message.into()));
}
/// Simple method for standardizing the message when no sub-command is executed.
pub fn warn_missing_subcommand(command: &str) {
warn_user(format!("No '{command}' sub-command executed."));
}
/// Method for standardizing message about list of warnings.
pub fn warn_unresolved_params(errors: &[String]) {
if !errors.is_empty() {
warning_message(format!(
"Errors resolving parameters:\n{}\n",
errors.join("\n")
));
}
}
/// Format the strings in the list of errors
pub fn format_param_error(param_name: &str, param_err: &str) -> String {
format!(" {param_name}: {param_err}")
}
/// Prompts the user for 'y/n' output.
///
/// If the user answers 'y' (case insensitive), 'true' is returned.
/// If the user answers 'n' (case insensitive), 'false' is returned.
/// The prompt will be repeated upto 3 times if the users does not enter 'y|n'. If the
/// max tries are exceeded, it returns 'false'.
pub fn user_confirm(message: String, default: Option<bool>) -> bool {
let max_tries = 3;
let mut confirmed = false;
let action = match default {
None => "y/n",
Some(true) => "Y/n",
Some(false) => "y/N",
};
for _ in 0..max_tries {
let mut input = String::new();
print!("{message}? ({action}) ");
stdout().flush().unwrap();
let _ = stdin().read_line(&mut input);
input = input.trim().to_string().to_lowercase();
if input.is_empty() {
if let Some(value) = default {
confirmed = value;
break;
}
}
if input.as_str() == "y" || input.as_str() == "yes" {
confirmed = true;
break;
}
if input.as_str() == "n" || input.as_str() == "no" {
break;
}
}
confirmed
}
/// Get the web application URL for the `API_KEY_PAGE`
pub fn get_api_access_url(api_url: &str) -> Result<String> {
// remove the any trailing '/'
let mut api = api_url.to_string();
if api.ends_with('/') {
api.truncate(api.len() - 1);
}
let api_access_path = "organization/api";
if api.starts_with("https://localhost:8000") {
return Ok(format!("https://localhost:7000/{api_access_path}"));
}
if api.starts_with("https://api.") && api.ends_with("cloudtruth.io") {
return Ok(format!(
"{}/{}",
api.replace("https://api", "https://app"),
api_access_path
));
}
Err(Report::new(ApplicationError::InvalidApiUrl(
api_url.to_string(),
)))
}
/// Quick pass at providing a current-time in an acceptable time format for the server.
pub fn current_time() -> String {
let now = Utc::now();
now.format(ISO8601).to_string()
}
/// Parse a list of key=value pairs separated by commas (example: foo=bar,bar=qux) into a HashMap
pub fn parse_key_value_pairs(input: &str) -> Option<HashMap<String, String>> {
input
.split(',')
.map(|pair| {
pair.split_once('=')
.map(|(key, value)| (key.to_string(), value.to_string()))
})
.collect()
}
/// Takes an optional CLI argument (`Option<&str>`) attempts to parse it to a valid `DateTime`, and
/// returns the ISO format that the API expects.
///
/// If this is not a recognized date-time format, it will return `None`.
pub fn parse_datetime(input: Option<&str>) -> Option<String> {
if let Some(orig) = input {
if let Ok(rfc2822) = DateTime::parse_from_rfc2822(orig) {
Some(rfc2822.format(ISO8601).to_string())
} else if let Ok(rfc3339) = DateTime::parse_from_rfc3339(orig) {
Some(rfc3339.format(ISO8601).to_string())
} else if let Ok(datetime) = NaiveDateTime::parse_from_str(orig, "%Y-%m-%dT%H:%M:%S%.fZ") {
Some(datetime.format(ISO8601).to_string())
} else if let Ok(datetime) = NaiveDateTime::parse_from_str(orig, "%Y-%m-%dT%H:%M:%S%.f") {
Some(Utc.from_utc_datetime(&datetime).format(ISO8601).to_string())
} else if let Ok(time_only) = NaiveTime::parse_from_str(orig, "%H:%M:%S%.f") {
let dt = Utc.from_utc_datetime(&Utc::now().date_naive().and_time(time_only));
Some(dt.format(ISO8601).to_string())
} else if let Ok(full_date) = NaiveDate::parse_from_str(orig, "%Y-%m-%d") {
let dt = Utc.from_utc_datetime(&full_date.and_time(default()));
Some(dt.format(ISO8601).to_string())
} else if let Ok(us_date) = NaiveDate::parse_from_str(orig, "%m-%d-%Y") {
let dt = Utc.from_utc_datetime(&us_date.and_time(default()));
Some(dt.format(ISO8601).to_string())
} else if let Ok(us_date) = NaiveDate::parse_from_str(orig, "%m/%d/%Y") {
let dt = Utc.from_utc_datetime(&us_date.and_time(default()));
Some(dt.format(ISO8601).to_string())
} else {
None
}
} else {
None
}
}
/// Returns a tag value, if the input value is not a recognized date-time format.
pub fn parse_tag(input: Option<&str>) -> Option<String> {
if parse_datetime(input).is_some() {
None
} else {
input.map(String::from)
}
}
/// Return the default value of a type according to the `Default` trait.
///
/// The type to return is inferred from context; this is equivalent to
/// `Default::default()` but shorter to type.
///
/// See: https://github.com/rust-lang/rust/issues/73014
#[inline]
pub fn default<T: Default>() -> T {
Default::default()
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn timedate_and_tag_parsing() {
// full RFC2822144
let now = Utc::now();
let input = now.to_rfc2822();
let output = parse_datetime(Some(&input)).unwrap();
assert_eq!(now.format("%FT%T.000000Z").to_string(), output); // no fractional seconds
assert_eq!(parse_tag(Some(&input)), None);
// full RFC23339
let now = Utc::now();
let input = now.to_rfc3339();
let output = parse_datetime(Some(&input)).unwrap();
assert_eq!(now.format(ISO8601).to_string(), output);
assert_eq!(parse_tag(Some(&input)), None);
// ISO8601 with Z offset
let input = Some("2021-07-27T18:34:23.270824Z");
let expected = Some("2021-07-27T18:34:23.270824Z".into());
assert_eq!(parse_datetime(input), expected);
assert_eq!(parse_tag(input), None);
// ISO8601 - missing trailing Z
let input = "2021-07-27T18:34:23.270824";
let output = parse_datetime(Some(input));
assert!(output.unwrap().contains(input));
assert_eq!(parse_tag(Some(input)), None);
// time only, without milliseconds
let input = Some("02:04:08");
let output = parse_datetime(input).unwrap();
assert!(output.contains("02:04:08"));
assert_eq!(parse_tag(input), None);
// time only, with milliseconds
let input = Some("03:05:12.345");
let output = parse_datetime(input).unwrap();
assert!(output.contains("T03:05:12.345"));
assert_eq!(parse_tag(input), None);
// full date (no time)
let input = Some("2020-02-02");
let output = parse_datetime(input).unwrap();
assert!(output.contains("2020-02-02"));
assert_eq!(parse_tag(input), None);
// US date with slashes
let input = Some("01/19/2021");
let output = parse_datetime(input).unwrap();
assert!(output.contains("2021-01-19"));
assert_eq!(parse_tag(input), None);
// US date with dashes
let input = Some("01-19-2021");
let output = parse_datetime(input).unwrap();
assert!(output.contains("2021-01-19"));
assert_eq!(parse_tag(input), None);
// unfortunately, it lets this through too!
let input = Some("this is bogus");
let expected = input.map(String::from);
assert_eq!(parse_datetime(input), None);
assert_eq!(parse_tag(input), expected);
// finally, no option given
assert_eq!(parse_datetime(None), None);
assert_eq!(parse_tag(None), None);
}
}