Skip to content

Commit 164ff3a

Browse files
kevinburke1kevinburke
authored andcommitted
all: use basic auth instead of token auth in querystring
Port the Client implementation to use github.com/kevinburke/rest, which gives us inspection of the HTTP request/response, user agents and basic auth out of the box, without needing to set those every time. Most of the rest of the code remains the same. Move the VERSION constant into the github subpackage (from the main package) so we can use it there. Fixes #95. Fixes #97.
1 parent ff3e066 commit 164ff3a

10 files changed

Lines changed: 173 additions & 69 deletions

File tree

LICENSE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014 Nicolas Hillegeer
3+
Copyright (c) 2014-2017 Nicolas Hillegeer
4+
Copyright (c) 2020 Meter, Inc.
45

56
Permission is hereby granted, free of charge, to any person obtaining a copy of
67
this software and associated documentation files (the "Software"), to deal in

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ Used libraries
120120
| [github.com/dustin/go-humanize](https://github.com/dustin/go-humanize) | humanize file sizes | MIT |
121121
| [github.com/tomnomnom/linkheader](https://github.com/tomnomnom/linkheader) | GH API pagination | MIT |
122122
| [github.com/voxelbrain/goptions](https://github.com/voxelbrain/goptions) | option parsing | BSD |
123+
| [github.com/kevinburke/rest](https://github.com/kevinburke/rest) | HTTP client | MIT |
123124

124125
Todo
125126
====
@@ -129,4 +130,5 @@ Todo
129130
Copyright
130131
=========
131132

132-
Copyright (c) 2014, Nicolas Hillegeer. All rights reserved.
133+
Copyright (c) 2014-2017, Nicolas Hillegeer. All rights reserved.
134+
Copyright (c) 2020, Meter, Inc. All rights reserved.

cmd.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
func infocmd(opt Options) error {
1818
user := nvls(opt.Info.User, EnvUser)
19+
authUser := nvls(opt.Info.AuthUser, EnvAuthUser)
1920
repo := nvls(opt.Info.Repo, EnvRepo)
2021
token := nvls(opt.Info.Token, EnvToken)
2122
tag := opt.Info.Tag
@@ -25,7 +26,7 @@ func infocmd(opt Options) error {
2526
}
2627

2728
// Find regular git tags.
28-
foundTags, err := Tags(user, repo, token)
29+
foundTags, err := Tags(user, repo, authUser, token)
2930
if err != nil {
3031
return fmt.Errorf("could not fetch tags, %v", err)
3132
}
@@ -52,14 +53,14 @@ func infocmd(opt Options) error {
5253
if tag == "" {
5354
// Get all releases.
5455
vprintf("%v/%v: getting information for all releases\n", user, repo)
55-
releases, err = Releases(user, repo, token)
56+
releases, err = Releases(user, repo, authUser, token)
5657
if err != nil {
5758
return err
5859
}
5960
} else {
6061
// Get only one release.
6162
vprintf("%v/%v/%v: getting information for the release\n", user, repo, tag)
62-
release, err := ReleaseOfTag(user, repo, tag, token)
63+
release, err := ReleaseOfTag(user, repo, tag, authUser, token)
6364
if err != nil {
6465
return err
6566
}
@@ -99,6 +100,7 @@ func renderInfoJSON(tags []Tag, releases []Release) error {
99100

100101
func uploadcmd(opt Options) error {
101102
user := nvls(opt.Upload.User, EnvUser)
103+
authUser := nvls(opt.Upload.AuthUser, EnvAuthUser)
102104
repo := nvls(opt.Upload.Repo, EnvRepo)
103105
token := nvls(opt.Upload.Token, EnvToken)
104106
tag := opt.Upload.Tag
@@ -118,7 +120,7 @@ func uploadcmd(opt Options) error {
118120
}
119121

120122
// Find the release corresponding to the entered tag, if any.
121-
rel, err := ReleaseOfTag(user, repo, tag, token)
123+
rel, err := ReleaseOfTag(user, repo, tag, authUser, token)
122124
if err != nil {
123125
return err
124126
}
@@ -130,7 +132,9 @@ func uploadcmd(opt Options) error {
130132
// uploads (which regrettably happen often using the Github API). See
131133
// issue #26.
132134
var assets []Asset
133-
err = github.Client{Token: token, BaseURL: EnvApiEndpoint}.Get(fmt.Sprintf(ASSET_RELEASE_LIST_URI, user, repo, rel.Id), &assets)
135+
client := github.NewClient(authUser, token, nil)
136+
client.SetBaseURL(EnvApiEndpoint)
137+
err = client.Get(fmt.Sprintf(ASSET_RELEASE_LIST_URI, user, repo, rel.Id), &assets)
134138
if err != nil {
135139
return err
136140
}
@@ -204,6 +208,7 @@ func uploadcmd(opt Options) error {
204208

205209
func downloadcmd(opt Options) error {
206210
user := nvls(opt.Download.User, EnvUser)
211+
authUser := nvls(opt.Download.AuthUser, EnvAuthUser)
207212
repo := nvls(opt.Download.Repo, EnvRepo)
208213
token := nvls(opt.Download.Token, EnvToken)
209214
tag := opt.Download.Tag
@@ -220,9 +225,9 @@ func downloadcmd(opt Options) error {
220225
var rel *Release
221226
var err error
222227
if latest {
223-
rel, err = LatestRelease(user, repo, token)
228+
rel, err = LatestRelease(user, repo, authUser, token)
224229
} else {
225-
rel, err = ReleaseOfTag(user, repo, tag, token)
230+
rel, err = ReleaseOfTag(user, repo, tag, authUser, token)
226231
}
227232
if err != nil {
228233
return err
@@ -379,6 +384,7 @@ func releasecmd(opt Options) error {
379384
func editcmd(opt Options) error {
380385
cmdopt := opt.Edit
381386
user := nvls(cmdopt.User, EnvUser)
387+
authUser := nvls(cmdopt.AuthUser, EnvAuthUser)
382388
repo := nvls(cmdopt.Repo, EnvRepo)
383389
token := nvls(cmdopt.Token, EnvToken)
384390
tag := cmdopt.Tag
@@ -393,7 +399,7 @@ func editcmd(opt Options) error {
393399
return err
394400
}
395401

396-
id, err := IdOfTag(user, repo, tag, token)
402+
id, err := IdOfTag(user, repo, tag, authUser, token)
397403
if err != nil {
398404
return err
399405
}
@@ -456,9 +462,10 @@ func deletecmd(opt Options) error {
456462
nvls(opt.Delete.Repo, EnvRepo),
457463
nvls(opt.Delete.Token, EnvToken),
458464
opt.Delete.Tag
465+
authUser := nvls(opt.Delete.AuthUser, EnvAuthUser)
459466
vprintln("deleting...")
460467

461-
id, err := IdOfTag(user, repo, tag, token)
468+
id, err := IdOfTag(user, repo, tag, authUser, token)
462469
if err != nil {
463470
return err
464471
}

github-release.go

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@ type Options struct {
1818

1919
goptions.Verbs
2020
Download struct {
21-
Token string `goptions:"-s, --security-token, description='Github token ($GITHUB_TOKEN if set). required if repo is private.'"`
22-
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
23-
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
24-
Latest bool `goptions:"-l, --latest, description='Download latest release (required if tag is not specified)',mutexgroup='input'"`
25-
Tag string `goptions:"-t, --tag, description='Git tag to download from (required if latest is not specified)', mutexgroup='input',obligatory"`
26-
Name string `goptions:"-n, --name, description='Name of the file', obligatory"`
21+
Token string `goptions:"-s, --security-token, description='Github token ($GITHUB_TOKEN if set). required if repo is private.'"`
22+
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
23+
AuthUser string `goptions:"-a, --auth-user, description='Username for authenticating to the API (falls back to $GITHUB_AUTH_USER or $GITHUB_USER)'"`
24+
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
25+
Latest bool `goptions:"-l, --latest, description='Download latest release (required if tag is not specified)',mutexgroup='input'"`
26+
Tag string `goptions:"-t, --tag, description='Git tag to download from (required if latest is not specified)', mutexgroup='input',obligatory"`
27+
Name string `goptions:"-n, --name, description='Name of the file', obligatory"`
2728
} `goptions:"download"`
2829
Upload struct {
29-
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
30-
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
31-
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
32-
Tag string `goptions:"-t, --tag, description='Git tag to upload to', obligatory"`
33-
Name string `goptions:"-n, --name, description='Name of the file', obligatory"`
34-
Label string `goptions:"-l, --label, description='Label (description) of the file'"`
35-
File *os.File `goptions:"-f, --file, description='File to upload (use - for stdin)', rdonly, obligatory"`
36-
Replace bool `goptions:"-R, --replace, description='Replace asset with same name if it already exists (WARNING: not atomic, failure to upload will remove the original asset too)'"`
30+
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
31+
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
32+
AuthUser string `goptions:"-a, --auth-user, description='Username for authenticating to the API (falls back to $GITHUB_AUTH_USER or $GITHUB_USER)'"`
33+
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
34+
Tag string `goptions:"-t, --tag, description='Git tag to upload to', obligatory"`
35+
Name string `goptions:"-n, --name, description='Name of the file', obligatory"`
36+
Label string `goptions:"-l, --label, description='Label (description) of the file'"`
37+
File *os.File `goptions:"-f, --file, description='File to upload (use - for stdin)', rdonly, obligatory"`
38+
Replace bool `goptions:"-R, --replace, description='Replace asset with same name if it already exists (WARNING: not atomic, failure to upload will remove the original asset too)'"`
3739
} `goptions:"upload"`
3840
Release struct {
3941
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
@@ -49,6 +51,7 @@ type Options struct {
4951
Edit struct {
5052
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
5153
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
54+
AuthUser string `goptions:"-a, --auth-user, description='Username for authenticating to the API (falls back to $GITHUB_AUTH_USER or $GITHUB_USER)'"`
5255
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
5356
Tag string `goptions:"-t, --tag, obligatory, description='Git tag to edit the release of'"`
5457
Name string `goptions:"-n, --name, description='New name of the release (defaults to tag)'"`
@@ -57,17 +60,19 @@ type Options struct {
5760
Prerelease bool `goptions:"-p, --pre-release, description='The release is a pre-release'"`
5861
} `goptions:"edit"`
5962
Delete struct {
60-
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
61-
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
62-
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
63-
Tag string `goptions:"-t, --tag, obligatory, description='Git tag of release to delete'"`
63+
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
64+
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
65+
AuthUser string `goptions:"-a, --auth-user, description='Username for authenticating to the API (falls back to $GITHUB_AUTH_USER or $GITHUB_USER)'"`
66+
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
67+
Tag string `goptions:"-t, --tag, obligatory, description='Git tag of release to delete'"`
6468
} `goptions:"delete"`
6569
Info struct {
66-
Token string `goptions:"-s, --security-token, description='Github token ($GITHUB_TOKEN if set). required if repo is private.'"`
67-
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
68-
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
69-
Tag string `goptions:"-t, --tag, description='Git tag to query (optional)'"`
70-
JSON bool `goptions:"-j, --json, description='Emit info as JSON instead of text'"`
70+
Token string `goptions:"-s, --security-token, description='Github token ($GITHUB_TOKEN if set). required if repo is private.'"`
71+
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
72+
AuthUser string `goptions:"-a, --auth-user, description='Username for authenticating to the API (falls back to $GITHUB_AUTH_USER or $GITHUB_USER)'"`
73+
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
74+
Tag string `goptions:"-t, --tag, description='Git tag to query (optional)'"`
75+
JSON bool `goptions:"-j, --json, description='Emit info as JSON instead of text'"`
7176
} `goptions:"info"`
7277
}
7378

@@ -87,6 +92,9 @@ var (
8792
)
8893

8994
var (
95+
// The user whose token is being used to authenticate to the API. If unset,
96+
// EnvUser is used.
97+
EnvAuthUser string
9098
EnvToken string
9199
EnvUser string
92100
EnvRepo string
@@ -96,8 +104,13 @@ var (
96104
func init() {
97105
EnvToken = os.Getenv("GITHUB_TOKEN")
98106
EnvUser = os.Getenv("GITHUB_USER")
107+
EnvAuthUser = os.Getenv("GITHUB_AUTH_USER")
99108
EnvRepo = os.Getenv("GITHUB_REPO")
100109
EnvApiEndpoint = os.Getenv("GITHUB_API")
110+
111+
if EnvAuthUser == "" {
112+
EnvAuthUser = EnvUser
113+
}
101114
}
102115

103116
func main() {
@@ -106,7 +119,7 @@ func main() {
106119
goptions.ParseAndFail(&options)
107120

108121
if options.Version {
109-
fmt.Printf("github-release v%s\n", VERSION)
122+
fmt.Printf("github-release v%s\n", github.VERSION)
110123
return
111124
}
112125

0 commit comments

Comments
 (0)