forked from xwmx/bash-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.bash
222 lines (208 loc) · 4.92 KB
/
helpers.bash
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
#!/usr/bin/env bash
# _ _
# | |__ ___| |_ __ ___ _ __ ___
# | '_ \ / _ \ | '_ \ / _ \ '__/ __|
# | | | | __/ | |_) | __/ | \__ \
# |_| |_|\___|_| .__/ \___|_| |___/
# |_|
#
# Helper functions.
#
# These functions are primarily intended to be used within scripts. Each name
# starts with a leading underscore to indicate that it is an internal
# function and avoid collisions with gloablly defined names.
#
# Bash Boilerplate: https://github.com/alphabetum/bash-boilerplate
#
# Copyright (c) 2016 William Melody • hi@williammelody.com
###############################################################################
###############################################################################
# _command_exists()
#
# Usage:
# _command_exists <command-name>
#
# Returns:
# 0 If a command with the given name is defined in the current environment.
# 1 If not.
#
# Information on why `hash` is used here:
# http://stackoverflow.com/a/677212
_command_exists() {
hash "${1}" 2>/dev/null
}
###############################################################################
# _contains()
#
# Usage:
# _contains <item> <list>
#
# Example:
# _contains "$item" "${list[*]}"
#
# Returns:
# 0 If the item is included in the list.
# 1 If not.
_contains() {
local _test_list=(${*:2})
for __test_element in "${_test_list[@]:-}"
do
if [[ "${__test_element}" == "${1}" ]]
then
return 0
fi
done
return 1
}
###############################################################################
# _interactive_input()
#
# Usage:
# _interactive_input
#
# Returns:
# 0 If the current input is interactive (eg, a shell).
# 1 If the current input is stdin / piped input.
_interactive_input() {
[[ -t 0 ]]
}
###############################################################################
# _join()
#
# Usage:
# _join <separator> <array>
#
# Examples:
# _join , a "b c" d => a,b c,d
# _join / var local tmp => var/local/tmp
# _join , "${FOO[@]}" => a,b,c
#
# More Information:
# http://stackoverflow.com/a/17841619
_join() {
local IFS="${1}"
shift
printf "%s\n" "${*}"
}
###############################################################################
# _readlink()
#
# Usage:
# _readlink [-e|-f|<options>] <path/to/symlink>
#
# Options:
# -f All but the last component must exist.
# -e All components must exist.
#
# Wrapper for `readlink` that provides portable versions of GNU `readlink -f`
# and `readlink -e`, which canonicalize by following every symlink in every
# component of the given name recursively.
#
# More Information:
# http://stackoverflow.com/a/1116890
_readlink() {
local _target_path
local _target_file
local _final_directory
local _final_path
local _option
for __arg in "${@:-}"
do
case "${__arg}" in
-e|-f)
_option="${__arg}"
;;
-*|--*)
# do nothing
# ':' is bash no-op
:
;;
*)
if [[ -z "${_target_path:-}" ]]
then
_target_path="${__arg}"
fi
;;
esac
done
if [[ -z "${_option}" ]]
then
readlink "${@}"
else
if [[ -z "${_target_path:-}" ]]
then
printf "_readlink: missing operand\n"
return 1
fi
cd "$(dirname "${_target_path}")" || return 1
_target_file="$(basename "${_target_path}")"
# Iterate down a (possible) chain of symlinks
while [[ -L "${_target_file}" ]]
do
_target_file="$(readlink "${_target_file}")"
cd "$(dirname "${_target_file}")" || return 1
_target_file="$(basename "${_target_file}")"
done
# Compute the canonicalized name by finding the physical path
# for the directory we're in and appending the target file.
_final_directory="$(pwd -P)"
_final_path="${_final_directory}/${_target_file}"
if [[ "${_option}" == "-f" ]]
then
printf "%s\n" "${_final_path}"
return 0
elif [[ "${_option}" == "-e" ]]
then
if [[ -e "${_final_path}" ]]
then
printf "%s\n" "${_final_path}"
return 0
else
return 1
fi
else
return 1
fi
fi
}
###############################################################################
# _spinner()
#
# Usage:
# _spinner <pid>
#
# Description:
# Display an ascii spinner while <pid> is running.
#
# Example Usage:
# ```
# _spinner_example() {
# printf "Working..."
# (sleep 1) &
# _spinner $!
# printf "Done!\n"
# }
# (_spinner_example)
# ```
#
# More Information:
# http://fitnr.com/showing-a-bash-spinner.html
_spinner() {
local _pid="${1:-}"
local _delay=0.75
local _spin_string="|/-\\"
if [[ -z "${_pid}" ]]
then
printf "Usage: _spinner <pid>\n"
return 1
fi
while ps a | awk '{print $1}' | grep -q "${_pid}"
do
local _temp="${_spin_string#?}"
printf " [%c] " "${_spin_string}"
_spin_string="${_temp}${_spin_string%${_temp}}"
sleep ${_delay}
printf "\b\b\b\b\b\b"
done
printf " \b\b\b\b"
}