Skip to content

Commit c77c8a4

Browse files
authored
refactor: check if the remote exists just before reading it (#1713)
* refactor: check if the remote exists in the read to avoid doing it in offline mode * fix: timeout error was not working * fix: use cached copy if available
1 parent a233b52 commit c77c8a4

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

taskfile/node_http.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import (
1717
// An HTTPNode is a node that reads a Taskfile from a remote location via HTTP.
1818
type HTTPNode struct {
1919
*BaseNode
20-
URL *url.URL
20+
URL *url.URL
21+
logger *logger.Logger
22+
timeout time.Duration
2123
}
2224

2325
func NewHTTPNode(
@@ -36,18 +38,12 @@ func NewHTTPNode(
3638
if url.Scheme == "http" && !insecure {
3739
return nil, &errors.TaskfileNotSecureError{URI: entrypoint}
3840
}
39-
ctx, cf := context.WithTimeout(context.Background(), timeout)
40-
defer cf()
41-
url, err = RemoteExists(ctx, l, url)
42-
if err != nil {
43-
return nil, err
44-
}
45-
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
46-
return nil, &errors.TaskfileNetworkTimeoutError{URI: url.String(), Timeout: timeout}
47-
}
41+
4842
return &HTTPNode{
4943
BaseNode: base,
5044
URL: url,
45+
timeout: timeout,
46+
logger: l,
5147
}, nil
5248
}
5349

@@ -60,13 +56,21 @@ func (node *HTTPNode) Remote() bool {
6056
}
6157

6258
func (node *HTTPNode) Read(ctx context.Context) ([]byte, error) {
59+
url, err := RemoteExists(ctx, node.logger, node.URL, node.timeout)
60+
if err != nil {
61+
return nil, err
62+
}
63+
node.URL = url
6364
req, err := http.NewRequest("GET", node.URL.String(), nil)
6465
if err != nil {
6566
return nil, errors.TaskfileFetchFailedError{URI: node.URL.String()}
6667
}
6768

6869
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
6970
if err != nil {
71+
if errors.Is(err, context.DeadlineExceeded) {
72+
return nil, &errors.TaskfileNetworkTimeoutError{URI: node.URL.String(), Timeout: node.timeout}
73+
}
7074
return nil, errors.TaskfileFetchFailedError{URI: node.URL.String()}
7175
}
7276
defer resp.Body.Close()

taskfile/reader.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,9 @@ func (r *Reader) readNode(node Node) (*ast.Taskfile, error) {
208208

209209
// Read the file
210210
b, err = node.Read(ctx)
211+
var taskfileNetworkTimeoutError *errors.TaskfileNetworkTimeoutError
211212
// If we timed out then we likely have a network issue
212-
if node.Remote() && errors.Is(ctx.Err(), context.DeadlineExceeded) {
213+
if node.Remote() && errors.As(err, &taskfileNetworkTimeoutError) {
213214
// If a download was requested, then we can't use a cached copy
214215
if r.download {
215216
return nil, &errors.TaskfileNetworkTimeoutError{URI: node.Location(), Timeout: r.timeout}

taskfile/taskfile.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"slices"
1010
"strings"
11+
"time"
1112

1213
"github.com/go-task/task/v3/errors"
1314
"github.com/go-task/task/v3/internal/filepathext"
@@ -40,7 +41,7 @@ var (
4041
// at the given URL with any of the default Taskfile files names. If any of
4142
// these match a file, the first matching path will be returned. If no files are
4243
// found, an error will be returned.
43-
func RemoteExists(ctx context.Context, l *logger.Logger, u *url.URL) (*url.URL, error) {
44+
func RemoteExists(ctx context.Context, l *logger.Logger, u *url.URL, timeout time.Duration) (*url.URL, error) {
4445
// Create a new HEAD request for the given URL to check if the resource exists
4546
req, err := http.NewRequest("HEAD", u.String(), nil)
4647
if err != nil {
@@ -50,6 +51,9 @@ func RemoteExists(ctx context.Context, l *logger.Logger, u *url.URL) (*url.URL,
5051
// Request the given URL
5152
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
5253
if err != nil {
54+
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
55+
return nil, &errors.TaskfileNetworkTimeoutError{URI: u.String(), Timeout: timeout}
56+
}
5357
return nil, errors.TaskfileFetchFailedError{URI: u.String()}
5458
}
5559
defer resp.Body.Close()

0 commit comments

Comments
 (0)