-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitflux-util
76 lines (61 loc) · 1.3 KB
/
gitflux-util
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
#!/usr/bin/env bash
has() {
local item=$1; shift
grep -q "$(escape "$item")" <<< "$@"
}
is() {
[[ -n $1 ]]
}
escape() {
sed 's/\([\.\$\*]\)/\\\1/g' <<< "$1"
}
is_available() {
type "$1" >/dev/null 2>&1
}
is_os_unix() {
# linux / mac osx
[[ $OSTYPE = "linux-gnu" || $OSTYPE = "darwin"* ]]
}
is_os_msys() {
# mingw
[[ $OSTYPE = "msys" ]]
}
set_executable() {
if is_os_unix; then
chmod +x "$1" >/dev/null 2>&1
fi
}
create_temp_dir() {
mktemp -d 2>/dev/null || mktemp -d -t 'git-flux'
}
# responsibly handle cleanup of the temporary directories
cleanup_dir_on_exit() {
trap 'rm -rf '"$@"' >/dev/null 2>&1' EXIT
}
url_last_path_seg() {
sed -e 's,.*[/:]\([^/]*\)/[^/]*$,\1,'
}
url_domain() {
sed -e 's,[^:/]*://\([^/]*\)/.*$,\1,'
}
json_get() {
json_select '["'"$1"'"]'
}
json_select() {
local selector="$1"
# check node is available
if hash node 2>/dev/null; then
local src='
var data=JSON.parse(process.argv[1] || "{}");
var val=data'"$selector"';
console.log(typeof val === "undefined" ? "" : typeof val === "string" ? val : JSON.stringify(val));
'
node -e "$src" "$(cat -)" 2>/dev/null
fi
}
# simple implementation: only escapes '\', '"' and new-lines
json_escape_string() {
local str="$1"
str="$(echo "$str" | sed -e '0N' -e 's,\(["\]\),\\\1,g')"
printf "%s" "${str//$'\n'/\n}"
}