Skip to content

Commit ef33fac

Browse files
author
Max Siegieda
authored
add ability to view build reports (#35)
* add ability to view build reports * no need to export apiResp * move newline, less verbose string cast
1 parent ad25ac8 commit ef33fac

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

build.go

+52
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package reco
22

33
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
47
"io"
8+
"strings"
59
"time"
610

711
"github.com/ReconfigureIO/reco/logger"
@@ -11,6 +15,12 @@ import (
1115

1216
var _ Job = &buildJob{}
1317

18+
// BuildReporter can return build reports.
19+
type BuildReporter interface {
20+
// Report returns a formatted JSON build report.
21+
Report(id string) (string, error)
22+
}
23+
1424
type buildJob struct {
1525
*clientImpl
1626
}
@@ -119,3 +129,45 @@ func (b buildJob) List(filter M) (printer.Table, error) {
119129
func (b buildJob) Log(id string, writer io.Writer) error {
120130
return b.clientImpl.logJob("build", id)
121131
}
132+
133+
type buildReport struct {
134+
Report string `json:"report"`
135+
}
136+
137+
func (b buildJob) Report(id string) (string, error) {
138+
var req = b.apiRequest(endpoints.builds.Report())
139+
req.param("id", id)
140+
resp, err := req.Do("GET", nil)
141+
if err != nil {
142+
return "", err
143+
}
144+
switch resp.StatusCode {
145+
case 404:
146+
return "", errors.New("Report not found")
147+
case 204:
148+
return "", errors.New("No report generated. Reports are only generated for COMPLETED builds")
149+
case 200:
150+
break
151+
default:
152+
return "", errors.New("Unknown error occured")
153+
}
154+
155+
var apiResp struct {
156+
Value struct {
157+
Report string `json:"report"`
158+
} `json:"value"`
159+
}
160+
err = json.NewDecoder(resp.Body).Decode(&apiResp)
161+
if err != nil {
162+
return "", err
163+
}
164+
165+
validJSONReport := strings.Replace(apiResp.Value.Report, "\\", "", -1)
166+
var prettyJSONReport bytes.Buffer
167+
err = json.Indent(&prettyJSONReport, []byte(validJSONReport), "", "\t")
168+
if err != nil {
169+
return "", err
170+
}
171+
172+
return prettyJSONReport.String(), nil
173+
}

cmd/build.go

+22
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ var (
4545
},
4646
}
4747

48+
buildCmdReport = &cobra.Command{
49+
Use: fmt.Sprintf("report [build_ID]"),
50+
Aliases: []string{"reports"},
51+
Short: fmt.Sprintf("View reports for a completed build"),
52+
Long: fmt.Sprintf("View reports for a completed build, containing area and resource utilisation figures."),
53+
Run: openReport,
54+
}
55+
4856
buildLogPreRun = func(cmd *cobra.Command, args []string) {
4957
if len(args) == 0 {
5058
exitWithError("ID required")
@@ -82,6 +90,7 @@ func init() {
8290
buildCmd.AddCommand(buildCmdLog)
8391
buildCmd.AddCommand(buildCmdStop)
8492
buildCmd.AddCommand(buildCmdStart)
93+
buildCmd.AddCommand(buildCmdReport)
8594
buildCmd.PersistentFlags().StringVar(&project, "project", project, "Project to use. If unset, the active project is used")
8695

8796
RootCmd.AddCommand(buildCmd)
@@ -145,3 +154,16 @@ func hasMain(dir string) bool {
145154
}
146155
return foundMain
147156
}
157+
158+
func openReport(_ *cobra.Command, args []string) {
159+
if len(args) != 1 {
160+
exitWithError("ID required")
161+
}
162+
163+
report, err := tool.Build().(reco.BuildReporter).Report(args[0])
164+
if err != nil {
165+
exitWithError(err)
166+
}
167+
168+
logger.Std.Printf("Build Report: %s", report)
169+
}

request.go

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ func (p Endpoint) Graph() string {
4747
return p.Append("{id}", "graph")
4848
}
4949

50+
// Report returns report endpoint.
51+
func (p Endpoint) Report() string {
52+
return p.Append("{id}", "reports")
53+
}
54+
5055
var (
5156
endpoints = struct {
5257
builds, deployments, projects, simulations, graphs, users Endpoint

0 commit comments

Comments
 (0)