-
Notifications
You must be signed in to change notification settings - Fork 1
/
driller
executable file
·165 lines (137 loc) · 3.82 KB
/
driller
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
#!/usr/bin/env bash
set -e
VERSION="v0.3.4.1"
function usage() {
cat <<EOF
USAGE:
$(basename -- $0) [OPTION] <dir>
OPTIONS:
-a, --absolute Absolute path (default).
-b, --basename Strip directory from path.
-D, --dereference Recursively find the ultimate target of a symlink.
-d, --dirname Strip file from path.
-h, --help This help.
-s, --scm-root Find the SCM root (git or hg) for file.
--version Version information.
EOF
exit 0
}
function version() {
echo ${VERSION}
exit 0
}
function fatal() {
echo "Fatal: $1" > /dev/stderr
exit 1
}
function __strip_extraneous_slashes() {
${REAL_PATH} -m -s -- "$1"
}
function __check_link_loop() {
if [[ -L $1 ]]; then
${REAL_PATH} -q "$1" &> /dev/null || fatal "Symlink loop."
fi
return 0
}
function __decorate_path() {
# add a trailing slash if it's a dir.
# remove trailing slashes if it's a file.
if [[ -d $1 ]] && [[ ! -L $1 ]]; then
path=$(echo $1 | sed -E 's#/*$#/#g')
else
path=$(echo $1 | sed -E 's#/*$##g')
fi
echo ${path}
}
function __dereference() {
__check_link_loop "$1"
working_dir=$(dirname -- "$1")
pushd ${working_dir} &> /dev/null
path=$(readlink -- $1 || echo $1)
path=$(${REAL_PATH} -- "${path}")
popd &> /dev/null
__decorate_path ${path}
}
function __clean_path() {
path=$1
path=$(__expand_tilde "${path}")
path=$(__strip_extraneous_slashes "${path}")
path=$(__decorate_path "${path}")
echo ${path}
}
function __dirname() {
path=$1
[[ ! -d "${path}" ]] && path=$(dirname -- "${path}")
__clean_path "${path}"
}
function __filename() {
basename -- "$(__clean_path "$1")"
}
function __scm_dir() {
[[ -d "$1/.hg" || -d "$1/.git" ]] && return 0
}
function __scm_root() {
path=$(__dereference $1)
dir=$(__dirname ${path})
while true; do
[[ "${dir}" == "/" ]] && fatal "SCM dir not found"
__scm_dir "${dir}" && break
dir="$(__clean_path "${dir}/../")"
done
echo $dir
}
function __user_home() {
user=$1
if [[ $(uname) == "Darwin" ]]; then
dscacheutil -q user -a name ${user} | grep ^dir: | cut -d\ -f2-
else
cat /etc/passwd | grep ^${user} | cut -d: -f6
fi
}
function __expand_tilde() {
# Expansion of ~ is usually done by your shell. This function does it too.
path=$1
user_section=$(echo ${path} | grep -o '^~[^\/]*')
[[ -z ${user_section} ]] && echo ${path} && return 0
path_section=$(echo ${path} | grep -o '\/.*$')
if [[ ${user_section} == "~" ]]; then
user=${USER}
else
user=$(echo ${user_section} | sed 's/~//g')
fi
echo "$(__user_home ${user})${path_section}"
}
REAL_PATH=$(which realpath || which grealpath || fatal "realpath command not found")
[[ -z $@ ]] && usage
PATH_ARGS=""
OPTION="--absolute"
PATH_ARGS=()
for i in $(seq 1 $#); do
eval a=\$$i
a=$(__expand_tilde "$a")
if [[ -e "$a" ]] || [[ -L "$a" ]]; then
PATH_ARGS+=( "$(__clean_path "$a")" )
elif [[ "$a" =~ ^--[a-z\-]+$ ]]; then
OPTION=$a
elif [[ "$a" =~ ^-[a-z]$ ]]; then
OPTION=$a
fi
done
if [[ -p /dev/stdin ]]; then
PATH_ARGS+=( "$(__clean_path $(cat /dev/stdin))" )
fi
for element in $(seq 0 $((${#PATH_ARGS[@]} - 1))); do
path=${PATH_ARGS[$element]}
[[ -z "${path}" ]] && break
case "${OPTION}" in
"--absolute"|"-a") echo "$path" ;;
"--basename"|"-b") [[ ! -d "$path" ]] && __filename "$path" ;;
"--dereference"|"-D") __dereference "$path" ;;
"--dirname"|"-d") __dirname "$path" ;;
"--scm"|"--scm-root"|"-s") __scm_root "$path" ;;
esac
done
case "${OPTION}" in
"--help") usage ;;
"--version") version ;;
esac