-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (53 loc) · 1.36 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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
func main() {
user := os.Args[1]
project := os.Args[2]
buildNumber := os.Args[3]
binaryName := os.Args[4]
destination := os.Args[5]
token := os.Args[6]
req, _ := http.NewRequest("GET", fmt.Sprintf("https://circleci.com/api/v1.1/project/github/%s/%s/%s/artifacts?circle-token=%s", user, project, buildNumber, token), nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var artifacts []Artifact
if err := json.Unmarshal(body, &artifacts); err != nil {
panic(err)
}
for _, artifact := range artifacts {
if artifact.PrettyPath == fmt.Sprintf("$CIRCLE_ARTIFACTS/%s", binaryName) {
binary, err := http.Get(fmt.Sprintf("%s?circle-token=%s", artifact.Url, token))
if err != nil {
panic(err)
}
defer binary.Body.Close()
out, err := os.Create(fmt.Sprintf("%s/%s", destination, binaryName))
if err != nil {
panic(err)
}
defer out.Close()
io.Copy(out, resp.Body)
}
return
}
}
type Artifact struct {
Path string `json:"path"`
PrettyPath string `json:"pretty_path"`
NodeIndex int `json:"node_index"`
Url string `json:"url"`
}