Skip to content

Commit 0f55057

Browse files
authored
Add OutputReader for build log output (#14)
* Refactor tests to own package; Add OutputReader for build log output * re-add removed debug output * minor updates
1 parent 93d5271 commit 0f55057

10 files changed

+579
-403
lines changed

client/build.go

+5-67
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@ import (
99
"bytes"
1010
"context"
1111
"encoding/json"
12-
"fmt"
1312
"net/http"
1413

1514
"github.com/golang/glog"
16-
"github.com/gorilla/websocket"
1715
jsonresp "github.com/sylabs/json-resp"
1816
)
1917

20-
// Build submits a build job to the Build Service. The context controls the
18+
// Submit sends a build job to the Build Service. The context controls the
2119
// lifetime of the request.
22-
func (c *Client) SubmitBuild(ctx context.Context, d Definition, libraryRef string, libraryURL string) (rd ResponseData, err error) {
20+
func (c *Client) Submit(ctx context.Context, d Definition, libraryRef string, libraryURL string) (bi BuildInfo, err error) {
2321

24-
b, err := json.Marshal(RequestData{
22+
b, err := json.Marshal(BuildRequest{
2523
Definition: d,
2624
LibraryRef: libraryRef,
2725
LibraryURL: libraryURL,
@@ -44,69 +42,9 @@ func (c *Client) SubmitBuild(ctx context.Context, d Definition, libraryRef strin
4442
}
4543
defer res.Body.Close()
4644

47-
err = jsonresp.ReadResponse(res.Body, &rd)
45+
err = jsonresp.ReadResponse(res.Body, &bi)
4846
if err == nil {
49-
glog.V(2).Infof("Build response - id: %s, wsurl: %s, libref: %s",
50-
rd.ID, rd.WSURL, rd.LibraryRef)
47+
glog.V(2).Infof("Build response - id: %s, libref: %s", bi.ID, bi.LibraryRef)
5148
}
52-
return rd, err
53-
}
54-
55-
// StreamOutput reads log output from the websocket URL. The context controls
56-
// the lifetime of the request.
57-
func (c *Client) StreamOutput(ctx context.Context, wsURL string) error {
58-
h := http.Header{}
59-
c.setRequestHeaders(h)
60-
61-
ws, resp, err := websocket.DefaultDialer.Dial(wsURL, h)
62-
if err != nil {
63-
glog.V(2).Infof("websocket dial err - %s, partial response: %+v", err, resp)
64-
return err
65-
}
66-
defer ws.Close()
67-
68-
for {
69-
// Check if context has expired
70-
select {
71-
case <-ctx.Done():
72-
return ctx.Err()
73-
default:
74-
}
75-
76-
// Read from websocket
77-
mt, msg, err := ws.ReadMessage()
78-
if err != nil {
79-
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
80-
return nil
81-
}
82-
glog.V(2).Infof("websocket read message err - %s", err)
83-
return err
84-
}
85-
86-
// Print to terminal
87-
switch mt {
88-
case websocket.TextMessage:
89-
fmt.Printf("%s", msg)
90-
case websocket.BinaryMessage:
91-
fmt.Print("Ignoring binary message")
92-
}
93-
}
94-
}
95-
96-
// GetBuildStatus gets the status of a build from the Remote Build Service
97-
func (c *Client) GetBuildStatus(ctx context.Context, buildID string) (rd ResponseData, err error) {
98-
req, err := c.newRequest(http.MethodGet, "/v1/build/"+buildID, "", nil)
99-
if err != nil {
100-
return
101-
}
102-
req = req.WithContext(ctx)
103-
104-
res, err := c.HTTPClient.Do(req)
105-
if err != nil {
106-
return
107-
}
108-
defer res.Body.Close()
109-
110-
err = jsonresp.ReadResponse(res.Body, &rd)
11149
return
11250
}

0 commit comments

Comments
 (0)