Skip to content

Commit 3840650

Browse files
author
Artur Kh
committed
Add a shortenification
1 parent 20fb116 commit 3840650

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

go-gist.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func main() {
5353
filetype := flag.String("t", "", "Sets the file extension and syntax type.")
5454
privateFlag := flag.Bool("p", false, "Indicates whether the gist is private.")
5555
desc := flag.String("d", "", "A description of the gist.")
56-
shortenFlag := flag.Bool("-s", false, "Shorten the gist URL using git.io.")
56+
shortenFlag := flag.Bool("s", false, "Shorten the gist URL using git.io.")
5757
uid := flag.String("u", "", "Update an existing gist. Takes ID as an argument.")
5858
anonymousFlag := flag.Bool("a", false, "Create an anonymous gist.")
5959
copyFlag := flag.Bool("c", false, "Copy the resulting URL to the clipboard.")

util.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"net/http"
8+
neturl "net/url"
79
"strings"
810
)
911

12+
const GitIOURL = "http://git.io"
13+
1014
func GistParseError(body []byte) error {
1115
var err map[string]json.RawMessage
1216
if err := json.Unmarshal(body, &err); err != nil {
@@ -32,5 +36,15 @@ func GistParseError(body []byte) error {
3236
}
3337

3438
func Shorten(url string) (string, error) {
35-
return "", nil
39+
resp, err := http.PostForm(GitIOURL, neturl.Values{"url": {url}})
40+
if err != nil {
41+
return "", err
42+
}
43+
defer resp.Body.Close()
44+
45+
if resp.StatusCode == 201 {
46+
url = resp.Header.Get("Location")
47+
}
48+
49+
return url, nil
3650
}

0 commit comments

Comments
 (0)