Skip to content

Commit

Permalink
feat: fix lade on for bash (#44)
Browse files Browse the repository at this point in the history
* fix: bash compat improved

* feat: upgrade deps

* chore: release 0.8.1

* chore: fix
  • Loading branch information
zifeo authored Aug 10, 2023
1 parent 3aa5d8f commit 2a417fc
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 43 deletions.
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ updates:
directory: /
schedule:
interval: monthly
groups:
deps:
patterns:
- "*"
- package-ecosystem: cargo
directory: /
schedule:
interval: monthly
groups:
deps:
patterns:
- "*"
18 changes: 18 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
changelog:
exclude:
authors:
- octocat
- dependabot
categories:
- title: Breaking changes 🛠
labels:
- breaking-change
- title: Documentation improvements 📖
labels:
- documentation
- title: Bug fixes 🐛
labels:
- bug
- title: New features 🎉
labels:
- "*"
12 changes: 6 additions & 6 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.8.1-beta.1"
version = "0.8.1"
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,16 +20,16 @@ self_update = { version = "0.37.0", features = [
"compression-zip-deflate",
"compression-zip-bzip2",
] }
serde = { version = "1.0.180", features = ["derive"] }
serde = { version = "1.0.183", features = ["derive"] }
serde_yaml = "0.9.25"
clap = { version = "4.3.19", features = ["derive"] }
regex = "1.9.1"
lade-sdk = { path = "./sdk", version = "0.8.1-beta.1" }
clap = { version = "4.3.21", features = ["derive"] }
regex = "1.9.3"
lade-sdk = { path = "./sdk", version = "0.8.1" }
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"] }
openssl = { version = "0.10.56", features = ["vendored"] }
serde_json = "1.0.104"
futures = "0.3.28"
path-clean = "1.0.1"
Expand Down
15 changes: 3 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ cargo install --git https://github.com/zifeo/lade --locked
# upgrade
lade upgrade

# install/uninstall shell auto launch (only required once, see below)
# install shell hooks (only required once)
lade install
lade uninstall
```

Compatible shells: [Fish](https://fishshell.com),
Expand All @@ -53,19 +52,11 @@ using a regex and load them into environment variables or files for the time of
the run.

```bash
eval "$(lade on)"

cd examples/terraform terraform apply

cd examples/terraform
terraform apply
# example = "hello world"

eval "$(lade off)"
```

You can also add `eval "$(lade on)"` to your shell configuration file (e.g.
`~/.bashrc`, `~/.zshrc` or `~/.config/fish/config.fish`) to automatically enable
Lade on each shell session (`lade install` will configure this for you).

See [lade.yml](lade.yml) or the [examples](./examples) folders for other uses
cases.

Expand Down
17 changes: 10 additions & 7 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:-}"
TMP_DIR=$(mktemp -d)
OUT_DIR="${OUT_DIR:-/usr/local/bin}"
VERSION="${VERSION:-$LATEST_VERSION}"
MACHINE=$(uname -m)
Expand Down Expand Up @@ -64,7 +65,7 @@ printf "Detected version: %s\n" "$VERSION"
ASSET="$NAME-v$VERSION-$PLATFORM"
DOWNLOAD_URL="$RELEASE_URL/download/v$VERSION/$ASSET.$EXT"

if curl --fail --silent --location --output "$ASSET.$EXT" "$DOWNLOAD_URL"; then
if curl --fail --silent --location --output "$TMP_DIR/$ASSET.$EXT" "$DOWNLOAD_URL"; then
printf "Downloaded successfully: %s\n" "$ASSET.$EXT"
else
cat >&2 <<EOF
Expand All @@ -80,11 +81,11 @@ EOF
exit 1
fi

tar -xzf "$ASSET.$EXT" "$EXE"
rm "$ASSET.$EXT"
chmod +x "$EXE"
tar -C "$TMP_DIR" -xzf "$TMP_DIR/$ASSET.$EXT" "$EXE"
chmod +x "$TMP_DIR/$EXE"

if [ "${OUT_DIR}" = "." ]; then
mv "$TMP_DIR/$EXE" .
printf "\n\n%s has been extracted to your current directory\n" "$EXE"
else
cat <<EOF
Expand All @@ -96,13 +97,15 @@ $ 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"
mv "$TMP_DIR/$EXE" "$OUT_DIR"
else
printf "Your password is required to run \"sudo mv %s %s\":\n" "$EXE" "$OUT_DIR"
sudo mv "$EXE" "$OUT_DIR"
printf "Sudo is required to run \"sudo mv %s %s\":\n" "$TMP_DIR/$EXE" "$OUT_DIR"
sudo mv "$TMP_DIR/$EXE" "$OUT_DIR"
fi
fi

rm -r "$TMP_DIR"

OUT_DIR=$(realpath $OUT_DIR)
if [[ ":$PATH:" != *":$OUT_DIR:"* ]]; then
cat <<EOF
Expand Down
32 changes: 21 additions & 11 deletions scripts/bash-preexec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fi

# We only support Bash 3.1+.
# Note: BASH_VERSINFO is first available in Bash-2.0.
if [[ -z "${BASH_VERSINFO-}" || BASH_VERSINFO[0] -lt 3 || BASH_VERSINFO[0] -eq 3 && BASH_VERSINFO[1] -lt 1 ]]; then
if [[ -z "${BASH_VERSINFO-}" ]] || (( BASH_VERSINFO[0] < 3 || (BASH_VERSINFO[0] == 3 && BASH_VERSINFO[1] < 1) )); then
return 1
fi

Expand Down Expand Up @@ -183,8 +183,8 @@ __bp_set_ret_value() {

__bp_in_prompt_command() {

local prompt_command_array
IFS=$'\n;' read -rd '' -a prompt_command_array <<< "${PROMPT_COMMAND:-}"
local prompt_command_array IFS=$'\n;'
read -rd '' -a prompt_command_array <<< "${PROMPT_COMMAND[*]:-}"

local trimmed_arg
__bp_trim_whitespace trimmed_arg "${1:-}"
Expand Down Expand Up @@ -290,7 +290,7 @@ __bp_preexec_invoke_exec() {

__bp_install() {
# Exit if we already have this installed.
if [[ "${PROMPT_COMMAND:-}" == *"__bp_precmd_invoke_cmd"* ]]; then
if [[ "${PROMPT_COMMAND[*]:-}" == *"__bp_precmd_invoke_cmd"* ]]; then
return 1;
fi

Expand Down Expand Up @@ -326,17 +326,25 @@ __bp_install() {
local existing_prompt_command
# Remove setting our trap install string and sanitize the existing prompt command string
existing_prompt_command="${PROMPT_COMMAND:-}"
existing_prompt_command="${existing_prompt_command//${__bp_install_string}[;$'\n']}" # Edge case of appending to PROMPT_COMMAND
existing_prompt_command="${existing_prompt_command//$__bp_install_string}"
# Edge case of appending to PROMPT_COMMAND
existing_prompt_command="${existing_prompt_command//$__bp_install_string/:}" # no-op
existing_prompt_command="${existing_prompt_command//$'\n':$'\n'/$'\n'}" # remove known-token only
existing_prompt_command="${existing_prompt_command//$'\n':;/$'\n'}" # remove known-token only
__bp_sanitize_string existing_prompt_command "$existing_prompt_command"
if [[ "${existing_prompt_command:-:}" == ":" ]]; then
existing_prompt_command=
fi

# Install our hooks in PROMPT_COMMAND to allow our trap to know when we've
# actually entered something.
PROMPT_COMMAND=$'__bp_precmd_invoke_cmd\n'
if [[ -n "$existing_prompt_command" ]]; then
PROMPT_COMMAND+=${existing_prompt_command}$'\n'
fi;
PROMPT_COMMAND+='__bp_interactive_mode'
PROMPT_COMMAND='__bp_precmd_invoke_cmd'
PROMPT_COMMAND+=${existing_prompt_command:+$'\n'$existing_prompt_command}
if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 1) )); then
PROMPT_COMMAND+=('__bp_interactive_mode')
else
# shellcheck disable=SC2179 # PROMPT_COMMAND is not an array in bash <= 5.0
PROMPT_COMMAND+=$'\n__bp_interactive_mode'
fi

# Add two functions to our arrays for convenience
# of definition.
Expand All @@ -359,8 +367,10 @@ __bp_install_after_session_init() {
local sanitized_prompt_command
__bp_sanitize_string sanitized_prompt_command "${PROMPT_COMMAND:-}"
if [[ -n "$sanitized_prompt_command" ]]; then
# shellcheck disable=SC2178 # PROMPT_COMMAND is not an array in bash <= 5.0
PROMPT_COMMAND=${sanitized_prompt_command}$'\n'
fi;
# shellcheck disable=SC2179 # PROMPT_COMMAND is not an array in bash <= 5.0
PROMPT_COMMAND+=${__bp_install_string}
}

Expand Down
10 changes: 5 additions & 5 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lade-sdk"
version = "0.8.1-beta.1"
version = "0.8.1"
edition = "2021"
description = "Lade SDK"
license = "MPL-2.0"
Expand All @@ -15,12 +15,12 @@ futures = "0.3.28"
itertools = "0.11.0"
log = "0.4.19"
once_cell = "1.18.0"
regex = "1.9.1"
regex = "1.9.3"
rust-ini = "0.19.0"
serde = { version = "1.0.180", features = ["derive"] }
serde = { version = "1.0.183", features = ["derive"] }
serde_json = "1.0.104"
serde_yaml = "0.9.25"
tempfile = "3.7.0"
tokio = { version = "1.29.1", features = ["fs"] }
tempfile = "3.7.1"
tokio = { version = "1.30.0", features = ["fs"] }
toml = "0.7.6"
url = "2.4.0"
9 changes: 8 additions & 1 deletion sdk/src/providers/onepassword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ impl Provider for OnePassword {
let loaded =
serde_json::from_slice::<Hydration>(&child.stdout).map_err(|err| {
let stderr = String::from_utf8_lossy(&child.stderr);
anyhow!("1Password error: {err} (stderr: {stderr})",)
if stderr.contains("could not resolve item UUID") {
anyhow!(
"One item does not seem to exist in the vault: {stderr}",
)
} else {
anyhow!("1Password error: {err} (stderr: {stderr})",)
}

})?;

let hydration = vars
Expand Down
6 changes: 5 additions & 1 deletion src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ impl Shell {
let user = directories::UserDirs::new().expect("cannot get HOME location");
let home_dir = user.home_dir();
let curr_exe = std::env::current_exe().expect("cannot get current executable path");
let command = format!("eval \"$({} on)\"", curr_exe.display());
let command = match self {
Shell::Bash => format!("source <(echo \"$({} on)\")", curr_exe.display()),
Shell::Zsh => format!("eval \"$({} on)\"", curr_exe.display()),
Shell::Fish => format!("eval \"$({} on)\"", curr_exe.display()),
};
let marker = "lade-do-not-edit".to_string();
let config_file = match self {
Shell::Bash => home_dir.join(".bashrc"),
Expand Down

0 comments on commit 2a417fc

Please sign in to comment.