-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoutput.go
74 lines (63 loc) · 1.76 KB
/
output.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
// Copyright (c) 2018-2019, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package client
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/gorilla/websocket"
)
// OutputReader interface is used to read the websocket output from the stream
type OutputReader interface {
// Read is called when a websocket message is received
Read(messageType int, p []byte) (int, error)
}
// GetOutput reads the build output log for the provided buildID - streaming to
// OutputReader. The context controls the lifetime of the request.
func (c *Client) GetOutput(ctx context.Context, buildID string, or OutputReader) error {
wsScheme := "ws"
if c.BaseURL.Scheme == "https" {
wsScheme = "wss"
}
u := c.BaseURL.ResolveReference(&url.URL{
Scheme: wsScheme,
Host: c.BaseURL.Host,
Path: "/v1/build-ws/" + buildID,
RawQuery: "",
})
h := http.Header{}
c.setRequestHeaders(h)
ws, resp, err := websocket.DefaultDialer.Dial(u.String(), h)
if err != nil {
c.Logger.Logf("websocket dial err - %s, partial response: %+v", err, resp)
return err
}
defer ws.Close()
for {
// Check if context has expired
select {
case <-ctx.Done():
return ctx.Err()
default:
}
// Read from websocket
mt, msg, err := ws.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
return nil
}
c.Logger.Logf("websocket read message err - %s", err)
return err
}
n, err := or.Read(mt, msg)
if err != nil {
return err
}
if n != len(msg) {
return fmt.Errorf("did not read all message contents: %d != %d", n, len(msg))
}
}
}