Skip to content

Commit

Permalink
change handling of missing auth (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahl authored Jan 5, 2024
1 parent 62fa756 commit a3e6e1b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cli/src/cli_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
18 changes: 7 additions & 11 deletions cli/src/cmd_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -226,14 +227,9 @@ impl RunnableCmd for CmdApi {
let Some(page_token) = maybe_page_token else {
return Result::<Option<(Vec<serde_json::Value>, Option<String>)>>::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);
}
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cmd_disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
8 changes: 4 additions & 4 deletions cli/src/cmd_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -244,15 +244,15 @@ 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);
};
let image_view = image_request.send().await?;

let instance = ctx
.client()
.client()?
.instance_create()
.project(&self.project)
.body_map(|body| {
Expand Down
6 changes: 4 additions & 2 deletions cli/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async fn main() {
.await
.unwrap();
if let Err(e) = result {
eprintln!("{e:?}");
eprintln!("{e}");
std::process::exit(1)
}
}
Expand Down

0 comments on commit a3e6e1b

Please sign in to comment.