This repository has been archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.sh
182 lines (137 loc) · 4.36 KB
/
functions.sh
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
#!/usr/bin/env bash
# # # # # # # # # # # # # # # # # # # #
# get the users bash profile
get_bash_rc() {
# # # # # # # # # # # # # # # # # # # #
# oh-my-zsh installed
if [ -f "$HOME/.zshrc" ]; then
echo "$HOME/.zshrc"
# # # # # # # # # # # # # # # # # # # #
# mac OSX and .bash_profile
elif [ $GIST_SYSTEM = 'Darwin' ] && [ -f "$HOME/.bash_profile" ]; then
echo "$HOME/.bash_profile"
# # # # # # # # # # # # # # # # # # # #
# mac OSX and .profile
elif [ $GIST_SYSTEM = 'Darwin' ] && [ -f "$HOME/.profile" ]; then
echo "$HOME/.profile"
# # # # # # # # # # # # # # # # # # # #
# last try with the .bashrc
else
echo "$HOME/.bashrc"
fi
}
# # # # # # # # # # # # # # # # # # # #
# shows the help
help() {
# spaces until the commands descriptions starts
SPACE=" "
printf " ${BLUE}gist ${BOLD}v${GIST_VERSION}-beta${NORMAL}\n"
printf "\n"
printf " help: gist [command]\n"
printf "\n"
printf " COMMANDS:\n"
printf " ${BLUE}update${NORMAL}${SPACE:6}Update gist CLI.\n"
printf " ${BLUE}token${NORMAL}${SPACE:5}Set Github API token.\n"
printf " ${BLUE}search${NORMAL}${SPACE:6}Search your Github gists.${NORMAL}\n"
printf " ${BLUE}list${NORMAL}${SPACE:4}List your gists.${NORMAL}\n"
printf "\n"
}
# # # # # # # # # # # # # # # # # # # #
# get unix timestamp from file
filemtime() {
local SYSTEM=$(uname -s)
local TIMESTAMP=0
if [ "$SYSTEM" == "Darwin" ]; then
if stat -f %y $1 &> /dev/null; then
local CHANGED=$(stat -f %m $1)
elif php -v &> /dev/null; then
local CHANGED=$(php -r "echo filemtime('${1}');" 2> /dev/null)
fi
if [ ! -z "$CHANGED" ]; then
TIMESTAMP=$(expr $(date +%s) - $(printf "%.0f" $CHANGED))
fi
else
local CHANGED=$(stat -c %Y $1)
TIMESTAMP=$(expr $(date +%s) - $(printf "%.0f" $CHANGED))
fi
echo $TIMESTAMP
}
# # # # # # # # # # # # # # # # # # # #
# check the requirements
has_requirements() {
if ! jq -h &> /dev/null; then
printf "${RED}jq is missing on your system: https://github.com/stedolan/jq${NORMAL}\n"
exit
fi
validate_config
}
validate_config() {
if [ ! -f "$GIST_CONFIG" ]; then
echo '{ "token": null }' | jq -M . > $GIST_CONFIG
fi
}
get_token() {
local TOKEN=$(cat $GIST_CONFIG | jq -r '.token')
echo $TOKEN
}
set_token() {
if [ -z "$1" ]; then
read -p "Your token: " GIST_TOKEN
else
GIST_TOKEN=$1
fi
local JSON=$(cat $GIST_CONFIG | jq ".token = \"${GIST_TOKEN}\"" | jq -M .)
echo $JSON > $GIST_CONFIG
}
has_parameter() {
local PARAM=$1
shift
for var in "$@"; do
if [ "$PARAM" == "$var" ]; then
return 0
fi
done
return 1
}
#get_version() {}
#has_new_version() {}
#is_cache_expired() {}
fetch_gist_content() {
local URL="$1"
local TOKEN=$(get_token)
CONTENT=$(curl -s -H "Authorization: token ${TOKEN}" "$URL")
echo $CONTENT | jq -r '.files[(.files | keys[0])].content'
}
fetch_list() {
echo $(curl -s -H "Authorization: token ${TOKEN}" "${1}" -D /tmp/headers.txt 2>&1 | jq '.')
}
fetch_gists() {
if [ -f $GIST_CACHE_ALL ]; then
local CACHING_TIME=$(filemtime $GIST_CACHE_ALL)
else
local CACHING_TIME=0
fi
if [ $CACHING_TIME != 0 ] && [ $CACHING_TIME -gt 1800 ]; then
cat $GIST_CACHE_ALL
return 1
fi
local GCN_FILE=/tmp/gcn.json
local TOKEN=$(get_token)
local GISTS=$(fetch_list "https://api.github.com/gists")
# local GISTS=$(curl -s -H "Authorization: token ${TOKEN}" "https://api.github.com/gists" -D /tmp/headers.txt 2>&1 | jq '.')
local NEXT=$(cat /tmp/headers.txt | sed -n 's/^Link: <\(.*\)>; rel="next".*/\1/p')
echo $GISTS > $GIST_CACHE_ALL
while [ -n "$NEXT" ]; do
fetch_list $NEXT > $GCN_FILE
echo $(jq -s '.[0] + .[1]' $GIST_CACHE_ALL $GCN_FILE) > $GIST_CACHE_ALL
local NEXT=$(cat /tmp/headers.txt | sed -n 's/^Link: <\(.*\)>; rel="next".*/\1/p')
done
cat $GIST_CACHE_ALL
}
#get_cache() {}
#set_cache() {}
search_in_gists_for() {
local SEARCH=$1
local GISTS=$(fetch_gists)
echo $GISTS | jq --arg var "$1" '. | map( select(.description != null) ) | map( select(.description | contains( $var )) )'
}