-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgetgql
executable file
·149 lines (139 loc) · 4.06 KB
/
getgql
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
#!/bin/sh
get_shell() {
# Check environemnt shell in an attempt to detect
# user default shell, should work on most
# Linux distributions and on Darwin
local shell="unsupported"
local shell_ver="$($SHELL --version 2>/dev/null | head -1 | grep 'bash\|zsh')"
if [ "${shell_ver}" != "" ]; then
shell="$(echo "${shell_ver}" | grep bash >/dev/null 2>&1 \
&& echo bash || echo zsh)"
fi
echo "$shell"
}
SHELL="$(get_shell)"
VERSION="${VERSION:-v0.1.1}"
if [ "${COMPLETION:-x}" = x ]; then
COMPLETION="$([ "${SHELL}" != "unsupported" ] && echo yes || echo no)"
fi
PREFIX="${PREFIX:-/usr/local/bin}"
case $SHELL in
bash)
COMPLETION_FILE=$HOME/.bashrc
;;
zsh)
COMPLETION_FILE=$HOME/.zshrc
;;
esac
usage()
{
echo "Install gql command line tool"
echo "\t\t--prefix <path>\tinstall prefix (default: /usr/local/bin)"
echo "\t\t--no-completion\tdo not append completion for user (default: no)"
echo "\t\t--shell <bash|zsh> \tinstall completion for bash or zsh(default: no)"
}
# source: https://stackoverflow.com/a/14104522
check_write_perm() {
local user=$(whoami)
if [ "$user" = "root" ]; then
# Script is being run as root
echo yes
return
fi
# Use -L to get information about the target of a symlink,
# not the link itself, as pointed out in the comments
local info
case "$(uname -s)" in
Linux)
info="$(stat -L -c "%A %G %U" $1 2>/dev/null)"
;;
Darwin)
info="$(stat -L -f "%Sp %Sg %Su" $1 2>/dev/null)"
;;
*)
access=no
echo "${access}"
return
esac
local perm=$(echo "${info}" | cut -f 1 -d " " )
local group=$(echo "${info}" | cut -f 2 -d " " )
local owner=$(echo "${info}" | cut -f 3 -d " " )
local access=no
# TODO: sh seems to have issues comparing "-", for now grep
# will do, but maybe there's more elegant solution
if [ "$(echo $perm | cut -c 9-9 | grep "w")" = "w" ]; then
# Everyone has write access
access=yes
elif [ "$(echo $perm | cut -c 6-6 | grep "w")" = "w" ]; then
# Some group has write access.
# Is user in that group?
gs="$(groups)"
for g in $gs; do
if [ "$group" = "$g" ]; then
access=yes
break
fi
done
elif [ "$(echo $perm | cut -c 3-3 | grep "w")" = "w" ]; then
# The owner has write access.
# Does the user own the file?
[ "$user" = "$owner" ] && access=yes
fi
echo "$access"
}
install() {
if [ "$(check_write_perm ${PREFIX})" != "yes" ]; then
echo "you do not have permission to write in $PREFIX"
echo "consider running this script with sudo"
echo "or with PREFIX=/path/to/local/bin sh"
exit 1
fi
cd $PREFIX
curl -L \
https://github.com/slothking-online/gql/releases/download/${VERSION}/gql-$(uname -s)-amd64-${VERSION}.tar.gz | \
gzip -d | \
tar x
cd - > /dev/null
}
completion_install() {
if [ "${COMPLETION}" = "no" ]; then
return
fi
if [ "${COMPLETION_FILE}" = "no" ]; then
echo "file ${COMPLETION_FILE} does not exist, skipping"
return
fi
grep -e "^\. <(gql completion ${SHELL})$" $COMPLETION_FILE > /dev/null ||
(
echo "# Line below was generated by gql installer, DO NOT EDIT" >> ${COMPLETION_FILE} && \
echo ". <(gql completion ${SHELL})" >> ${COMPLETION_FILE} \
)
}
while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
case $PARAM in
-h | --help)
usage
exit
;;
--no-completion)
COMPLETION="no"
;;
--prefix)
PREFIX="$2"
shift
;;
--shell)
SHELL="$2"
shift
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
usage
exit 1
;;
esac
shift
done
install
completion_install