/
application.go
190 lines (137 loc) · 3.66 KB
/
application.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/google/go-github/github"
)
type fetcher interface {
Fetch() error
}
type writer func(format string, a ...interface{})
type application struct {
requestedAsset string
requestedOutput string
ownerAndRepo string
writer writer
client *github.Client
context context.Context
workingDir string
}
func newApplication(ownerAndRepo, requestedAsset, requestedOutput string) fetcher {
printlnWrapper := func(format string, a ...interface{}) {
fmt.Printf(format, a...)
}
return &application{
requestedAsset: requestedAsset,
requestedOutput: requestedOutput,
ownerAndRepo: ownerAndRepo,
writer: printlnWrapper,
client: github.NewClient(nil),
context: context.Background(),
workingDir: ".grm",
}
}
func (a *application) Fetch() error {
owner, repo, err := a.findOwnerAndRepo(a.ownerAndRepo)
if err != nil {
return err
}
release, _, err := a.client.Repositories.GetLatestRelease(a.context, owner, repo)
tag := release.GetTagName()
if err := a.assertTag(owner, repo, tag); err != nil {
return err
}
downloadURL, err := a.findDownloadURL(release)
if err != nil {
return err
}
output := a.findOutputPath(downloadURL)
a.writer("Downloading '%s' to %s...", downloadURL, output)
if err := a.downloadFile(output, downloadURL); err != nil {
return err
}
if err := a.writeTag(owner, repo, tag); err != nil {
return err
}
a.writer("Done.\n")
return nil
}
func (a *application) assertTag(owner, repo, tag string) error {
file := filepath.Join(a.workingDir, owner, repo)
// If the file exists and has content, check it
if content, err := ioutil.ReadFile(file); err == nil {
if string(content) == tag {
return fmt.Errorf("Already up to date, nothing to download")
}
}
return nil
}
func (a *application) writeTag(owner, repo, tag string) error {
file := filepath.Join(a.workingDir, owner, repo)
if err := os.MkdirAll(filepath.Dir(file), os.ModePerm); err != nil {
return err
}
out, err := os.Create(file)
if err != nil {
return err
}
defer out.Close()
if _, err := io.WriteString(out, tag); err != nil {
return fmt.Errorf("Failed to write tag to %s: %s", file, err)
}
return nil
}
func (a *application) findDownloadURL(release *github.RepositoryRelease) (string, error) {
if a.requestedAsset == "" {
return release.GetTarballURL(), nil
}
asset, err := a.findReleaseAsset(a.requestedAsset, release.Assets)
if err != nil {
return "", err
}
return asset.GetBrowserDownloadURL(), nil
}
func (a *application) findOutputPath(downloadURL string) (output string) {
if a.requestedOutput == "" {
parts := strings.Split(downloadURL, "/")
output = parts[len(parts)-1]
} else {
output = a.requestedOutput
}
return
}
func (a *application) findOwnerAndRepo(input string) (owner, repo string, err error) {
parts := strings.Split(input, "/")
if len(parts) != 2 {
return "", "", fmt.Errorf("Expected owner/repo format")
}
return parts[0], parts[1], nil
}
func (a *application) findReleaseAsset(name string, assets []github.ReleaseAsset) (github.ReleaseAsset, error) {
for _, asset := range assets {
if name == asset.GetName() {
return asset, nil
}
}
return github.ReleaseAsset{}, fmt.Errorf("Can't find '%s' in release assets", name)
}
func (a *application) downloadFile(filepath string, url string) (err error) {
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
return err
}