Skip to content

Commit 4218ca3

Browse files
committed
first release
1 parent 26d3a5d commit 4218ca3

10 files changed

Lines changed: 611 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
go-app
2+
13
# Compiled Object files, Static and Dynamic libs (Shared Objects)
24
*.o
35
*.a

api.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"io/ioutil"
8+
"net/http"
9+
"os"
10+
)
11+
12+
const (
13+
API_URL = "https://api.github.com"
14+
)
15+
16+
/* create a new request that sends the auth token */
17+
func NewAuthRequest(method, url, bodyType, token string, body io.Reader) (*http.Request, error) {
18+
vprintln("creating request:", method, url, bodyType, token)
19+
20+
req, err := http.NewRequest(method, url, body)
21+
if err != nil {
22+
return nil, err
23+
}
24+
25+
if body != nil {
26+
switch v := body.(type) {
27+
case *os.File:
28+
/* apparently chunking doesnt work yet...
29+
* vprintln("OS.FILE detected, chunking!", v)
30+
* req.TransferEncoding = []string{"chunked"} */
31+
32+
/* then we explicitly read the file... (let's hope it's not stdin) */
33+
off, err := GetFileSize(v)
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
req.ContentLength = off
39+
vprintln("setting content-length to", off)
40+
}
41+
}
42+
43+
req.Header.Set("Content-Type", bodyType)
44+
req.Header.Set("Authorization", fmt.Sprintf("token %s", token))
45+
46+
return req, nil
47+
}
48+
49+
func DoAuthRequest(method, url, bodyType, token string, body io.Reader) (*http.Response, error) {
50+
req, err := NewAuthRequest(method, url, bodyType, token, body)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
resp, err := http.DefaultClient.Do(req)
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
return resp, nil
61+
}
62+
63+
func GithubGet(uri string, v interface{}) error {
64+
resp, err := http.Get(API_URL + uri)
65+
if err != nil {
66+
return fmt.Errorf("could not fetch releases, %v", err)
67+
}
68+
defer resp.Body.Close()
69+
70+
vprintln("GET", API_URL+uri, "->", resp)
71+
72+
if resp.StatusCode != http.StatusOK {
73+
return fmt.Errorf("github did not response with 200 OK but with %v", resp.Status)
74+
}
75+
76+
if VERBOSITY == 0 {
77+
if err = json.NewDecoder(resp.Body).Decode(v); err != nil {
78+
return fmt.Errorf("could not unmarshall JSON into Release struct, %v", err)
79+
}
80+
} else {
81+
body, err := ioutil.ReadAll(resp.Body)
82+
vprintln("BODY", string(body))
83+
84+
if err = json.Unmarshal(body, v); err != nil {
85+
return fmt.Errorf("could not unmarshall JSON into Release struct, %v", err)
86+
}
87+
}
88+
89+
return nil
90+
}

assets.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
type Asset struct {
4+
Url string `json:"url"`
5+
Id int `json:"id"`
6+
Name string `json:"name"`
7+
}

commit.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
type Commit struct {
4+
Sha string `json:"sha"`
5+
Url string `json:"url"`
6+
}

error.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"strings"
8+
)
9+
10+
/* usually when something goes wrong, github sends something like this back */
11+
type Message struct {
12+
Message string `json:"message"`
13+
Errors []GithubError `json:"errors"`
14+
}
15+
16+
type GithubError struct {
17+
Resource string `json:"resource"`
18+
Code string `json:"code"`
19+
Field string `json:"field"`
20+
}
21+
22+
/* transforms a stream into a Message, if it's valid json */
23+
func ToMessage(r io.Reader) (*Message, error) {
24+
var msg Message
25+
if err := json.NewDecoder(r).Decode(&msg); err != nil {
26+
return nil, err
27+
}
28+
29+
return &msg, nil
30+
}
31+
32+
func (m *Message) String() string {
33+
str := fmt.Sprintf("msg: %v, errors: ", m.Message)
34+
35+
errstr := make([]string, len(m.Errors))
36+
for idx, err := range m.Errors {
37+
errstr[idx] = fmt.Sprintf("[field: %v, code: %v]",
38+
err.Field, err.Code)
39+
}
40+
41+
return str + strings.Join(errstr, ", ")
42+
}

file.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
func GetFileSize(f *os.File) (int64, error) {
9+
/* first try stat */
10+
off, err := fsizeStat(f)
11+
if err != nil {
12+
/* if that fails, try seek */
13+
return fsizeSeek(f)
14+
}
15+
16+
return off, nil
17+
}
18+
19+
func fsizeStat(f *os.File) (int64, error) {
20+
fi, err := f.Stat()
21+
22+
if err != nil {
23+
return 0, err
24+
}
25+
26+
return fi.Size(), nil
27+
}
28+
29+
func fsizeSeek(f *os.File) (int64, error) {
30+
off, err := f.Seek(0, 2)
31+
if err != nil {
32+
return 0, fmt.Errorf("seeking did not work, stdin is not" +
33+
"supported yet because github doesn't support chunking" +
34+
"requests (and I haven't implemented detecting stdin and" +
35+
"buffering yet")
36+
}
37+
38+
_, err = f.Seek(0, 0)
39+
if err != nil {
40+
return 0, fmt.Errorf("could not seek back in the file")
41+
}
42+
return off, nil
43+
}

0 commit comments

Comments
 (0)