summaryrefslogtreecommitdiffhomepage
path: root/scripts/gist
blob: e83b3c2928bc0e01085d53264892d0914ea54cb0 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env bash
#
# Author: Hsieh Chin Fan (typebrook) <typebrook@gmail.com>
# License: MIT
# https://gist.github.com/typebrook/b0d2e7e67aa50298fdf8111ae7466b56
#
#
# This script host your gists as local cloned git repo
# It works under GNU with jq and curl, both are easy to get in most cases
#
# Use the following commands to manage your gists:
# 
# * update the local list of your gists, star for your starred gists
# gist (update | u) [star | s] 
# 
# * list your gists with format: [number] [url] [file_num] [comment_num] [short description]
# gist [star | s]
# 
# * clone gist repos which are not in local
# * pull master branch if a local repo is behind its remote
# gist (sync | S) 
#
# * Go to local gist repo
# . gist <number_of_gist_in_list> 
#
# * create a new gist with files
# gist (new | n) [-d | --desc "<gist-description>"] <files>...
#
# * create a new gist with STDIN
# gist (new | n) [-d | --desc "<gist-description>"] [-f | --file <file>] < <file-with-content>
#
# * show the detail of a gist
# gist (detail | d) <gist_index>
#
# * edit a gist description
# gist (edit | e) <gist_index>
#
# * delete a gist
# gist (delete | D) <gist_index>...
#
# * clean removed gists in local
# gist (clean | C)
# 
# * update a gist 
# Since now a gist is a local cloned repo
# It is your business to do git commit and git push
#
# * show this help message
# gist (help | h)

# TODO pipe syntax fix
# TODO error handling, unit test
# TODO parallel branch works with json parsing on python
# TODO parallel branch works with wget and other stuff
# TODO completion

# Validate settings.
config=~/.config/gistrc
[ "$TRACE" ] && set -x

# TODO error handling while password is not true
_auth() {
    data="{\"scopes\":[\"gist\"], \"note\": \"gist-$(date -u +'%Y-%m-%dT%H:%M:%SZ')\"}"
    read -p "Github username: " user
    read -sp "Github password: " password
    mkdir -p ~/.config && umask 0077 && echo user=$user > $config

    curl https://api.github.com/authorizations \
        --user "$user:$password" \
        --data "$data" > /dev/null

    read -p "2-factor code: " OTP
    curl https://api.github.com/authorizations \
        --user "$user:$password" -H "X-GitHub-OTP: $OTP" \
        --data "$data" |\
    sed '1 s/[^{]//g' | jq -r .token |\
    sed 's/^/token=/' >> $config
}

while ! source $config; do
    _auth
done

github_api=https://api.github.com
auth_header="Authorization: token $token"

[[ -z "$folder" ]] && folder=~/git/gist
mkdir -p $folder
index=$folder/index
starred=$folder/starred

# Show the list of gist, but not updated time
# TODO a way to show files
# TODO show git status outdated
_show_list() {
    if [[ ! -e "$1" ]]; then 
        echo No local file found for last update
        echo Please run command: 
        echo "    gist update"
        exit 0
    fi
    cat $1 |\
    while read line_num link file_url_array file_num extra description; do
        repo=$folder/$(echo $link | sed 's#.*/##')

        # if repo is not yet cloned, show green message "Sync Now"
        cd $repo 2>/dev/null || extra="\e[32m[Sync Now]\e[0m"
        # if there are some changes in git index or working directory, show blue message "working"
        [[ -n $(git status --short) ]] 2>/dev/null && extra="\e[36m[working]\e[0m" 
        # if there is a commit not yet push, show red message "ahead"
        [[ -n $(git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" 

        echo -e $line_num $link $file_num $extra $(echo $description | cut -c -60)
    done
}

# get the list of gists
# TODO support secret gist
_update() {
    echo "fetching from api.github.com..."
    echo
    list_file=$index
    route="users/$user/gists"
    mark=""
    [[ "$1" =~ ^(star|s)$ ]] && list_file=$starred && route="gists/starred" && mark="s"

    curl -s -H "$auth_header" $github_api/$route |\
    _parse_response | nl -s' ' | sed -E "s/^ */$mark/" > $list_file && \
    _show_list $list_file
    if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi
}

# TODO check if a user create a very first gist
_parse_response() {
    jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' |\
    tac |\
    while read link file_url_array file_num comment_num description; do
        blob_code=$(echo $file_url_array | jq -r '.[]' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-')
        echo $link $blob_code $file_num $comment_num $description | tr -d '"'
    done
}

_sync_repos() {
    list_file=$index
    [[ "$1" == "--star" ]] && list_file=$starred && route="gists/starred"

    # clone repos which are not in the local
    comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \
             <(cat $list_file | cut -d' ' -f2 | sed 's#.*/##' | sort) |\
    xargs -I{} git clone git@github.com:{}.git $folder/{}

    # pull if remote repo has different blob objects
    cat $index | cut -d' ' -f2,3 |\
    while read url blob_code_remote; do
        repo=$folder/$(echo $url | sed 's#.*/##')
        blob_code_local=$(cd $repo && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-')

        cd $repo && \
        [[ $blob_code_local != $blob_code_remote ]] && \
        [[ $(git rev-parse origin/master) == $(git rev-parse master) ]] && \
        git pull
    done
    echo Everything is fine!
}

_gist_id() {
    GIST_ID=$(cat $index $starred | sed -n "/^$1 / p" | cut -d' ' -f2 | sed -E 's#.*/##')
    if [[ -z "$GIST_ID" ]]; then
        echo -e "Not a valid index: \e[31m$1\e[0m"
        echo Use the index number in the first column instead:
        echo
        _show_list "$index $starred"
        exit 1
    fi
}

# FIXME error handling, if repo not cloned yet
_goto_gist() {
    _gist_id $1
    echo This gist is at $folder/$GIST_ID
    echo -e "You can run the following command to jump to this directory: \n"
    echo -e "    \e[32m. gist $1\e[0m"
    echo 
    cd $folder/$GIST_ID && ls && tig --all 2> /dev/null
}

_delete_gist() {
    for i in "$@"; do
        _gist_id "$i"
        curl -X DELETE -s -H "$auth_header" $github_api/gists/$GIST_ID && \
        echo "$i" deleted
    done
    _update
}

# remove repos which are not in user gists anymore
_clean_repos() {
    comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \
             <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\
    while read dir; do
        mv $folder/$dir /tmp && echo move $folder/$dir to /tmp
    done
}

# TODO format with simple text
_show_detail() {
    _gist_id $1
    curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\
    jq '{site: .html_url, description: .description, public: .public, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}'

    curl -s -H "$auth_header" $github_api/gists/$GIST_ID/comments |\
    jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}'
}

_set_gist() {
    while [[ "$1" =~ ^- && "$1" != "--" ]]; do case $1 in
        -d | --desc)
          description="$2"
          shift; shift;;
        -f | --file)
          filename="$2"
          shift; shift;;
      esac
    done
    if [[ "$1" == '--' ]]; then shift; fi
    files="$@" && echo $files | xargs ls > /dev/null || exit 1
}

_new_file() {
    [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty
    tmp_file=$(mktemp)
    cat > $tmp_file
    echo -e '\n' > /dev/tty
    [[ -z "$1" ]] && echo -n 'Type file name: ' > /dev/tty && read filename
    mv $tmp_file /tmp/$filename
    echo /tmp/$filename
}

# create a new gist with files
# FIXME error handling if gist is not created, file doesn't exist
_create_gist() {
    _set_gist "$@"
    [[ -z "$files" ]] && files=$(_new_file $filename)
    [[ -z "$description" ]] && echo -n 'Type description: ' && read description

    for file in $files; do
        FILE=$(basename $file)
        jq --arg FILE "$FILE" '. as $content | { ($FILE): {content: $content} }' -Rs $file
    done |\
    jq --slurp --arg DESC "$description" '{
        public: true,
        files: add,
        description: ($DESC)
    }' |\
    curl -H "$auth_header" --data @- $github_api/gists |\
    sed '1 s/^/[/; $ s/$/]/' |\
    _parse_response |\
    sed -E "s/^/$(( $(wc -l $index | cut -d' ' -f1) + 1 )) /" >> $index && \
    echo -e '\nGist created'
    _show_list $index | tail -1
}

# update description of a gist
_edit_gist() {
    _gist_id $1

    echo -n 'Type new description: '
    read DESC
    jq -n --arg DESC "$DESC" '{ description: ($DESC) }' |\
    curl -X PATCH -H "$auth_header" --data @- $github_api/gists/$GIST_ID > /dev/null && \
    _update
}

_help_message() {
    sed -E -n ' /^$/ q; 8,$ s/^#//p' $0
}

_cases() {
    if [[ $1 == 'token' ]]; then
        [[ ${#2} -eq 40 ]] && echo token=$2 ||\
        echo Invalid token format, it is not 40 chars '\n' > /dev/tty
    elif [[ $1 == 'auto_sync' ]]; then
        echo $1=true
    elif [[ $1 == 'folder' ]]; then
        echo $1=$2 
    elif [[ $1 == 'user' ]]; then
        echo $1=$2
    else
        echo $1=
    fi
}

# TODO consider the case that $2 is empty -> remove original setting
_configure() {
    [[ -z "$@" ]] && (vim $config) && exit 0
    target=$(_cases "$@")

    [[ -n $target ]] && sed -i "/^$1=/ d" $config && echo $target >> $config
    cat $config
}


case "$1" in
  "")
    _show_list $index ;;
  star | s)
    _show_list $starred ;;
  update | u)
    _update "$2" ;;
  new | n)
    shift
    _create_gist "$@" ;;
  edit | e)
    _edit_gist "$2" ;;
  sync | S)
    _sync_repos ;;
  detail | d)
    shift
    _show_detail "$@" ;;
  delete | D)
    shift
    _delete_gist "$@" ;;
  clean | C)
    _clean_repos ;;
  config | c)
    shift
    _configure "$@" ;;
  help | h)
    _help_message ;;
  *)
    _goto_gist "$1" ;;
esac