fogus (owner)

Revisions

gist: 42455 Download_button fork
public
Public Clone URL: git://gist.github.com/42455.git
Embed All Files: show embed
gist.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
#!/bin/sh
 
# to post `cat file | gist.sh`
# or `gist.sh < file`
# to get `gist.sh 1234`
 
log ()
{
  echo "$@" >&2
}
 
err_exit ()
{
  log "Error: $1"
  exit 1
}
 
require ()
{
  which $1 > /dev/null || err_exit "$0 requires '$1'"
}
 
help ()
{
  log 'Usage:
* Posting to GitHub:
$ cat file|gist.sh
or
$ gist.sh < file
 
* Getting from GitHub:
$ gist.sh 1234
'
}
 
gist_get ()
{
  URL="https://gist.github.com/$1.txt"
  GIST=$(curl -s $URL)
 
  log "* reading Gist from $URL\n\n"
  echo "$GIST"
}
 
gist_post ()
{
  log "* readin Gist from stdin"
 
  REQUEST_FILE=/tmp/gist.sh.req
  RESPONSE_FILE=/tmp/gist.sh.res
 
  #cleanup
  : > $RESPONSE_FILE
  : > $REQUEST_FILE
 
  # read file content
  #TODO: improve
  while read line; do
echo "$line" >> $REQUEST_FILE
  done
 
if [ ! -s $REQUEST_FILE ]; then
help && exit 0
  fi
curl http://gist.github.com/gists \
       -i \
       --silent \
       --data-urlencode private=on \
       --data-urlencode 'file_name[gistfile1]=by gist.sh' \
       --data-urlencode 'file_ext[gistfile1]=' \
       --data-urlencode "file_contents[gistfile1]@$REQUEST_FILE" \
       -o $RESPONSE_FILE
 
  LOCATION=$(cat $RESPONSE_FILE|sed -ne '/Location/p'|cut -f2- -d:)
  log "Gist location:$LOCATION"
}
 
require curl
 
case $1 in
  -h|--help)
    help
  ;;
  "")
    gist_post
  ;;
  *[a-zA-Z0-9]) # gist ID
    gist_get $1
  ;;
esac