/
node_http.go
67 lines (56 loc) · 1.35 KB
/
node_http.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
package read
import (
"context"
"io"
"net/http"
"net/url"
"github.com/go-task/task/v3/errors"
)
// An HTTPNode is a node that reads a Taskfile from a remote location via HTTP.
type HTTPNode struct {
*BaseNode
URL *url.URL
}
func NewHTTPNode(uri string, insecure bool, opts ...NodeOption) (*HTTPNode, error) {
base := NewBaseNode(opts...)
url, err := url.Parse(uri)
if err != nil {
return nil, err
}
if url.Scheme == "http" && !insecure {
return nil, &errors.TaskfileNotSecureError{URI: uri}
}
return &HTTPNode{
BaseNode: base,
URL: url,
}, nil
}
func (node *HTTPNode) Location() string {
return node.URL.String()
}
func (node *HTTPNode) Remote() bool {
return true
}
func (node *HTTPNode) Read(ctx context.Context) ([]byte, error) {
req, err := http.NewRequest("GET", node.URL.String(), nil)
if err != nil {
return nil, errors.TaskfileFetchFailedError{URI: node.URL.String()}
}
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return nil, errors.TaskfileFetchFailedError{URI: node.URL.String()}
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.TaskfileFetchFailedError{
URI: node.URL.String(),
HTTPStatusCode: resp.StatusCode,
}
}
// Read the entire response body
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return b, nil
}