-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
150 lines (137 loc) · 3.69 KB
/
main.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
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"strings"
"sync"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-tools/go-steputils/stepconf"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
// Config is config
type Config struct {
AccessToken stepconf.Secret `env:"github_access_token,required"`
Tag string `env:"tag,required"`
FileNames string `env:"source,required"`
RepositoryURL string `env:"repository_url,required"`
}
// Meta is meta
type Meta struct {
FileName string
FileURL string
}
func failf(format string, args ...interface{}) {
log.Errorf(format, args...)
os.Exit(1)
}
// formats:
// https://hostname/owner/repository.git
// git@hostname:owner/repository.git
// ssh://git@hostname:port/owner/repository.git
func parseRepo(url string) (host string, owner string, name string) {
url = strings.TrimSuffix(url, ".git")
var repo string
switch {
case strings.HasPrefix(url, "https://"):
url = strings.TrimPrefix(url, "https://")
idx := strings.Index(url, "/")
host, repo = url[:idx], url[idx+1:]
case strings.HasPrefix(url, "git@"):
url = url[strings.Index(url, "@")+1:]
idx := strings.Index(url, ":")
host, repo = url[:idx], url[idx+1:]
case strings.HasPrefix(url, "ssh://"):
url = url[strings.Index(url, "@")+1:]
host = url[:strings.Index(url, ":")]
repo = url[strings.Index(url, "/")+1:]
}
split := strings.Split(repo, "/")
return host, split[0], split[1]
}
func contains(arr []string, str string) bool {
for _, v := range arr {
if v == str {
return true
}
}
return false
}
func getRelease(
ctx context.Context,
client *github.Client,
tag string,
owner string,
repo string) (*github.RepositoryRelease, *github.Response, error) {
if tag == "latest" {
return client.Repositories.GetLatestRelease(ctx, owner, repo)
}
return client.Repositories.GetReleaseByTag(ctx, owner, repo, tag)
}
func createGitHubClient(ctx context.Context, c Config) (*github.Client) {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: string(c.AccessToken)},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
return client
}
func main() {
var c Config
if err := stepconf.Parse(&c); err != nil {
failf("Issue with input: %v", err)
}
stepconf.Print(c)
fileNames := strings.Split(c.FileNames, ",")
ctx := context.Background()
client := createGitHubClient(ctx, c)
_, owner, repo := parseRepo(c.RepositoryURL)
release, _, err := getRelease(ctx, client, c.Tag, owner, repo)
if err != nil {
failf("Failed to fetch release: %v", err)
}
var metas []Meta
assets := release.Assets
for _, asset := range assets {
if contains(fileNames, *asset.Name) {
metas = append(metas, Meta{asset.GetName(), asset.GetURL()})
}
}
if len(metas) < 1 {
failf("No such files on target release: %v", err)
}
wg := &sync.WaitGroup{}
for _, meta := range metas {
wg.Add(1)
go func(meta Meta) {
if req, err := http.NewRequest("GET", meta.FileURL, nil); err != nil {
failf("Failed to create request: %v", err)
} else {
req.Header.Add("Authorization", fmt.Sprintf("token %v", string(c.AccessToken)))
req.Header.Add("Accept", "application/octet-stream")
log.Infof("Downloading %v", meta.FileName)
dlClient := new(http.Client)
res, err := dlClient.Do(req)
if err != nil {
failf("Failed to download file: %v", err)
}
out, err := os.Create(meta.FileName)
if err != nil {
failf("Failed to create file: %v", err)
}
defer res.Body.Close()
_, err = io.Copy(out, res.Body)
if err != nil {
failf("Failed to create file: %v", err)
}
}
wg.Done()
}(meta)
}
wg.Wait()
log.Successf("Success to download files!")
os.Exit(0)
}