-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirenvrc
288 lines (247 loc) · 7.9 KB
/
direnvrc
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env sh
set -eo pipefail
# This is the directory where all the recipes and ingredients are stored.
# By default, it is an empty string.
# If you want to use a different path, then override the default value with
# and absolute path name pointing to a direnv.d/ directory
CUPBOARD=""
# This is the directory where all the temporary locks are stored.
# By default, it is an empty string.
# If you want to use a different path, then override the default value with
# an absolute path name pointing to a directory writeable by $USER.
LOCKER_ROOM=""
# @FUNCTION: project
# @USAGE: project <name>
#
# @DESCRIPTION:
# This function expands the supplied <name> to the environment variable PROJECT.
#
# Example:
# $ cat ~/my/project/.envrc
# project darkstar
#
# $ direnv allow ~/my/project/.envrc
# direnv: loading ~/.direnvrc
# direnv: loading .envrc
# direnv: export +PROJECT
#
# $ env | grep PROJECT
# PROJECT=darkstar
function project() {
local name="${1:-isempty}"
# Exit if the project name is empty
if [[ "isempty" == ${name##*/} ]]; then
log_error "You must supply a valid project name when invoking project()!"
exit 1
fi
# Export PROJECT
export PROJECT="${name}"
log_status "setting project: ${name##*/}"
}
# @FUNCTION: concoct
# @USAGE: concoct <recipe>
#
# @DESCRIPTION:
# This function loads the supplied <recipe>.
function concoct() {
# Determine the location of the direnv.d directory
if [[ -z $( echo ${CUPBOARD} ) ]]; then
local direnvrc="$HOME/.direnvrc"
if [[ ! -L ${direnvrc} ]]; then
log_error "${direnvrc} is not a symbolic link!"
log_error "this Direnv recipe has not been installed by 'bestow'."
log_error "sorry, I cannot continue. Please, ask upstream for feedbacks:"
log_error "- https://github.com/Dr-Terrible/dotfiles-direnv"
exit 1
fi
CUPBOARD="$( readlink ${direnvrc} )"
CUPBOARD="$( dirname ${CUPBOARD} )/direnv.d"
fi
local cmd="${1}"
# Exit if the recipe name is empty
if [[ -z $( echo ${cmd} ) ]]; then
log_error "You must supply a valid recipe name when invoking concoct()!"
exit 1
fi
local recipe="${CUPBOARD}/recipes/${1}.direnv"
# If the recipe isn't readable, then exit
if [[ ! -r ${recipe} ]]; then
log_error "Unable to read recipe: ${cmd}!"
exit 1
fi
# Load the required recipe from direnv.d directory and guard it with
# a lock to avoid conflicting executions of the same commands.
shift
log_status "checking ${cmd}"
_include "${recipe}" "$@"
}
# @FUNCTION: require
# @USAGE: require <food container> <ingredient>
#
# @DESCRIPTION:
# This function loads an <ingredient> from a specific <food container>. If the
# <food container>, or the <ingredient>, doesn't exist, the function returns
# an error. This helper function is supposed to be used by recipes only.
#
# NOTE: Works like Direnv's use() function, except that the required external
# dependencies must be installed to allow Direnv to complete the set up of
# the environment.
function require() {
local container="${1:-empty}"
local cmd="${2:-empty}"
local ingredient="${CUPBOARD}/ingredients/${container}/${cmd}.direnv"
# Load the required ingredient from direnv.d directory, and guard it
# with a lock to avoid conflicting executions of the same commands.
if [[ ! -r ${ingredient} ]]; then
log_error "Unable to find ingredient: ${cmd}!"
log_error "No such file or directory: ${ingredient}"
exit 1
fi
_include "${ingredient}"
# Detect the presence of the specified command
if ! has ${cmd}; then
log_error "Unable to find command: ${cmd}!"
exit 1
fi
shift
shift
local DIRENV_LOG_FORMAT=" ---> %s"
usepkg ${cmd} "$@"
}
# Usage: log_status_nonewline [<message> ...]
#
# Logs a status message without the trailing newline char.
# Acts like echo, but wraps output in the standard direnv log format
# (controlled by $DIRENV_LOG_FORMAT), and directs it to stderr
# rather than stdout.
#
# Example:
#
# log_status_nonewline "Loading ..."
#
function log_status_nonewline() {
if [[ -n $DIRENV_LOG_FORMAT ]]; then
local msg=$*
# shellcheck disable=SC2059
printf "${DIRENV_LOG_FORMAT}" "$msg" >&2
fi
}
# Usage: usepkg <program_name> [<version>]
#
# A semantic command dispatcher intended for loading external dependencies into
# the environment.
#
# Example:
#
# use_go() {
# local version="$( go version | cut -d ' ' -f 3 )"
# printf "${version//go/v}\n"
# }
# usepkg go
# # output: go v1.9
#
function usepkg() {
local cmd=$1
log_status_nonewline "found $* "
shift
"use_$cmd" "$@"
printf "\n"
}
# @FUNCTION:
# @USAGE: make_dir <directory> ... <directoryN>
#
# @DESCRIPTION:
# This helper function creates the given directories,
# if they do not already exist.
function make_dir() {
for dir in $@; do
[[ -d ${dir} ]] && continue
mkdir -p ${dir}
printf " >>>> created: ${dir//${PWD}\//}/ \n"
done
}
# @FUNCTION:
# @USAGE: make_tmpfs <directory>
#
# @DESCRIPTION:
# This helper function creates a temporary directory that won't clash with other
# running envrc processes, and avoids race conditions if possible.
function make_tmpfs() {
for dir in $@; do
[[ -d ${dir} ]] && continue
# TODO: find a way to execute the mount without requiring root privileges.
# possibly, try to use systemd services for mounting points.
# for now we use symlinks to /tmp
#mount -t tmpfs none ${dir} -o 'rw,nosuid,nodev,noexec,noauto,nouser,owner,lazytime,async'
# Make sure $PROJECT exists
if [ -z $( echo "${PROJECT}" ) ]; then
log_error "Environment variable \$PROJECT is missing"
return -1
fi
# Make sure mktemp is available.
if ! command -pv mktemp >/dev/null ; then
log_error "Command 'mktemp' not found"
return -1
fi
# Make sure /tmp is writeable.
# TODO: /tmp is not cross-platform, and TMPDIR is a bash-isms (overwritten
# by os-project); we should rely on something else.
if [ ! -w "/tmp" ]; then
log_error "/tmp is not writeable"
return -1
fi
# Create the directory which will contain the template for mktemp.
local template="envrc-${USER}"
mkdir -p "/tmp/${template}"
# Create the temporary directory and symlink it into the local project.
local tempdir=$( mktemp --suffix="-${dir//${PWD}\//}" -d "/tmp/${template}/${PROJECT}-XXXXXXXX" )
ln -sf "${tempdir}" "${dir//${PWD}\//}"
printf " >>>> mounted ${dir//${PWD}\//} -> ${tempdir} \n"
done
}
# @FUNCTION: _include
# @USAGE: _include <file>
# @INTERNAL
#
# @DESCRIPTION:
# This helper function will read commands from <file> and execute them in the
# current shell environment.
#
# It's a thin wrapper around shell built-in command ".", with the addiction of
# a file locking mechanism to prevent concurrent execution of the entire
# content of the given <file>.
#
# Beware, the locking mechanism has a time-out of 60 seconds, after which the
# lock is released; this is necessary to avoid to stall infinitely in case the
# commands from <file> got stuck due to undefined behaviours.
#
# NOTE: By default, the locks are stored in "${XDG_RUNTIME_DIR}/direnv", but
# if the XDG environment variables are not available then it will fall back
# to "/tmp/direnv-$USER".
function _include() {
local file="${1}"
# Determine the location of the directory for storing the locks
if [[ -z $( echo "${LOCKER_ROOM}" ) ]]; then
# Try to determine a decent runtime path exclusive for the current user.
local runtime
if [[ -n "${XDG_RUNTIME_DIR}" ]]; then
runtime="${XDG_RUNTIME_DIR}/direnv/"
else
runtime="/tmp/direnv-$USER"
fi
LOCKER_ROOM="${runtime}/.locks/"
fi
# Prepare the lock
local lock_file="${file##*/}"
local lock_dir="${file//${lock_file}/}"
lock_dir="${LOCKER_ROOM}/$( basename ${lock_dir} )"
[ ! -d "${lock_dir}" ] && mkdir -p "${lock_dir}"
# Load the required <file> and guard it with a lock.
# NOTE: subsequent invocations of the same <file> from different shell
# sessions will queue up, creating a queue of processes.
exec 200>"${lock_dir}"/${lock_file}.lock
flock -w 60 -x 200
shift
. "${file}" "$@"
flock -u 200 # unlocking
}