Skip to content

Commit

Permalink
feat: improve error messages (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
zifeo authored Apr 18, 2023
1 parent c9a687b commit 4b4849b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 27 deletions.
3 changes: 3 additions & 0 deletions lade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@
F3: file://~/Documents/github/lade/examples/sources/config.toml?query=.format
F4: file://$HOME/Documents/github/lade/examples/sources/config.yaml?query=.format
F5: file://Cargo.toml?query=.package.version

^echo z:
DEBUG: "true"
20 changes: 15 additions & 5 deletions sdk/src/providers/doppler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::HashMap, path::Path};

use crate::Hydration;
use anyhow::{anyhow, bail, Ok, Result};
use anyhow::{anyhow, bail, Result};
use async_process::{Command, Stdio};
use async_trait::async_trait;
use futures::future::try_join_all;
Expand Down Expand Up @@ -87,13 +87,20 @@ impl Provider for Doppler {
];
info!("{}", cmd.join(" "));

let child = Command::new(cmd[0])
let child = match Command::new(cmd[0])
.args(&cmd[1..])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await
.expect("error running Doppler");
.await {
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
bail!("Doppler CLI not found. Make sure the binary is in your PATH or install it from https://docs.doppler.com/docs/install-cli.")
},
Err(e) => {
bail!("Doppler error: {e}")
},
Ok(child) => child,
};

let loaded = serde_json::from_slice::<
HashMap<String, DopplerExport>,
Expand All @@ -112,7 +119,10 @@ impl Provider for Doppler {
var,
loaded
.get(key)
.expect("Variable not found")
.unwrap_or_else(|| panic!(
"Variable not found in Doppler: {}",
key
))
.computed
.clone(),
)
Expand Down
16 changes: 12 additions & 4 deletions sdk/src/providers/infisical.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, bail, Ok, Result};
use anyhow::{anyhow, bail, Result};
use async_process::{Command, Stdio};
use async_trait::async_trait;
use futures::future::try_join_all;
Expand Down Expand Up @@ -100,14 +100,22 @@ impl Provider for Infisical {
write!(file, "{}", serde_json::to_string(&config)?)?;
drop(file);

let child = Command::new(cmd[0])
let child = match Command::new(cmd[0])
.args(&cmd[1..])
.current_dir(temp_dir.path())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await
.expect("error running infisical");
{
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
bail!("Infisical CLI not found. Make sure the binary is in your PATH or install it from https://infisical.com/docs/cli/overview.")
},
Err(e) => {
bail!("Infisical error: {e}")
},
Ok(child) => child,
};

temp_dir.close()?;

Expand Down Expand Up @@ -135,7 +143,7 @@ impl Provider for Infisical {
var,
loaded
.get(key)
.expect("Variable not found")
.unwrap_or_else(|| panic!("Variable not found in Infisical: {}", key))
.clone(),
)
})
Expand Down
43 changes: 30 additions & 13 deletions sdk/src/providers/onepassword.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, path::Path};

use anyhow::{anyhow, bail, Ok, Result};
use anyhow::{anyhow, bail, Result};
use async_process::{Command, Stdio};
use async_trait::async_trait;
use futures::{future::try_join_all, AsyncWriteExt};
Expand Down Expand Up @@ -54,14 +54,10 @@ impl Provider for OnePassword {
return Ok(HashMap::new());
}

let json = serde_json::to_string(
&vars
.iter()
.map(|(k, v)| {
(k, v.replace(&format!("{host}/"), "").replace("%20", " "))
})
.collect::<HashMap<_, _>>(),
)?;
let json = &vars
.iter()
.map(|(k, v)| (k, v.replace(&format!("{host}/"), "").replace("%20", " ")))
.collect::<HashMap<_, _>>();
let cmd = &["op", "inject", "--account", &host.to_string()];
info!("{}", cmd.join(" "));

Expand All @@ -75,10 +71,20 @@ impl Provider for OnePassword {
debug!("stdin: {:?}", json);

let mut stdin = process.stdin.take().expect("Failed to open stdin");
stdin.write_all(json.as_bytes()).await?;
stdin
.write_all(serde_json::to_string(&json)?.as_bytes())
.await?;
drop(stdin);

let child = process.output().await?;
let child = match process.output().await {
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
bail!("1Password CLI not found. Make sure the binary is in your PATH or install it from https://1password.com/downloads/command-line/.")
},
Err(e) => {
bail!("1Password error: {e}")
},
Ok(child) => child,
};

let loaded =
serde_json::from_slice::<Hydration>(&child.stdout).map_err(|err| {
Expand All @@ -87,9 +93,20 @@ impl Provider for OnePassword {
})?;

let hydration = vars
.into_iter()
.iter()
.map(|(key, var)| {
(var, loaded.get(&key).expect("Variable not found").clone())
let value = match (loaded.get(key), json.get(key)) {
(Some(loaded), Some(original)) if loaded == original => None,
(Some(loaded), _) => Some(loaded),
_ => None,
};

(
var.clone(),
value
.unwrap_or_else(|| panic!("Variable not found in 1Password: {}", key))
.clone(),
)
})
.collect::<Hydration>();

Expand Down
20 changes: 15 additions & 5 deletions sdk/src/providers/vault.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, bail, Ok, Result};
use anyhow::{anyhow, bail, Result};
use async_process::{Command, Stdio};
use async_trait::async_trait;
use futures::future::try_join_all;
Expand Down Expand Up @@ -85,13 +85,20 @@ impl Provider for Vault {
];
info!("{}", cmd.join(" "));

let child = Command::new(cmd[0])
let child = match Command::new(cmd[0])
.args(&cmd[1..])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await
.expect("error running Vault");
.await {
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
bail!("Vault CLI not found. Make sure the binary is in your PATH or install it from https://developer.hashicorp.com/vault/docs/commands.")
},
Err(e) => {
bail!("Vault error: {e}")
},
Ok(child) => child,
};

let loaded =
serde_json::from_slice::<VaultExport>(&child.stdout)
Expand All @@ -114,7 +121,10 @@ impl Provider for Vault {
.nth(3)
.expect("Missing variable"),
)
.expect("Variable not found")
.unwrap_or_else(|| panic!(
"Variable not found in Vault: {}",
key
))
.clone(),
)
})
Expand Down

0 comments on commit 4b4849b

Please sign in to comment.