Skip to content

Commit

Permalink
feat: use timeout in RemoteExists function
Browse files Browse the repository at this point in the history
  • Loading branch information
pd93 committed Mar 25, 2024
1 parent 4647003 commit 8ef8d45
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (e *Executor) Setup() error {
}

func (e *Executor) getRootNode() (taskfile.Node, error) {
node, err := taskfile.NewRootNode(e.Logger, e.Entrypoint, e.Dir, e.Insecure)
node, err := taskfile.NewRootNode(e.Logger, e.Entrypoint, e.Dir, e.Insecure, e.Timeout)
if err != nil {
return nil, err
}
Expand Down
7 changes: 5 additions & 2 deletions taskfile/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/experiments"
Expand All @@ -27,28 +28,30 @@ func NewRootNode(
entrypoint string,
dir string,
insecure bool,
timeout time.Duration,
) (Node, error) {
dir = getDefaultDir(entrypoint, dir)
// Check if there is something to read on STDIN
stat, _ := os.Stdin.Stat()
if (stat.Mode()&os.ModeCharDevice) == 0 && stat.Size() > 0 {
return NewStdinNode(dir)
}
return NewNode(l, entrypoint, dir, insecure)
return NewNode(l, entrypoint, dir, insecure, timeout)
}

func NewNode(
l *logger.Logger,
entrypoint string,
dir string,
insecure bool,
timeout time.Duration,
opts ...NodeOption,
) (Node, error) {
var node Node
var err error
switch getScheme(entrypoint) {
case "http", "https":
node, err = NewHTTPNode(l, entrypoint, dir, insecure, opts...)
node, err = NewHTTPNode(l, entrypoint, dir, insecure, timeout, opts...)
default:
// If no other scheme matches, we assume it's a file
node, err = NewFileNode(l, entrypoint, dir, opts...)
Expand Down
17 changes: 15 additions & 2 deletions taskfile/node_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"path/filepath"
"time"

"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/execext"
Expand All @@ -19,7 +20,14 @@ type HTTPNode struct {
URL *url.URL
}

func NewHTTPNode(l *logger.Logger, entrypoint, dir string, insecure bool, opts ...NodeOption) (*HTTPNode, error) {
func NewHTTPNode(
l *logger.Logger,
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 {
Expand All @@ -28,10 +36,15 @@ func NewHTTPNode(l *logger.Logger, entrypoint, dir string, insecure bool, opts .
if url.Scheme == "http" && !insecure {
return nil, &errors.TaskfileNotSecureError{URI: entrypoint}
}
url, err = RemoteExists(l, url)
ctx, cf := context.WithTimeout(context.Background(), timeout)
defer cf()
url, err = RemoteExists(ctx, l, url)
if err != nil {
return nil, err
}
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil, &errors.TaskfileNetworkTimeoutError{URI: url.String(), Timeout: timeout}
}
return &HTTPNode{
BaseNode: base,
URL: url,
Expand Down
2 changes: 1 addition & 1 deletion taskfile/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func Read(
return err
}

includeReaderNode, err := NewNode(l, entrypoint, dir, insecure,
includeReaderNode, err := NewNode(l, entrypoint, dir, insecure, timeout,
WithParent(node),
WithOptional(include.Optional),
)
Expand Down
5 changes: 3 additions & 2 deletions taskfile/taskfile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package taskfile

import (
"context"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -43,15 +44,15 @@ var (
// at the given URL with any of the default Taskfile files names. If any of
// these match a file, the first matching path will be returned. If no files are
// found, an error will be returned.
func RemoteExists(l *logger.Logger, u *url.URL) (*url.URL, error) {
func RemoteExists(ctx context.Context, l *logger.Logger, u *url.URL) (*url.URL, error) {
// Create a new HEAD request for the given URL to check if the resource exists
req, err := http.NewRequest("HEAD", u.String(), nil)
if err != nil {
return nil, errors.TaskfileFetchFailedError{URI: u.String()}
}

// Request the given URL
resp, err := http.DefaultClient.Do(req)
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return nil, errors.TaskfileFetchFailedError{URI: u.String()}
}
Expand Down

0 comments on commit 8ef8d45

Please sign in to comment.