1
0
mirror of https://github.com/go-task/task.git synced 2025-11-25 22:32:55 +02:00

feat: use timeout in RemoteExists function

This commit is contained in:
Pete Davison
2024-03-25 19:05:21 +00:00
parent a496a1dfa8
commit 64b7d3415a
5 changed files with 25 additions and 8 deletions

View File

@@ -54,7 +54,7 @@ func (e *Executor) Setup() error {
} }
func (e *Executor) getRootNode() (taskfile.Node, 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 { if err != nil {
return nil, err return nil, err
} }

View File

@@ -5,6 +5,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/go-task/task/v3/errors" "github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/experiments" "github.com/go-task/task/v3/internal/experiments"
@@ -27,6 +28,7 @@ func NewRootNode(
entrypoint string, entrypoint string,
dir string, dir string,
insecure bool, insecure bool,
timeout time.Duration,
) (Node, error) { ) (Node, error) {
dir = getDefaultDir(entrypoint, dir) dir = getDefaultDir(entrypoint, dir)
// Check if there is something to read on STDIN // Check if there is something to read on STDIN
@@ -34,7 +36,7 @@ func NewRootNode(
if (stat.Mode()&os.ModeCharDevice) == 0 && stat.Size() > 0 { if (stat.Mode()&os.ModeCharDevice) == 0 && stat.Size() > 0 {
return NewStdinNode(dir) return NewStdinNode(dir)
} }
return NewNode(l, entrypoint, dir, insecure) return NewNode(l, entrypoint, dir, insecure, timeout)
} }
func NewNode( func NewNode(
@@ -42,13 +44,14 @@ func NewNode(
entrypoint string, entrypoint string,
dir string, dir string,
insecure bool, insecure bool,
timeout time.Duration,
opts ...NodeOption, opts ...NodeOption,
) (Node, error) { ) (Node, error) {
var node Node var node Node
var err error var err error
switch getScheme(entrypoint) { switch getScheme(entrypoint) {
case "http", "https": case "http", "https":
node, err = NewHTTPNode(l, entrypoint, dir, insecure, opts...) node, err = NewHTTPNode(l, entrypoint, dir, insecure, timeout, opts...)
default: default:
// If no other scheme matches, we assume it's a file // If no other scheme matches, we assume it's a file
node, err = NewFileNode(l, entrypoint, dir, opts...) node, err = NewFileNode(l, entrypoint, dir, opts...)

View File

@@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"path/filepath" "path/filepath"
"time"
"github.com/go-task/task/v3/errors" "github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/execext" "github.com/go-task/task/v3/internal/execext"
@@ -19,7 +20,14 @@ type HTTPNode struct {
URL *url.URL 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...) base := NewBaseNode(dir, opts...)
url, err := url.Parse(entrypoint) url, err := url.Parse(entrypoint)
if err != nil { if err != nil {
@@ -28,10 +36,15 @@ func NewHTTPNode(l *logger.Logger, entrypoint, dir string, insecure bool, opts .
if url.Scheme == "http" && !insecure { if url.Scheme == "http" && !insecure {
return nil, &errors.TaskfileNotSecureError{URI: entrypoint} 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 { if err != nil {
return nil, err return nil, err
} }
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil, &errors.TaskfileNetworkTimeoutError{URI: url.String(), Timeout: timeout}
}
return &HTTPNode{ return &HTTPNode{
BaseNode: base, BaseNode: base,
URL: url, URL: url,

View File

@@ -74,7 +74,7 @@ func Read(
return err return err
} }
includeReaderNode, err := NewNode(l, entrypoint, dir, insecure, includeReaderNode, err := NewNode(l, entrypoint, dir, insecure, timeout,
WithParent(node), WithParent(node),
WithOptional(include.Optional), WithOptional(include.Optional),
) )

View File

@@ -1,6 +1,7 @@
package taskfile package taskfile
import ( import (
"context"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@@ -43,7 +44,7 @@ var (
// at the given URL with any of the default Taskfile files names. If any of // 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 // these match a file, the first matching path will be returned. If no files are
// found, an error will be returned. // 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 // Create a new HEAD request for the given URL to check if the resource exists
req, err := http.NewRequest("HEAD", u.String(), nil) req, err := http.NewRequest("HEAD", u.String(), nil)
if err != nil { if err != nil {
@@ -51,7 +52,7 @@ func RemoteExists(l *logger.Logger, u *url.URL) (*url.URL, error) {
} }
// Request the given URL // Request the given URL
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil { if err != nil {
return nil, errors.TaskfileFetchFailedError{URI: u.String()} return nil, errors.TaskfileFetchFailedError{URI: u.String()}
} }