-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathutils-template.go
123 lines (107 loc) · 2.65 KB
/
utils-template.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
package common
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
type DownloadFile struct {
URL string `json:"url"`
Name string `json:"name"`
}
type TemplateDefinition struct {
Version string `json:"version"`
Dockerfiles []DownloadFile `json:"dockerfiles"`
ServiceYmls []DownloadFile `json:"service-ymls"`
DockerComposeYmls []DownloadFile `json:"docker-compose-ymls"`
BundleManifest []DownloadFile `json:"bundle-manifest-jsons"`
}
func Fetch(url string, mod *time.Time) (io.ReadCloser, error) {
PrintlnL2("Downloading from %s", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if mod != nil {
req.Header.Add("If-Modified-Since", mod.Format(http.TimeFormat))
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if mod != nil && resp.StatusCode == 304 {
return nil, errors.New("item moved")
}
if resp.StatusCode != 200 {
err := fmt.Errorf("bad http status from %s: %v", url, resp.Status)
return nil, err
}
if s := resp.Header.Get("Last-Modified"); mod != nil && s != "" {
t, err := time.Parse(http.TimeFormat, s)
if err == nil {
*mod = t
}
}
return resp.Body, nil
}
func FetchJSON(url string, mod *time.Time, v interface{}) error {
r, err := Fetch(url, mod)
if err != nil {
return err
}
defer r.Close()
return json.NewDecoder(r).Decode(v)
}
func DownloadTemplates(tempDir string, td TemplateDefinition, templatePath string, flagBranch string) error {
err := DownloadSingleFile(tempDir, DownloadFile{URL: strings.Replace(templatePath, "{{.branch}}", flagBranch, -1), Name: "templates.json"}, flagBranch)
if err != nil {
return err
}
for _, temp := range td.Dockerfiles {
err := DownloadSingleFile(tempDir, temp, flagBranch)
if err != nil {
return err
}
}
for _, temp := range td.ServiceYmls {
err := DownloadSingleFile(tempDir, temp, flagBranch)
if err != nil {
return err
}
}
for _, temp := range td.DockerComposeYmls {
err := DownloadSingleFile(tempDir, temp, flagBranch)
if err != nil {
return err
}
}
for _, temp := range td.BundleManifest {
err := DownloadSingleFile(tempDir, temp, flagBranch)
if err != nil {
return err
}
}
return nil
}
func DownloadSingleFile(tempDir string, temp DownloadFile, flagBranch string) error {
r, err := Fetch(strings.Replace(temp.URL, "{{.branch}}", flagBranch, -1), nil)
if err != nil {
return err
}
defer r.Close()
output, err := os.Create(filepath.Join(tempDir, temp.Name))
if err != nil {
return err
}
defer output.Close()
_, err = io.Copy(output, r)
if err != nil {
return err
}
return nil
}