Skip to content

Commit

Permalink
feat: node.Read accepts a context
Browse files Browse the repository at this point in the history
  • Loading branch information
pd93 committed Sep 2, 2023
1 parent bee8f18 commit 2bac808
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
3 changes: 2 additions & 1 deletion taskfile/read/node.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package read

import (
"context"
"strings"

"github.com/go-task/task/v3/errors"
Expand All @@ -10,7 +11,7 @@ import (
)

type Node interface {
Read() ([]byte, error)
Read(ctx context.Context) ([]byte, error)
Parent() Node
Optional() bool
Location() string
Expand Down
3 changes: 2 additions & 1 deletion taskfile/read/node_file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package read

import (
"context"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -39,7 +40,7 @@ func (node *FileNode) Remote() bool {
return false
}

func (node *FileNode) Read() ([]byte, error) {
func (node *FileNode) Read(ctx context.Context) ([]byte, error) {
if node.Dir == "" {
d, err := os.Getwd()
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions taskfile/read/node_http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package read

import (
"context"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -36,8 +37,13 @@ func (node *HTTPNode) Remote() bool {
return true
}

func (node *HTTPNode) Read() ([]byte, error) {
resp, err := http.Get(node.URL.String())
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()}
}
Expand Down
5 changes: 3 additions & 2 deletions taskfile/read/taskfile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package read

import (
"context"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -68,7 +69,7 @@ func readTaskfile(

// If we still don't have a copy, get the file in the usual way
if b == nil {
b, err = node.Read()
b, err = node.Read(context.Background())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -258,7 +259,7 @@ func Taskfile(
Entrypoint: path,
Dir: node.Dir,
}
b, err := osNode.Read()
b, err := osNode.Read(context.Background())
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2bac808

Please sign in to comment.