Use withenv script

This commit is contained in:
Teajey 2026-02-20 23:49:42 +09:00
parent a4146d1421
commit 297ec2780b
Signed by: Teajey
GPG Key ID: 970E790FE834A713
4 changed files with 97 additions and 1 deletions

View File

@ -2,4 +2,4 @@ top_level=$(git rev-parse --show-toplevel)
git_version=$($top_level/scripts/git-version) git_version=$($top_level/scripts/git-version)
go generate ../core/internal/db go generate ../core/internal/db
go run -ldflags=-X=lishwist/http/env.GitVersion=$git_version main.go ../scripts/withenv .env .env.local .env.dev .env.dev.local -- go run -ldflags=-X=lishwist/http/env.GitVersion=$git_version main.go

15
scripts/show Executable file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
if [ $# -eq 0 ]; then
echo "Usage: $0 <file> [file2] ..." >&2
exit 1
fi
for file in "$@"; do
if [ ! -f "$file" ]; then
echo "Error: File not found: $file" >&2
exit 1
fi
printf '\n\n\n ## FROM %s\n\n' "$file"
cat "$file"
done

30
scripts/upfind Executable file
View File

@ -0,0 +1,30 @@
#!/usr/bin/env bash
set -euo pipefail
if [[ $# -eq 0 ]]; then
echo "Usage: upfind <glob> [glob...]" >&2
exit 1
fi
dir="$(pwd)"
dirs=()
while true; do
dirs+=("$dir")
parent="$(dirname "$dir")"
if [[ "$parent" == "$dir" ]]; then
break
fi
dir="$parent"
done
for (( i=${#dirs[@]}-1; i>=0; i-- )); do
for glob in "$@"; do
for match in "${dirs[$i]}"/$glob; do
if [[ -e "$match" ]]; then
echo "$match"
fi
done
done
done

51
scripts/withenv Executable file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env bash
if [ $# -eq 0 ]; then
echo "Usage: withenv <env-file> [env-file2] ... -- <command> [args]" >&2
exit 1
fi
script_dir=$(dirname "$0")
env_files=()
cmd_args=()
parsing_files=true
for arg in "$@"
do
if [ "$arg" = "--" ]
then
parsing_files=false
continue
fi
if $parsing_files
then
env_files+=("$arg")
else
cmd_args+=("$arg")
fi
done
if [ ${#env_files[@]} -eq 0 ]; then
echo "Error: No env files provided" >&2
echo >&2
echo "Usage: withenv <env-file> [env-file2] ... -- <command> [args]" >&2
exit 1
fi
if [ ${#cmd_args[@]} -eq 0 ]; then
echo "Error: No command specified after --" >&2
echo >&2
echo "Usage: withenv <env-file> [env-file2] ... -- <command> [args]" >&2
exit 1
fi
data=$($script_dir/upfind "${env_files[@]}" | xargs $script_dir/show) || exit $?
(
set -a
eval "$data"
set +a
exec "${cmd_args[@]}"
)