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
use std::{path::Path, result::Result as StdResult};
use crate::{
context::{FlightCtx, DEFAULT_IMAGE_REGISTRY_URL},
error::CliErrorKind,
ops::validator::validate_name,
};
pub fn validate_path_inline(s: &str) -> StdResult<String, String> {
validate_name(s)
.or_else(|_| validate_path(s))
.or_else(|_| validate_stdin(s))
.or_else(|_| validate_inline_flight_spec(s))
.map_err(|s| {
format!("the value must be a SPEC|PATH|- where PATH is a valid path or - opens STDIN\n\ncaused by: {s}")
})
}
pub fn validate_path(path: &str) -> StdResult<String, String> {
if !Path::exists(path.as_ref()) {
#[cfg(not(any(feature = "semantic_ui_tests", feature = "ui_tests")))]
return Err(format!("path '{path}' does not exist"));
}
Ok(path.into())
}
pub fn validate_stdin(s: &str) -> StdResult<String, &'static str> {
if s != "-" {
return Err("the value '-' was not provided");
}
Ok(s.into())
}
pub fn validate_inline_flight_spec(s: &str) -> StdResult<String, String> {
if let Err(e) = FlightCtx::from_inline_flight(s, DEFAULT_IMAGE_REGISTRY_URL) {
return Err(format!("invalid Fligth SPEC: {}", {
match e.kind() {
CliErrorKind::InlineFlightUnknownItem(s) => format!("unknown item {s}"),
CliErrorKind::InlineFlightMissingValue(s) => {
format!("key '{s}' is missing the value")
}
CliErrorKind::InlineFlightHasSpace => {
String::from("inline flight contains a space (' ')")
}
CliErrorKind::InlineFlightMissingImage => {
String::from("Missing required image=IMAGE-SPEC")
}
CliErrorKind::InlineFlightInvalidName(s) => {
format!("Flight name '{s}' isn't valid")
}
CliErrorKind::ImageReference(e) => format!("invalid IMAGE-SPEC: {e}"),
_ => unreachable!(),
}
}));
}
Ok(s.into())
}