-
-
Notifications
You must be signed in to change notification settings - Fork 669
/
Copy pathnode_http.go
126 lines (109 loc) · 2.92 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
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
package taskfile
import (
"context"
"io"
"net/http"
"net/url"
"path/filepath"
"time"
"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/filepathext"
)
// An HTTPNode is a node that reads a Taskfile from a remote location via HTTP.
type HTTPNode struct {
*BaseNode
url *url.URL // stores url pointing actual remote file. (e.g. with Taskfile.yml)
entrypoint string // stores entrypoint url. used for building graph vertices.
timeout time.Duration
}
func NewHTTPNode(
entrypoint string,
dir string,
insecure bool,
timeout time.Duration,
opts ...NodeOption,
) (*HTTPNode, error) {
base := NewBaseNode(dir, opts...)
url, err := url.Parse(entrypoint)
if err != nil {
return nil, err
}
if url.Scheme == "http" && !insecure {
return nil, &errors.TaskfileNotSecureError{URI: url.Redacted()}
}
return &HTTPNode{
BaseNode: base,
url: url,
entrypoint: url.Redacted(),
timeout: timeout,
}, nil
}
func (node *HTTPNode) Location() string {
return node.entrypoint
}
func (node *HTTPNode) Remote() bool {
return true
}
func (node *HTTPNode) Read(ctx context.Context) ([]byte, error) {
url, err := RemoteExists(ctx, node.url, node.timeout)
if err != nil {
return nil, err
}
node.url = url
req, err := http.NewRequest("GET", node.url.String(), nil)
if err != nil {
return nil, errors.TaskfileFetchFailedError{URI: node.url.Redacted()}
}
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return nil, &errors.TaskfileNetworkTimeoutError{URI: node.url.Redacted(), Timeout: node.timeout}
}
return nil, errors.TaskfileFetchFailedError{URI: node.url.Redacted()}
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.TaskfileFetchFailedError{
URI: node.url.Redacted(),
HTTPStatusCode: resp.StatusCode,
}
}
// Read the entire response body
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return b, nil
}
func (node *HTTPNode) ResolveEntrypoint(entrypoint string) (string, error) {
ref, err := url.Parse(entrypoint)
if err != nil {
return "", err
}
return node.url.ResolveReference(ref).String(), nil
}
func (node *HTTPNode) ResolveDir(dir string) (string, error) {
path, err := execext.Expand(dir)
if err != nil {
return "", err
}
if filepathext.IsAbs(path) {
return path, nil
}
// NOTE: Uses the directory of the entrypoint (Taskfile), not the current working directory
// This means that files are included relative to one another
parent := node.Dir()
if node.Parent() != nil {
parent = node.Parent().Dir()
}
return filepathext.SmartJoin(parent, path), nil
}
func (node *HTTPNode) FilenameAndLastDir() (string, string) {
dir, filename := filepath.Split(node.entrypoint)
dir = filepath.Base(dir)
if dir == node.url.Host {
dir = "."
}
return filepath.Base(dir), filename
}