Skip to content

Commit 20fb116

Browse files
author
Artur Kh
committed
Add a rawify functionality
1 parent d1b3eb4 commit 20fb116

File tree

3 files changed

+76
-22
lines changed

3 files changed

+76
-22
lines changed

gist.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package main
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"io/ioutil"
78
"net/http"
89
"regexp"
910
)
1011

1112
const (
12-
GistURI = "gist.github.com"
13-
GitHubAPIURL = "https://api.github.com"
13+
DefaultGistName = "a"
14+
GistURI = "gist.github.com"
15+
GitHubAPIURL = "https://api.github.com"
1416
)
1517

1618
type File struct {
@@ -53,12 +55,11 @@ func (gist *Gist) Create(anonymous bool) (string, error) {
5355
return "", err
5456
}
5557

56-
var resp Gist
57-
if err := json.Unmarshal(body, &resp); err != nil {
58+
if err := json.Unmarshal(body, &gist); err != nil {
5859
return "", err
5960
}
6061

61-
return resp.HtmlUrl, nil
62+
return gist.HtmlUrl, nil
6263
}
6364

6465
func (gist *Gist) Update(uid string) (string, error) {
@@ -118,6 +119,34 @@ func GistList() ([]*Gist, error) {
118119
return resp, nil
119120
}
120121

122+
func (gist *Gist) Rawify() (string, error) {
123+
if gist.HtmlUrl == "" {
124+
return "", errors.New("HTML URL is empty.")
125+
}
126+
127+
req, _ := http.NewRequest("GET", gist.HtmlUrl, nil)
128+
client := &http.Transport{}
129+
resp, err := client.RoundTrip(req)
130+
if err != nil {
131+
return "", err
132+
}
133+
134+
defer resp.Body.Close()
135+
136+
var url string
137+
138+
if resp.StatusCode == 200 {
139+
url = gist.HtmlUrl + "/raw"
140+
} else if resp.StatusCode == 302 {
141+
gist.HtmlUrl = resp.Header.Get("location")
142+
if url, err = gist.Rawify(); err != nil {
143+
return "", err
144+
}
145+
}
146+
147+
return url, nil
148+
}
149+
121150
func doRequest(req *http.Request) ([]byte, error) {
122151
req.Header.Add("Content-type", "application/json")
123152

go-gist.go

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import (
1111
)
1212

1313
const (
14-
VERSION = "0.1"
15-
DefaultGistName = "a"
14+
VERSION = "0.1"
1615
)
1716

1817
var config = NewConfig()
@@ -34,32 +33,37 @@ func main() {
3433
// -t EXT --type=EXT Sets the file extension and syntax type.
3534
// -p --private Indicates whether the gist is private.
3635
// -d DESC --description=DESC Adds a description to your gist.
37-
// -s --shorten *Shorten the gist URL using git.io.
36+
// -s --shorten Shorten the gist URL using git.io.
3837
// -u URL --update=URL Update an existing gist.
3938
// -a --anonymous Create an anonymous gist.
4039
// -c --copy Copy the resulting URL to the clipboard.
41-
// -e --embed *Copy the embed code for the gist to the clipboard.
42-
// -o --open *Open the resulting URL in a browser.
40+
// -e --embed Copy the embed code for the gist to the clipboard.
41+
// -o --open Open the resulting URL in a browser.
4342
// -P --paste Paste from the clipboard to gist.
44-
// -R --raw *Display raw URL of the new gist.
43+
// -R --raw Display raw URL of the new gist.
4544
// -l USER --list=USER Lists all gists for a user.
4645
// -h --help Show this help message and exit.
4746
// --version Show version and exit.`
4847
//
4948
// arguments, err := docopt.Parse(usage, nil, true, "go-gist "+VERSION, false)
5049
// fmt.Println(arguments)
5150

52-
anonymousFlag := flag.Bool("a", false, "Create an anonymous gist.")
53-
copyFlag := flag.Bool("c", false, "Copy the resulting URL to the clipboard.")
54-
listFlag := flag.Bool("l", false, "List gists.")
5551
loginFlag := flag.Bool("login", false, "Authenticate gist on this computer.")
56-
pasteFlag := flag.Bool("P", false, "Paste from the clipboard to gist.")
57-
privateFlag := flag.Bool("p", false, "Indicates whether the gist is private.")
58-
59-
desc := flag.String("d", "", "A description of the gist.")
6052
filename := flag.String("f", "", "Sets the filename and syntax type.")
6153
filetype := flag.String("t", "", "Sets the file extension and syntax type.")
54+
privateFlag := flag.Bool("p", false, "Indicates whether the gist is private.")
55+
desc := flag.String("d", "", "A description of the gist.")
56+
shortenFlag := flag.Bool("-s", false, "Shorten the gist URL using git.io.")
6257
uid := flag.String("u", "", "Update an existing gist. Takes ID as an argument.")
58+
anonymousFlag := flag.Bool("a", false, "Create an anonymous gist.")
59+
copyFlag := flag.Bool("c", false, "Copy the resulting URL to the clipboard.")
60+
// -e --embed Copy the embed code for the gist to the clipboard.
61+
// -o --open Open the resulting URL in a browser.
62+
pasteFlag := flag.Bool("P", false, "Paste from the clipboard to gist.")
63+
rawFlag := flag.Bool("R", false, "Display a raw URL of the new gist.")
64+
listFlag := flag.Bool("l", false, "List gists.")
65+
66+
versionFlag := flag.Bool("version", false, "Show version and exit.")
6367

6468
flag.Parse()
6569

@@ -69,8 +73,10 @@ func main() {
6973
err = login()
7074
} else if *listFlag {
7175
err = list()
76+
} else if *versionFlag {
77+
fmt.Println("go-gist " + VERSION)
7278
} else {
73-
err = gist(*uid, *desc, *filetype, *filename, !*privateFlag, *anonymousFlag, *copyFlag, *pasteFlag)
79+
err = gist(*uid, *desc, *filetype, *filename, !*privateFlag, *anonymousFlag, *copyFlag, *pasteFlag, *rawFlag, *shortenFlag)
7480
}
7581

7682
if err != nil {
@@ -106,7 +112,7 @@ func list() error {
106112
return nil
107113
}
108114

109-
func gist(uid, desc, filetype, filename string, public, anonymous, copyFlag, pasteFlag bool) error {
115+
func gist(uid, desc, filetype, filename string, public, anonymous, copyFlag, pasteFlag, rawFlag, shortenFlag bool) error {
110116
var err error
111117
var clipboard *Clipboard
112118

@@ -125,7 +131,9 @@ func gist(uid, desc, filetype, filename string, public, anonymous, copyFlag, pas
125131
return err
126132
}
127133

128-
if filetype != "" {
134+
if filename != "" {
135+
name = filename
136+
} else if filetype != "" {
129137
name += "." + filetype
130138
}
131139

@@ -137,7 +145,7 @@ func gist(uid, desc, filetype, filename string, public, anonymous, copyFlag, pas
137145

138146
if filename != "" {
139147
name = filename
140-
} else if filename == "" && filetype != "" {
148+
} else if filetype != "" {
141149
name = DefaultGistName + "." + filetype
142150
} else {
143151
name = DefaultGistName
@@ -148,6 +156,7 @@ func gist(uid, desc, filetype, filename string, public, anonymous, copyFlag, pas
148156
return err
149157
}
150158
} else {
159+
fmt.Println("(type a gist. <ctrl-c> to cancel, <ctrl-d> when done)")
151160
if content, err = ioutil.ReadAll(os.Stdin); err != nil {
152161
return err
153162
}
@@ -168,6 +177,18 @@ func gist(uid, desc, filetype, filename string, public, anonymous, copyFlag, pas
168177
return err
169178
}
170179

180+
if rawFlag {
181+
if url, err = gist.Rawify(); err != nil {
182+
return err
183+
}
184+
}
185+
186+
if shortenFlag {
187+
if url, err = Shorten(url); err != nil {
188+
return err
189+
}
190+
}
191+
171192
if copyFlag {
172193
if err := clipboard.Copy(url); err != nil {
173194
return err

util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ func GistParseError(body []byte) error {
3030

3131
return errors.New(message)
3232
}
33+
34+
func Shorten(url string) (string, error) {
35+
return "", nil
36+
}

0 commit comments

Comments
 (0)