-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenv-switch
executable file
·161 lines (133 loc) · 4.42 KB
/
env-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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/sh
set -e
set -u
fn_list_envs() { (
my_current="${1:-}"
my_config_dir="${2}"
cd ~/.config/"${my_config_dir}"/ || exit 1
{
echo "Available envs from ~/.config/${my_config_dir}/:"
echo ""
} > /dev/tty
my_first_env=
for my_env in *.env; do
if test "${my_env}" = "*.env"; then
{
echo " (none)"
echo ""
} > /dev/tty
echo >&2 "Error: no files match ~/.config/${my_config_dir}/*.env"
echo "" > /dev/tty
return 1
fi
if test "${my_env}" = "current.env"; then
continue
fi
if test -z "${my_first_env}"; then
my_first_env="${my_env}"
if test -z "${my_current}"; then
my_current="${my_env}"
my_current_new="${my_env}"
fi
fi
if test "${my_current}" = "${my_env}"; then
echo " ${my_env} (current)" > /dev/tty
else
echo " ${my_env}" > /dev/tty
fi
done
echo "" > /dev/tty
echo "${my_current_new:-}"
); }
main() { (
my_config_dir="${1:-}"
my_current_new="${2:-}"
echo ""
if test -z "${my_config_dir}" ||
test "${my_config_dir}" = "help" ||
test "${my_config_dir}" = "--help"; then
echo ""
echo "env-switch - switch the 'current.env' for any directory in ~/.config/"
echo ""
echo "USAGE"
echo " env-switch <config-dir> [env-name]"
echo ""
echo "EXAMPLES"
echo " env-switch 'proxmox-sh'"
echo " env-switch 'caddy-sh'"
echo " env-switch 'proxmox-sh' 'profile-1'"
echo " env-switch 'proxmox-sh' 'profile-2'"
echo ""
return 1
fi
if ! test -e ~/.config/"${my_config_dir}"/; then
{
echo "ERROR"
echo " not found: ~/.config/${my_config_dir}/"
echo ""
echo "SOLUTION"
echo " mkdir -p ~/.config/${my_config_dir}/"
echo ""
} >&2
return 1
fi
my_current=
if test -e ~/.config/"${my_config_dir}"/current.env; then
if ! test -L ~/.config/"${my_config_dir}"/current.env; then
my_ts="$(
date '+%F_%H.%M.%S'
)"
echo "Moving non-link ~/.config/${my_config_dir}/current.env to ~/.config/${my_config_dir}/${my_ts}.env"
mv ~/.config/"${my_config_dir}"/current.env ~/.config/"${my_config_dir}/${my_ts}.env"
fi
# shellcheck disable=SC2012
my_current="$(
ls -l ~/.config/"${my_config_dir}/current.env" |
cut -d '>' -f2 |
cut -d' ' -f2
)"
my_current="$(basename "${my_current}")"
fi
if test -n "${my_current_new}"; then
if ! test -e ~/.config/"${my_config_dir}/${my_current_new}"; then
if test -e ~/.config/"${my_config_dir}/${my_current_new}.env"; then
my_current_new="${my_current_new}.env"
fi
if test "current.env" = "${my_current_new}"; then
{
echo "ERROR"
echo " can't set 'current.env' to itself"
echo ""
} >&2
return 1
fi
if ! test -e ~/.config/"${my_config_dir}/${my_current_new}"; then
{
echo "ERROR"
echo " not found: ~/.config/${my_config_dir}/${my_current_new}"
echo ""
echo "SOLUTION"
echo " Pick one of the available envs"
echo ""
my_current_new="$(
fn_list_envs "${my_current}" "${my_config_dir}"
)"
} >&2
return 1
fi
fi
echo "Selecting '~/.config/${my_config_dir}/${my_current_new}' as 'current.env'"
ln -sf "./${my_current_new}" ~/.config/"${my_config_dir}/current.env"
echo ""
return 0
fi
my_current_new="$(
fn_list_envs "${my_current}" "${my_config_dir}"
)"
if test -n "${my_current_new}"; then
echo "Selecting '~/.config/${my_config_dir}/${my_current_new}' as 'current.env'"
ln -sf "./${my_current_new}" ~/.config/"${my_config_dir}"/current.env
fi
echo ""
); }
main "${@:-}"