-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmudo
executable file
·122 lines (103 loc) · 2.71 KB
/
mudo
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
#! /bin/sh
# shellcheck shell=sh
#
# try to keep it bourne shell so it stays snappy
#
# shellcheck disable=SC2236
# shellcheck disable=SC2166
# shellcheck disable=SC2006
[ "${TRACE}" = 'YES' -o "${MUDO_TRACE}" = 'YES' ] && set -x && : "$0" "$@"
if [ $# -eq 0 ]
then
cat <<EOF >&2
usage:
mudo [-e|-f] <command> ...
mudo lets you execute commands with an unrestricted PATH from within a
mulle-env environment. It does not let you break out of a sandbox though.
With the -e option you can replace the current environment with the
unrestricted user environment.
With the -f option you can run mudo even if there is no current environment
making mudo basicaly a no-op. This can be useful in scripts.
See the difference by comparing \`env\` with \`mudo env\` and \`mudo -e env\`.
EOF
exit 1
fi
if [ -z "${MULLE_VIRTUAL_ROOT}" -o -z "${MULLE_HOSTNAME}" ]
then
if [ "$1" = "-f" ]
then
shift
exec "$@"
fi
echo "error: mudo must be run from within the mulle virtual environment" >&2
exit 1
fi
# we are in a virtual env, so if -f is given remove it
if [ "$1" = "-f" ]
then
shift
fi
if [ "$1" != "-e" ]
then
#
# does not work with builtins like `command -v ps`
#
if [ -z "${MULLE_OLDPATH}" ]
then
exec "$@"
else
PATH="${MULLE_OLDPATH}" exec "$@"
fi
exit $?
fi
# remove -e
shift
# we are in a virtual env, so if -f is given, remove it (do it one more time)
if [ "$1" = "-f" ]
then
shift
fi
# get mulle-env paths into this shell script environment like f.i.:
#
# MULLE_ENV_VAR_DIR='/Volumes/Source/srcM/mulle-cpp/.mulle/var/walitza/env'
# MULLE_ENV_ETC_DIR='/Volumes/Source/srcM/mulle-cpp/.mulle/etc/env'
# MULLE_ENV_SHARE_DIR='/Volumes/Source/srcM/mulle-cpp/.mulle/share/env'
#
# If this doesn't work we are in a "none" environment, presumably
if ! MULLE_ENV="${MULLE_ENV:-"`command -v mulle-env`"}"
then
echo "error: mulle-env not in PATH ($PATH)
Tip: mudo can not be used from a \"none\" environment." >&2
exit 1
fi
eval "`"${MULLE_ENV}" --search-as-is mulle-tool-env env`"
if [ -z "${MULLE_ENV_VAR_DIR}" ]
then
echo "error: mulle-env failed to provide MULLE_ENV_VAR_DIR" >&2
exit 1
fi
# TODO: should probably be in MULLE_ENV_TMP_DIR if that existed
filename="${MULLE_ENV_VAR_DIR}/old-environment.${MULLE_ENV_PID}"
environment="`cat "${filename}" 2> /dev/null`"
if [ -z "${environment}" ]
then
echo "error: mudo did not find a previous environment (${filename})" >&2
exit 1
fi
#
# does not work with builtins like `command -v ps`
#
OLDIFS="${IFS}"
IFS="
"
for line in ${environment}
do
set -- "${line}" "$@"
done
IFS="${OLDIFS}"
if [ -z "${MULLE_OLDPATH}" ]
then
exec env -i - "$@"
else
PATH="${MULLE_OLDPATH}" exec env -i - "$@"
fi