Skip to content

Commit a753177

Browse files
committed
Improve error handling and move to coffee mod
1 parent 863b169 commit a753177

2 files changed

Lines changed: 43 additions & 23 deletions

File tree

rust/stackable-cockpit/src/platform/operator/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ pub enum SpecParseError {
5151
#[snafu(display("empty operator spec input"))]
5252
EmptyInput,
5353

54-
#[snafu(display("invalid operator name {name:?}"))]
54+
#[snafu(display(
55+
"invalid operator name {name:?}. \
56+
It could be the case that this version of stackablectl is too old to know about this particular operator"
57+
))]
5558
InvalidName { name: String },
5659
}
5760

rust/stackablectl/src/cmds/operator.rs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,6 @@ const INSTALL_AFTER_HELP_TEXT: &str = "Examples:
3838
3939
Use \"stackablectl operator install <OPERATOR> -c <OPTION>\" to create a local cluster";
4040

41-
const COFFEE_ASCII_ART: &str = r#"
42-
) )
43-
( (
44-
.------.
45-
| |]
46-
\ /
47-
`----'
48-
49-
Psst... "coffee" is not an operator, but we get it.
50-
Stackable runs on coffee too. Have a great day! ☕
51-
"#;
52-
5341
#[derive(Debug, Args)]
5442
pub struct OperatorArgs {
5543
#[command(subcommand)]
@@ -110,7 +98,7 @@ Possible valid values are:
11098
11199
Use \"stackablectl operator list\" to list available versions for all operators
112100
Use \"stackablectl operator describe <OPERATOR>\" to get available versions for one operator")]
113-
operators: Vec<String>,
101+
operators: Vec<coffee::OperatorOrCoffee>,
114102

115103
/// Namespace in the cluster used to deploy the operators
116104
#[arg(long, default_value = DEFAULT_OPERATOR_NAMESPACE, visible_aliases(["operator-ns"]))]
@@ -181,9 +169,6 @@ pub enum CmdError {
181169

182170
#[snafu(display("OCI error"))]
183171
OciError { source: oci::Error },
184-
185-
#[snafu(display("failed to parse operator spec"))]
186-
ParseOperatorSpec { source: operator::SpecParseError },
187172
}
188173

189174
/// This list contains a list of operator version grouped by stable, test and
@@ -333,15 +318,14 @@ async fn install_cmd(args: &OperatorInstallArgs, cli: &Cli) -> Result<String, Cm
333318
let operators: Vec<operator::OperatorSpec> = args
334319
.operators
335320
.iter()
336-
.filter_map(|operator| match operator.as_str() {
337-
"coffee" | "coffe" => {
338-
indicatif_println!("{COFFEE_ASCII_ART}");
321+
.filter_map(|operator| match operator {
322+
coffee::OperatorOrCoffee::Coffee => {
323+
indicatif_println!("{}", coffee::COFFEE_ASCII_ART);
339324
None
340325
}
341-
_ => Some(operator),
326+
coffee::OperatorOrCoffee::Operator(spec) => Some(spec.clone()),
342327
})
343-
.map(|s| s.parse().context(ParseOperatorSpecSnafu))
344-
.collect::<Result<_, _>>()?;
328+
.collect();
345329

346330
// In case no operators need to be installed (e.g. coffee was already installed), there is no
347331
// need to connect to Kubernetes and potentially produce error messages.
@@ -631,3 +615,36 @@ where
631615
None => Ok(vec![]),
632616
}
633617
}
618+
619+
mod coffee {
620+
use std::str::FromStr;
621+
622+
pub const COFFEE_ASCII_ART: &str = r#"
623+
) )
624+
( (
625+
.------.
626+
| |]
627+
\ /
628+
`----'
629+
630+
Psst... "coffee" is not an operator, but we get it.
631+
Stackable runs on coffee too. Have a great day! ☕
632+
"#;
633+
634+
#[derive(Clone, Debug)]
635+
pub enum OperatorOrCoffee {
636+
Operator(super::operator::OperatorSpec),
637+
Coffee,
638+
}
639+
640+
impl FromStr for OperatorOrCoffee {
641+
type Err = super::operator::SpecParseError;
642+
643+
fn from_str(s: &str) -> Result<Self, Self::Err> {
644+
match s {
645+
"coffee" | "coffe" => Ok(OperatorOrCoffee::Coffee),
646+
_ => s.parse().map(OperatorOrCoffee::Operator),
647+
}
648+
}
649+
}
650+
}

0 commit comments

Comments
 (0)