From 1204bace2c1187956900eb26eb39b4681b26bb80 Mon Sep 17 00:00:00 2001 From: Jason Swank <632526+jswank@users.noreply.github.com> Date: Fri, 23 Jan 2026 13:20:05 -0500 Subject: [PATCH] update direnv config --- direnv/.config/direnv/direnvrc | 19 ++---- direnv/.config/direnv/lib/export-secret.sh | 16 +++++ direnv/.config/direnv/lib/repo-overlay.sh | 73 ++++++++++++++++++++++ 3 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 direnv/.config/direnv/lib/export-secret.sh create mode 100644 direnv/.config/direnv/lib/repo-overlay.sh diff --git a/direnv/.config/direnv/direnvrc b/direnv/.config/direnv/direnvrc index df319ab..0eff220 100644 --- a/direnv/.config/direnv/direnvrc +++ b/direnv/.config/direnv/direnvrc @@ -1,15 +1,4 @@ -export_secret() { - local name="$1" - local file="/run/secrets/${name}" - - if [[ ! -f "$file" ]]; then - printf "secret file not found: %s\n" $file >&2 - return 1 - fi - - # Read first line, remove trailing spaces/tabs, convert nulls to newlines - local value - value=$(head -n1 "$file" | sed 's/[ \t]*$//' | tr '\0' '\n') - - export "${name}=${value}" -} +# Source library files in alphabetical order +for lib in ~/.config/direnv/lib/*.sh; do + [[ -f "$lib" ]] && source "$lib" +done diff --git a/direnv/.config/direnv/lib/export-secret.sh b/direnv/.config/direnv/lib/export-secret.sh new file mode 100644 index 0000000..c958bea --- /dev/null +++ b/direnv/.config/direnv/lib/export-secret.sh @@ -0,0 +1,16 @@ +# Exports a Podman / Docker secret as an environment variable +export-secret() { + local name="$1" + local file="/run/secrets/${name}" + + if [[ ! -f "$file" ]]; then + printf "secret file not found: %s\n" $file >&2 + return 1 + fi + + # Read first line, remove trailing spaces/tabs, convert nulls to newlines + local value + value=$(head -n1 "$file" | sed 's/[ \t]*$//' | tr '\0' '\n') + + export "${name}=${value}" +} diff --git a/direnv/.config/direnv/lib/repo-overlay.sh b/direnv/.config/direnv/lib/repo-overlay.sh new file mode 100644 index 0000000..54d67f1 --- /dev/null +++ b/direnv/.config/direnv/lib/repo-overlay.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +# repo_overlay - a direnv function to copy files from centralized overlays directory +# +# Set REPO_OVERLAY_DIR to customize the overlays base directory (default: $HOME/overlays) +# +# Usage: Add 'repo_overlay' to your .envrc file + +repo-overlay() { + # Get the repo URL from git remote + local repo_url + repo_url=$(git config --get remote.origin.url 2>/dev/null) + + if [ -z "$repo_url" ]; then + echo "repo_overlay: Not a git repository or no origin remote configured" + return 1 + fi + + # Extract owner and repo name from URL + # Handles both SSH (git@github.com:owner/repo.git) and HTTPS (https://github.com/owner/repo.git) + local owner_repo + if [[ "$repo_url" =~ ^git@[^:]+:(.+)\.git$ ]] || \ + [[ "$repo_url" =~ ^https?://[^/]+/(.+)\.git$ ]] || \ + [[ "$repo_url" =~ ^git@[^:]+:(.+)$ ]] || \ + [[ "$repo_url" =~ ^https?://[^/]+/(.+)$ ]]; then + owner_repo="${BASH_REMATCH[1]}" + else + echo "repo_overlay: Could not parse repository URL: $repo_url" + return 1 + fi + + # Determine overlay source directory + local overlay_base="${REPO_OVERLAY_DIR:-$HOME/overlays}" + local overlay_source="${overlay_base}/${owner_repo}" + + if [ ! -d "$overlay_source" ]; then + echo "repo_overlay: No overlay directory found at ${overlay_source}" + return 0 + fi + + # Copy files from overlay directory + local copied_count=0 + local skipped_count=0 + + # Find all files in overlay directory (excluding directories) + while IFS= read -r -d '' source_file; do + # Get relative path from overlay source + local rel_path="${source_file#$overlay_source/}" + local dest_file="./${rel_path}" + + if [ -f "$dest_file" ]; then + echo "repo_overlay: Skipping ${rel_path} (already exists)" + ((skipped_count++)) + else + # Create parent directory if needed + local dest_dir + dest_dir=$(dirname "$dest_file") + mkdir -p "$dest_dir" + + # Copy the file + cp "$source_file" "$dest_file" + echo "repo_overlay: Copied ${rel_path}" + ((copied_count++)) + fi + done < <(find "$overlay_source" -type f -print0) + + # Summary + if [ $copied_count -eq 0 ] && [ $skipped_count -eq 0 ]; then + echo "repo_overlay: No files found in ${overlay_source}" + else + echo "repo_overlay: Complete (${copied_count} copied, ${skipped_count} skipped)" + fi +}