-
Notifications
You must be signed in to change notification settings - Fork 0
/
curlenv-switch
executable file
·50 lines (37 loc) · 1.39 KB
/
curlenv-switch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
#
# Usage: curlenv-switch <environment>
set -euo pipefail
log() { echo "curlenv-switch: $*"; }
log_err() { log "$*" 1>&2; }
[[ -x "$(command -v direnv)" ]] || (log_err "direnv not found" && exit 1)
readonly target_env="$1"
[[ -f env/${target_env}.envrc ]] || [[ -f env/${target_env}.env ]] || (log_err "unknown environment: $target_env" && exit 1)
link_file() {
local source_file="$1"
local dest_file="$2"
[[ -L "$dest_file" ]] && rm -f "$dest_file"
[[ -f "$source_file" ]] \
&& ln -sf "$source_file" "$dest_file" \
&& log "wrote $dest_file"
}
self_path=$(cd "$(dirname "$0")" && pwd -P)
project_path=$(realpath "$self_path/..")
# dotenv
curlenv_dotenv="$project_path/.env.curlenv"
curlenv_dotenv_target="${curlenv_dotenv}.target"
curlenv_dotenv_target_secret="${curlenv_dotenv_target}.secret"
# run-commands
curlenv_envrc_target="$project_path/.envrc.curlenv.target"
curlenv_envrc_target_secret="${curlenv_envrc_target}.secret"
# Write files
{
cat > "$curlenv_dotenv" <<-EOF
# DO NOT EDIT! Generated by curlenv-switch.
CURLENV_CURRENT="$target_env"
EOF
} && log "wrote $curlenv_dotenv"
link_file "env/${target_env}.env" "$curlenv_dotenv_target"
link_file "env/${target_env}.secret.env" "$curlenv_dotenv_target_secret"
link_file "env/${target_env}.envrc" "$curlenv_envrc_target"
link_file "env/${target_env}.secret.envrc" "$curlenv_envrc_target_secret"