mirror of
https://github.com/go-task/task.git
synced 2024-12-16 10:59:23 +02:00
22ce67c5e5
* feat: remote taskfiles over http * feat: allow insecure connections when --insecure flag is provided * feat: better error handling for fetch errors * fix: ensure cache directory always exists * fix: setup logger before everything else * feat: put remote taskfiles behind an experiment * feat: --download and --offline flags for remote taskfiles * feat: node.Read accepts a context * feat: experiment docs * chore: changelog * chore: remove unused optional param from Node interface * chore: tidy up and generalise NewNode function * fix: use sha256 in remote checksum * feat: --download by itself will not run a task * feat: custom error if remote taskfiles experiment is not enabled * refactor: BaseNode functional options and simplified FileNode * fix: use hex encoding for checksum instead of b64
68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package read
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/go-task/task/v3/errors"
|
|
)
|
|
|
|
// An HTTPNode is a node that reads a Taskfile from a remote location via HTTP.
|
|
type HTTPNode struct {
|
|
*BaseNode
|
|
URL *url.URL
|
|
}
|
|
|
|
func NewHTTPNode(uri string, insecure bool, opts ...NodeOption) (*HTTPNode, error) {
|
|
base := NewBaseNode(opts...)
|
|
url, err := url.Parse(uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if url.Scheme == "http" && !insecure {
|
|
return nil, &errors.TaskfileNotSecureError{URI: uri}
|
|
}
|
|
return &HTTPNode{
|
|
BaseNode: base,
|
|
URL: url,
|
|
}, nil
|
|
}
|
|
|
|
func (node *HTTPNode) Location() string {
|
|
return node.URL.String()
|
|
}
|
|
|
|
func (node *HTTPNode) Remote() bool {
|
|
return true
|
|
}
|
|
|
|
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()}
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, errors.TaskfileFetchFailedError{
|
|
URI: node.URL.String(),
|
|
HTTPStatusCode: resp.StatusCode,
|
|
}
|
|
}
|
|
|
|
// Read the entire response body
|
|
b, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return b, nil
|
|
}
|