diff --git a/cli/src/cli_builder.rs b/cli/src/cli_builder.rs index 913e46d7..d316658f 100644 --- a/cli/src/cli_builder.rs +++ b/cli/src/cli_builder.rs @@ -274,7 +274,7 @@ struct GeneratedCmd(CliCommand); #[async_trait] impl RunIt for GeneratedCmd { async fn run_cmd(&self, matches: &ArgMatches, ctx: &Context) -> Result<()> { - let cli = Cli::new_with_override(ctx.client().clone(), OxideOverride); + let cli = Cli::new_with_override(ctx.client()?.clone(), OxideOverride); cli.execute(self.0, matches).await; Ok(()) } diff --git a/cli/src/cmd_api.rs b/cli/src/cmd_api.rs index db25db41..72a1cd48 100644 --- a/cli/src/cmd_api.rs +++ b/cli/src/cmd_api.rs @@ -164,11 +164,12 @@ impl RunnableCmd for CmdApi { } } - let client = ctx.client().client(); - let uri = format!("{}{}", ctx.client().baseurl(), endpoint_with_query); + let client = ctx.client()?; + let rclient = client.client(); + let uri = format!("{}{}", client.baseurl(), endpoint_with_query); // Make the request. - let mut req = client.request(method.clone(), uri); + let mut req = rclient.request(method.clone(), uri); if !bytes.is_empty() { req = req.body(bytes.clone()) @@ -226,14 +227,9 @@ impl RunnableCmd for CmdApi { let Some(page_token) = maybe_page_token else { return Result::, Option)>>::Ok(None); }; - let uri = format!( - "{}{}?page_token={}", - ctx.client().baseurl(), - endpoint, - page_token, - ); - - let mut req = client.request(method.clone(), uri); + let uri = format!("{}{}?page_token={}", client.baseurl(), endpoint, page_token,); + + let mut req = rclient.request(method.clone(), uri); for (key, value) in headers.clone() { req = req.header(key, value); } diff --git a/cli/src/cmd_disk.rs b/cli/src/cmd_disk.rs index 44b6fa37..52b6d743 100644 --- a/cli/src/cmd_disk.rs +++ b/cli/src/cmd_disk.rs @@ -249,7 +249,7 @@ impl RunnableCmd for CmdDiskImport { false } async fn run(&self, ctx: &crate::context::Context) -> Result<()> { - let client = ctx.client(); + let client = ctx.client()?; if !Path::new(&self.path).exists() { bail!("path {} does not exist", self.path.to_string_lossy()); diff --git a/cli/src/cmd_instance.rs b/cli/src/cmd_instance.rs index 4db55669..4daaa77a 100644 --- a/cli/src/cmd_instance.rs +++ b/cli/src/cmd_instance.rs @@ -93,7 +93,7 @@ impl RunnableCmd for CmdInstanceSerialConsole { // cli process becomes an interactive remote shell. async fn run(&self, ctx: &crate::context::Context) -> Result<()> { let mut req = ctx - .client() + .client()? .instance_serial_console_stream() .instance(self.instance.clone()) .most_recent(self.most_recent); @@ -168,7 +168,7 @@ impl RunnableCmd for CmdInstanceSerialHistory { // cli process becomes an interactive remote shell. async fn run(&self, ctx: &crate::context::Context) -> Result<()> { let mut req = ctx - .client() + .client()? .instance_serial_console() .instance(self.instance.clone()); @@ -244,7 +244,7 @@ pub struct CmdInstanceFromImage { impl RunnableCmd for CmdInstanceFromImage { async fn run(&self, ctx: &crate::context::Context) -> Result<()> { // Validate the image and get its ID (if specified by name). - let mut image_request = ctx.client().image_view().image(&self.image); + let mut image_request = ctx.client()?.image_view().image(&self.image); // We only need the project if the image is specified by name. if let NameOrId::Name(_) = &self.image { image_request = image_request.project(&self.project); @@ -252,7 +252,7 @@ impl RunnableCmd for CmdInstanceFromImage { let image_view = image_request.send().await?; let instance = ctx - .client() + .client()? .instance_create() .project(&self.project) .body_map(|body| { diff --git a/cli/src/context.rs b/cli/src/context.rs index 8aebcd67..f8ac33f6 100644 --- a/cli/src/context.rs +++ b/cli/src/context.rs @@ -24,8 +24,10 @@ impl Context { Ok(Self { client, config }) } - pub fn client(&self) -> &Client { - self.client.as_ref().expect("no authenticated hosts") + pub fn client(&self) -> Result<&Client> { + self.client + .as_ref() + .ok_or_else(|| anyhow!("no authenticated hosts")) } pub fn config(&self) -> &Config { diff --git a/cli/src/main.rs b/cli/src/main.rs index 6bf4374c..ad522b21 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -62,7 +62,7 @@ async fn main() { .await .unwrap(); if let Err(e) = result { - eprintln!("{e:?}"); + eprintln!("{e}"); std::process::exit(1) } }