Skip to content

Commit c94acf0

Browse files
committed
Clarifies that glob pattern should be enclosed in quotes
This is to avoid the shell expanding the glob pattern before it arrives to Go land. This commit also does some code cleaning.
1 parent 64e60c5 commit c94acf0

1 file changed

Lines changed: 31 additions & 28 deletions

File tree

main.go

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ import (
2222
)
2323

2424
var (
25-
GithubToken string
26-
GithubUser string
27-
GithubRepo string
28-
GithubAPIEndpoint string
29-
Version string
30-
Debug bool
25+
githubToken string
26+
githubUser string
27+
githubRepo string
28+
githubAPIEndpoint string
29+
// Version gets initialized in compilation time.
30+
Version string
31+
debug bool
3132
)
3233

34+
// Release represents a Github Release.
3335
type Release struct {
3436
UploadURL string `json:"upload_url,omitempty"`
3537
TagName string `json:"tag_name"`
@@ -45,28 +47,29 @@ var verFlag bool
4547
func init() {
4648
log.SetFlags(0)
4749

48-
Debug, _ = strconv.ParseBool(os.Getenv("DEBUG"))
50+
debug, _ = strconv.ParseBool(os.Getenv("DEBUG"))
4951

50-
GithubToken = os.Getenv("GITHUB_TOKEN")
51-
GithubUser = os.Getenv("GITHUB_USER")
52-
GithubRepo = os.Getenv("GITHUB_REPO")
53-
GithubAPIEndpoint = os.Getenv("GITHUB_API")
52+
githubToken = os.Getenv("GITHUB_TOKEN")
53+
githubUser = os.Getenv("GITHUB_USER")
54+
githubRepo = os.Getenv("GITHUB_REPO")
55+
githubAPIEndpoint = os.Getenv("GITHUB_API")
5456

5557
flag.BoolVar(&verFlag, "version", false, "-version")
5658
flag.Parse()
5759
}
5860

59-
var usage string = `Github command line release tool.
61+
var usage = `Github command line release tool.
6062
6163
Usage:
62-
github-release <user/repo> <tag> <branch> <description> <files>
64+
github-release <user/repo> <tag> <branch> <description> "<files>"
6365
6466
Parameters:
6567
<user/repo>: Github user and repository
6668
<tag>: Used to created the release. It is also used as the release's name
6769
<branch>: Reference from where to create the provided <tag>, if it does not exist
6870
<description>: The release description
69-
<files>: Glob pattern describing the list of files to include in the release
71+
<files>: Glob pattern describing the list of files to include in the release.
72+
Make sure you enclose it in quotes to avoid the shell expanding the glob pattern.
7073
7174
Options:
7275
-version: Displays version
@@ -80,7 +83,7 @@ Environment variables:
8083
8184
Before using this tool make sure you set the environment variable GITHUB_TOKEN
8285
with a valid Github token and correct authorization scopes to allow you to create releases
83-
in your project. For more information about creating Github tokens please read the
86+
in your project. For more information about creating Github tokens please read the
8487
official Github documentation at https://help.github.com/articles/creating-an-access-token-for-command-line-use/
8588
8689
Author: https://github.com/c4milo
@@ -103,16 +106,16 @@ func main() {
103106
log.Fatal(usage)
104107
}
105108

106-
if GithubToken == "" {
109+
if githubToken == "" {
107110
log.Fatal(`Error: GITHUB_TOKEN environment variable is not set.
108111
Please refer to https://help.github.com/articles/creating-an-access-token-for-command-line-use/ for more help\n`)
109112
}
110113

111-
GithubUser = userRepo[0]
112-
GithubRepo = userRepo[1]
113-
GithubAPIEndpoint = fmt.Sprintf("https://api.github.com/repos/%s/%s", GithubUser, GithubRepo)
114+
githubUser = userRepo[0]
115+
githubRepo = userRepo[1]
116+
githubAPIEndpoint = fmt.Sprintf("https://api.github.com/repos/%s/%s", githubUser, githubRepo)
114117

115-
if Debug {
118+
if debug {
116119
log.Println("Glob pattern received: ")
117120
log.Println(os.Args[5])
118121
}
@@ -122,7 +125,7 @@ Please refer to https://help.github.com/articles/creating-an-access-token-for-co
122125
log.Fatalf("Error: Invalid glob pattern: %s\n", os.Args[5])
123126
}
124127

125-
if Debug {
128+
if debug {
126129
log.Println("Expanded glob pattern: ")
127130
log.Printf("%v\n", filepaths)
128131
}
@@ -156,16 +159,16 @@ func uploadFile(uploadURL, path string) {
156159
log.Printf("Error: %s\n", err.Error())
157160
}
158161

159-
if Debug {
162+
if debug {
160163
log.Println("========= UPLOAD RESPONSE ===========")
161164
log.Println(string(body[:]))
162165
}
163166
}
164167

165-
// Creates a Github Release, attaching the given files as release assets
166-
// If a release already exist, up in Github, this function will attempt to attach the given files to it
168+
// CreateRelease creates a Github Release, attaching the given files as release assets
169+
// If a release already exist, up in Github, this function will attempt to attach the given files to it.
167170
func CreateRelease(tag, branch, desc string, filepaths []string) {
168-
endpoint := fmt.Sprintf("%s/releases", GithubAPIEndpoint)
171+
endpoint := fmt.Sprintf("%s/releases", githubAPIEndpoint)
169172

170173
release := Release{
171174
TagName: tag,
@@ -224,12 +227,12 @@ func doRequest(method, url, contentType string, reqBody io.Reader, bodySize int6
224227
return nil, err
225228
}
226229

227-
req.Header.Set("Authorization", fmt.Sprintf("token %s", GithubToken))
230+
req.Header.Set("Authorization", fmt.Sprintf("token %s", githubToken))
228231
req.Header.Set("Content-type", contentType)
229232
req.Header.Set("Accept", "application/vnd.github.v3+json")
230233
req.ContentLength = bodySize
231234

232-
if Debug {
235+
if debug {
233236
log.Println("================ REQUEST DUMP ==================")
234237
dump, err := httputil.DumpRequestOut(req, true)
235238
if err != nil {
@@ -240,7 +243,7 @@ func doRequest(method, url, contentType string, reqBody io.Reader, bodySize int6
240243

241244
resp, err := http.DefaultClient.Do(req)
242245

243-
if Debug {
246+
if debug {
244247
log.Println("================ RESPONSE DUMP ==================")
245248
dump, err := httputil.DumpResponse(resp, true)
246249
if err != nil {

0 commit comments

Comments
 (0)