-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.go
173 lines (148 loc) · 3.57 KB
/
build.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
package reco
import (
"bytes"
"encoding/json"
"errors"
"io"
"strings"
"time"
"github.com/ReconfigureIO/reco/logger"
"github.com/ReconfigureIO/reco/printer"
humanize "github.com/dustin/go-humanize"
)
var _ Job = &buildJob{}
// BuildReporter can return build reports.
type BuildReporter interface {
// Report returns a formatted JSON build report.
Report(id string) (string, error)
}
type buildJob struct {
*clientImpl
}
func (b buildJob) prepareBuild(message string) (string, error) {
projectID, err := b.projectID()
if err != nil {
return "", err
}
req := b.apiRequest(endpoints.builds.String())
reqBody := M{"project_id": projectID, "message": message}
resp, err := req.Do("POST", reqBody)
if err != nil {
return "", err
}
var respJSON struct {
Value struct {
ID string `json:"id"`
} `json:"value"`
Error string `json:"error"`
}
err = decodeJSON(resp.Body, &respJSON)
return respJSON.Value.ID, err
}
func (b buildJob) Start(args Args) (string, error) {
srcDir := String(args.At(0))
wait := Bool(args.At(1))
message := String(args.At(2))
logger.Info.Println("preparing build")
id, err := b.prepareBuild(message)
if err != nil {
return "", err
}
logger.Info.Println("done. Build ID: ", id)
logger.Info.Println("archiving")
srcArchive, err := archiveDir(srcDir)
if err != nil {
return "", err
}
logger.Info.Println("done")
logger.Info.Println("uploading")
if err := b.uploadJob("build", id, srcArchive); err != nil {
return "", err
}
logger.Info.Println("done")
logger.Info.Println()
if wait {
b.waitAndLog("build", id)
}
return id, nil
}
func (b buildJob) Status(id string) string {
return b.clientImpl.getStatus("build", id)
}
func (b buildJob) Stop(id string) error {
return b.clientImpl.stopJob("build", id)
}
func (b buildJob) List(filter M) (printer.Table, error) {
var table printer.Table
allProjects := filter.Bool("all")
builds, err := b.clientImpl.listBuilds(filter)
if err != nil {
return table, err
}
var body [][]string
for _, build := range builds {
buildTime := "-"
if !build.Time.IsZero() {
buildTime = humanize.Time(build.Time)
}
row := []string{
build.ID,
build.Status,
buildTime,
timeRounder(build.Duration).Nearest(time.Second),
build.Message,
}
if allProjects {
row = append(row, build.Project)
}
body = append(body, row)
}
table = printer.Table{
Header: []string{"build id", "status", "started", "duration", "message"},
Body: body,
}
if allProjects {
table.Header = append(table.Header, "project")
}
return table, nil
}
func (b buildJob) Log(id string, writer io.Writer) error {
return b.clientImpl.logJob("build", id)
}
type buildReport struct {
Report string `json:"report"`
}
func (b buildJob) Report(id string) (string, error) {
var req = b.apiRequest(endpoints.builds.Report())
req.param("id", id)
resp, err := req.Do("GET", nil)
if err != nil {
return "", err
}
switch resp.StatusCode {
case 404:
return "", errors.New("Report not found")
case 204:
return "", errors.New("No report generated. Reports are only generated for COMPLETED builds")
case 200:
break
default:
return "", errors.New("Unknown error occured")
}
var apiResp struct {
Value struct {
Report string `json:"report"`
} `json:"value"`
}
err = json.NewDecoder(resp.Body).Decode(&apiResp)
if err != nil {
return "", err
}
validJSONReport := strings.Replace(apiResp.Value.Report, "\\", "", -1)
var prettyJSONReport bytes.Buffer
err = json.Indent(&prettyJSONReport, []byte(validJSONReport), "", "\t")
if err != nil {
return "", err
}
return prettyJSONReport.String(), nil
}