Skip to content

Commit 3f0a5a4

Browse files
committed
Revert "github: remove unused functions"
This reverts commit 3c693ad. Mistakenly the outer functions were still pointing at the aktau/github package, which is why I didn't notice that I broke things. Update import statements to point at our repo instead.
1 parent 3c693ad commit 3f0a5a4

7 files changed

Lines changed: 98 additions & 5 deletions

File tree

assets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"net/http"
66
"time"
77

8-
"github.com/aktau/github-release/github"
8+
"github.com/meterup/github-release/github"
99
)
1010

1111
const (

cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"os"
1212
"strconv"
1313

14-
"github.com/aktau/github-release/github"
14+
"github.com/meterup/github-release/github"
1515
)
1616

1717
func infocmd(opt Options) error {

github-release.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"os"
66

7-
"github.com/aktau/github-release/github"
7+
"github.com/meterup/github-release/github"
88
"github.com/voxelbrain/goptions"
99
)
1010

github/file.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package github
22

33
import (
4+
"bytes"
5+
"errors"
46
"fmt"
7+
"io"
58
"os"
69
)
710

@@ -41,3 +44,36 @@ func fsizeSeek(f *os.File) (int64, error) {
4144
}
4245
return off, nil
4346
}
47+
48+
// materializeFile takes a physical file or stream (named pipe, user input,
49+
// ...) and returns an io.Reader and the number of bytes that can be read
50+
// from it.
51+
func materializeFile(f *os.File) (io.Reader, int64, error) {
52+
fi, err := f.Stat()
53+
if err != nil {
54+
return nil, 0, err
55+
}
56+
57+
// If the file is actually a char device (like user typed input)
58+
// or a named pipe (like a streamed in file), buffer it up.
59+
//
60+
// When uploading a file, you need to either explicitly set the
61+
// Content-Length header or send a chunked request. Since the
62+
// github upload server doesn't accept chunked encoding, we have
63+
// to set the size of the file manually. Since a stream doesn't have a
64+
// predefined length, it's read entirely into a byte buffer.
65+
if fi.Mode()&(os.ModeCharDevice|os.ModeNamedPipe) == 1 {
66+
vprintln("input was a stream, buffering up")
67+
68+
var buf bytes.Buffer
69+
n, err := buf.ReadFrom(f)
70+
if err != nil {
71+
return nil, 0, errors.New("req: could not buffer up input stream: " + err.Error())
72+
}
73+
return &buf, n, err
74+
}
75+
76+
// We know the os.File is most likely an actual file now.
77+
n, err := GetFileSize(f)
78+
return f, n, err
79+
}

github/github.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ const DefaultBaseURL = "https://api.github.com"
1919
// Set to values > 0 to control verbosity, for debugging.
2020
var VERBOSITY = 0
2121

22+
// DoAuthRequest ...
23+
//
24+
// TODO: This function is amazingly ugly (separate headers, token, no API
25+
// URL constructions, et cetera).
26+
func DoAuthRequest(method, url, mime, token string, headers map[string]string, body io.Reader) (*http.Response, error) {
27+
req, err := newAuthRequest(method, url, mime, token, headers, body)
28+
if err != nil {
29+
return nil, err
30+
}
31+
32+
resp, err := http.DefaultClient.Do(req)
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
return resp, nil
38+
}
39+
2240
// Client collects a few options that can be set when contacting the GitHub
2341
// API, such as authorization tokens. Methods called on Client will supply
2442
// these options when calling the API.
@@ -209,6 +227,45 @@ func (c Client) getPaginated(uri string) (io.ReadCloser, error) {
209227
return r, nil
210228
}
211229

230+
// Create a new request that sends the auth token.
231+
func newAuthRequest(method, url, mime, token string, headers map[string]string, body io.Reader) (*http.Request, error) {
232+
vprintln("creating request:", method, url, mime, token)
233+
234+
var n int64 // content length
235+
var err error
236+
if f, ok := body.(*os.File); ok {
237+
// Retrieve the content-length and buffer up if necessary.
238+
body, n, err = materializeFile(f)
239+
if err != nil {
240+
return nil, err
241+
}
242+
}
243+
244+
req, err := http.NewRequest(method, url, body)
245+
if err != nil {
246+
return nil, err
247+
}
248+
249+
// net/http automatically does this if req.Body is of type
250+
// (bytes.Reader|bytes.Buffer|strings.Reader). Sadly, we also need to
251+
// handle *os.File.
252+
if n != 0 {
253+
vprintln("setting content-length to", n)
254+
req.ContentLength = n
255+
}
256+
257+
if mime != "" {
258+
req.Header.Set("Content-Type", mime)
259+
}
260+
req.Header.Set("Authorization", "token "+token)
261+
262+
for k, v := range headers {
263+
req.Header.Set(k, v)
264+
}
265+
266+
return req, nil
267+
}
268+
212269
// nextLink returns the HTTP header Link annotated with 'next', "" otherwise.
213270
func nextLink(links linkheader.Links) string {
214271
for _, link := range links {

releases.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"strings"
66
"time"
77

8-
"github.com/aktau/github-release/github"
98
"github.com/dustin/go-humanize"
9+
"github.com/meterup/github-release/github"
1010
)
1111

1212
const (

tags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/aktau/github-release/github"
6+
"github.com/meterup/github-release/github"
77
)
88

99
const (

0 commit comments

Comments
 (0)