Skip to content

Commit

Permalink
feat: add error banner to avoid vault failing in silence (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
zifeo authored Aug 3, 2023
1 parent b039c6d commit 31818a8
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 19 deletions.
15 changes: 8 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resolver = "2"

[package]
name = "lade"
version = "0.7.1-beta.1"
version = "0.8.0"
edition = "2021"
description = "Automatically load secrets from your preferred vault as environment variables, and clear them once your shell command is over."
license = "MPL-2.0"
Expand All @@ -20,24 +20,25 @@ self_update = { version = "0.37.0", features = [
"compression-zip-deflate",
"compression-zip-bzip2",
] }
serde = { version = "1.0.171", features = ["derive"] }
serde_yaml = "0.9.23"
clap = { version = "4.3.14", features = ["derive"] }
serde = { version = "1.0.180", features = ["derive"] }
serde_yaml = "0.9.25"
clap = { version = "4.3.19", features = ["derive"] }
regex = "1.9.1"
lade-sdk = { path = "./sdk", version = "0.7.1-beta.1" }
lade-sdk = { path = "./sdk", version = "0.8.0" }
tokio = { version = "1", features = ["full"] }
indexmap = { version = "2.0.0", features = ["serde"] }
clap-verbosity-flag = "2.0.1"
env_logger = "0.10.0"
openssl = { version = "0.10.55", features = ["vendored"] }
serde_json = "1.0.103"
serde_json = "1.0.104"
futures = "0.3.28"
path-clean = "1.0.1"
directories = "5.0.1"
chrono = { version = "0.4.26", features = ["serde"] }
log = "0.4.19"
semver = "1.0.18"
sysinfo = "0.29.5"
sysinfo = "0.29.7"
textwrap = "0.16.0"

[dev-dependencies]
assert_cmd = "2.0.12"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ steps.

```bash
curl -fsSL https://raw.githubusercontent.com/zifeo/lade/main/installer.sh | bash
sudo mv whiz /usr/local/bin # or any other directory in your $PATH

# via cargo
cargo install lade --locked
Expand Down Expand Up @@ -152,8 +151,10 @@ double `!!` escapes itself.
## Development

```bash
eval "$(lade off)"
eval "$(cargo run -- on)"
echo a $A1 $A2 $B1 $B2 $B3 $C1 $C2 $C3
cargo run -- -vvv set echo a
eval "$(cargo run -- off)"
eval "$(lade on)"
```
30 changes: 27 additions & 3 deletions installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ LATEST_VERSION=$(curl "$RELEASE_URL/latest" -s -L -I -o /dev/null -w '%{url_effe
LATEST_VERSION="${LATEST_VERSION##*v}"

PLATFORM="${PLATFORM:-}"
OUT_DIR="${OUT_DIR:-/usr/local/bin}"
VERSION="${VERSION:-$LATEST_VERSION}"
MACHINE=$(uname -m)

Expand Down Expand Up @@ -83,9 +84,32 @@ tar -xzf "$ASSET.$EXT" "$EXE"
rm "$ASSET.$EXT"
chmod +x "$EXE"

cat <<EOF
if [ "${OUT_DIR}" = "." ]; then
printf "\n\n%s has been extracted to your current directory\n" "$EXE"
else
cat <<EOF
$EXE will be moved to $OUT_DIR
Set the OUT_DIR environment variable to change the installation directory:
$ curl -fsSL $INSTALLER_URL | OUT_DIR=. bash
EOF
if [ -w "${OUT_DIR}" ]; then
read -p "Press enter to continue (or cancel with Ctrl+C):"
mv "$EXE" "$OUT_DIR"
else
printf "Your password is required to run \"sudo mv %s %s\":\n" "$EXE" "$OUT_DIR"
sudo mv "$EXE" "$OUT_DIR"
fi
fi

$NAME has been extracted to your folder, please move it to /usr/local/bin (or any other folder on your PATH):
OUT_DIR=$(realpath $OUT_DIR)
if [[ ":$PATH:" != *":$OUT_DIR:"* ]]; then
cat <<EOF
The installation directory is not in your PATH, consider adding it:
$ export PATH="\$PATH:$OUT_DIR"
Or moving the executable to another directory in your PATH:
$ sudo mv $EXE /usr/local/bin
$ $EXE
EOF
fi
2 changes: 1 addition & 1 deletion lade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
B3: doppler://api.doppler.com/lade/prd/DOPPLER_B

^echo c:
C1: infisical://app.infisical.com/63a2290a0edf8bf1f65e3784/dev/INFISICAL_A
C1: infisical://infisical.exponent.ch/63fd114968d64dec0823ac52/test/STORE_FILE
C2: infisical://app.infisical.com/63a2290a0edf8bf1f65e3784/dev/INFISICAL_B
C3: infisical://app.infisical.com/63a2290a0edf8bf1f65e3784/prod/INFISICAL_B

Expand Down
12 changes: 6 additions & 6 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lade-sdk"
version = "0.7.1-beta.1"
version = "0.8.0"
edition = "2021"
description = "Lade SDK"
license = "MPL-2.0"
Expand All @@ -9,18 +9,18 @@ license = "MPL-2.0"
access-json = "0.1.0"
anyhow = "1.0.72"
async-process = "1.7.0"
async-trait = "0.1.71"
async-trait = "0.1.72"
directories = "5.0.1"
futures = "0.3.28"
itertools = "0.11.0"
log = "0.4.19"
once_cell = "1.18.0"
regex = "1.9.1"
rust-ini = "0.19.0"
serde = { version = "1.0.171", features = ["derive"] }
serde_json = "1.0.103"
serde_yaml = "0.9.23"
tempfile = "3.6.0"
serde = { version = "1.0.180", features = ["derive"] }
serde_json = "1.0.104"
serde_yaml = "0.9.25"
tempfile = "3.7.0"
tokio = { version = "1.29.1", features = ["fs"] }
toml = "0.7.6"
url = "2.4.0"
34 changes: 33 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use log::{debug, warn};
use self_update::{backends::github::Update, cargo_crate_version, update::UpdateStatus};
use semver::Version;
use std::{env, ffi::OsStr, fs};
use tokio::time;
mod config;
mod shell;
use clap::{CommandFactory, Parser};
Expand Down Expand Up @@ -123,7 +124,35 @@ async fn main() -> Result<()> {
Command::Set(EvalCommand { commands }) => {
debug!("setting: {:?}", commands);
let command = commands.join(" ");
for (output, vars) in config.collect_hydrate(command).await? {

let hydration = match config.collect_hydrate(command).await {
std::result::Result::Ok(hydration) => hydration,
Err(e) => {
let width = 80;
let wrap_width = width - 4;

let header = "Lade could not get secrets from one loader:";
let error = e.to_string();
let hint =
"Hint: check whether the loader is connected? to the correct? vault.";
let wait = "Waiting 5 seconds before continuing...";

eprintln!("┌{}┐", "-".repeat(width - 2));
eprintln!("| {} {}|", header, " ".repeat(wrap_width - header.len()),);
for line in textwrap::wrap(error.trim(), wrap_width - 2) {
eprintln!("| > {} {}|", line, " ".repeat(wrap_width - 2 - line.len()),);
}
eprintln!("| {} {}|", hint, " ".repeat(wrap_width - hint.len()));
eprintln!("| {} {}|", wait, " ".repeat(wrap_width - wait.len()));
eprintln!("└{}┘", "-".repeat(width - 2));
time::sleep(time::Duration::from_secs(5)).await;
std::process::exit(1);
}
};

let mut names = vec![];
for (output, vars) in hydration {
names.extend(vars.keys().cloned());
match output {
Some(path) => {
if path.exists() {
Expand All @@ -145,6 +174,9 @@ async fn main() -> Result<()> {
}
}
}
if !names.is_empty() {
eprintln!("Lade loaded: {}.", names.join(", "));
}
Ok(())
}
Command::Unset(EvalCommand { commands }) => {
Expand Down
7 changes: 7 additions & 0 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl Shell {
_ => bail!("Unsupported shell"),
}
}

pub fn on(&self) -> String {
match self {
Shell::Bash => format!(
Expand All @@ -57,13 +58,15 @@ impl Shell {
Shell::Fish => import!("../scripts/on.fish"),
}
}

pub fn off(&self) -> String {
match self {
Shell::Bash => import!("../scripts/off.bash"),
Shell::Zsh => import!("../scripts/off.zsh"),
Shell::Fish => import!("../scripts/off.fish"),
}
}

pub fn set(&self, env: HashMap<String, String>) -> String {
env.into_iter()
.map(|(k, v)| match self {
Expand All @@ -77,19 +80,23 @@ impl Shell {
.collect::<Vec<_>>()
.join(";")
}

pub fn unset(&self, keys: Vec<String>) -> String {
let format = match self {
Shell::Zsh | Shell::Bash => |k| format!("unset -v ${k}"),
Shell::Fish => |k| format!("set --global --erase {k}"),
};
keys.into_iter().map(format).collect::<Vec<_>>().join(";")
}

pub fn install(&self) -> String {
self.configure_auto_launch(true).display().to_string()
}

pub fn uninstall(&self) -> String {
self.configure_auto_launch(false).display().to_string()
}

fn configure_auto_launch(&self, install: bool) -> PathBuf {
let user = directories::UserDirs::new().expect("cannot get HOME location");
let home_dir = user.home_dir();
Expand Down

0 comments on commit 31818a8

Please sign in to comment.