2023-12-29 20:32:03 +00:00
|
|
|
package taskfile
|
2023-09-12 16:42:54 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2025-04-19 12:12:08 +01:00
|
|
|
"fmt"
|
2023-09-12 16:42:54 -05:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2024-02-13 01:07:00 +00:00
|
|
|
"path/filepath"
|
2025-04-19 12:12:08 +01:00
|
|
|
"strings"
|
2023-09-12 16:42:54 -05:00
|
|
|
|
|
|
|
"github.com/go-task/task/v3/errors"
|
2024-02-13 01:07:00 +00:00
|
|
|
"github.com/go-task/task/v3/internal/execext"
|
|
|
|
"github.com/go-task/task/v3/internal/filepathext"
|
2023-09-12 16:42:54 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// An HTTPNode is a node that reads a Taskfile from a remote location via HTTP.
|
|
|
|
type HTTPNode struct {
|
2025-05-01 18:13:51 +00:00
|
|
|
*baseNode
|
2025-04-28 16:16:10 +00:00
|
|
|
URL *url.URL // stores url pointing actual remote file. (e.g. with Taskfile.yml)
|
2023-09-12 16:42:54 -05:00
|
|
|
}
|
|
|
|
|
2024-03-25 19:05:21 +00:00
|
|
|
func NewHTTPNode(
|
|
|
|
entrypoint string,
|
|
|
|
dir string,
|
|
|
|
insecure bool,
|
|
|
|
opts ...NodeOption,
|
|
|
|
) (*HTTPNode, error) {
|
2024-03-04 18:00:28 +00:00
|
|
|
base := NewBaseNode(dir, opts...)
|
2024-02-13 01:07:00 +00:00
|
|
|
url, err := url.Parse(entrypoint)
|
2023-09-12 16:42:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if url.Scheme == "http" && !insecure {
|
2025-04-28 16:16:10 +00:00
|
|
|
return nil, &errors.TaskfileNotSecureError{URI: url.Redacted()}
|
2024-02-13 01:07:00 +00:00
|
|
|
}
|
2023-09-12 16:42:54 -05:00
|
|
|
return &HTTPNode{
|
2025-05-01 18:13:51 +00:00
|
|
|
baseNode: base,
|
2025-04-28 16:16:10 +00:00
|
|
|
URL: url,
|
2023-09-12 16:42:54 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *HTTPNode) Location() string {
|
2025-04-28 16:16:10 +00:00
|
|
|
return node.URL.Redacted()
|
2023-09-12 16:42:54 -05:00
|
|
|
}
|
|
|
|
|
2025-04-19 12:12:08 +01:00
|
|
|
func (node *HTTPNode) Read() ([]byte, error) {
|
|
|
|
return node.ReadContext(context.Background())
|
2023-09-12 16:42:54 -05:00
|
|
|
}
|
|
|
|
|
2025-04-19 12:12:08 +01:00
|
|
|
func (node *HTTPNode) ReadContext(ctx context.Context) ([]byte, error) {
|
2025-04-28 16:16:10 +00:00
|
|
|
url, err := RemoteExists(ctx, *node.URL)
|
2024-09-07 21:54:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-04-28 16:16:10 +00:00
|
|
|
req, err := http.NewRequest("GET", url.String(), nil)
|
2023-09-12 16:42:54 -05:00
|
|
|
if err != nil {
|
2025-04-28 16:16:10 +00:00
|
|
|
return nil, errors.TaskfileFetchFailedError{URI: node.Location()}
|
2023-09-12 16:42:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
|
|
|
|
if err != nil {
|
2025-04-19 12:12:08 +01:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, err
|
2024-09-07 21:54:05 +02:00
|
|
|
}
|
2025-04-28 16:16:10 +00:00
|
|
|
return nil, errors.TaskfileFetchFailedError{URI: node.Location()}
|
2023-09-12 16:42:54 -05:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, errors.TaskfileFetchFailedError{
|
2025-04-28 16:16:10 +00:00
|
|
|
URI: node.Location(),
|
2023-09-12 16:42:54 -05:00
|
|
|
HTTPStatusCode: resp.StatusCode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the entire response body
|
|
|
|
b, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
2024-01-25 12:36:31 +00:00
|
|
|
|
2024-02-13 19:29:28 +00:00
|
|
|
func (node *HTTPNode) ResolveEntrypoint(entrypoint string) (string, error) {
|
2024-02-13 19:28:42 +00:00
|
|
|
ref, err := url.Parse(entrypoint)
|
2024-02-13 01:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return node.URL.ResolveReference(ref).String(), nil
|
|
|
|
}
|
|
|
|
|
2024-02-13 19:29:28 +00:00
|
|
|
func (node *HTTPNode) ResolveDir(dir string) (string, error) {
|
2025-04-19 11:51:31 +01:00
|
|
|
path, err := execext.ExpandLiteral(dir)
|
2024-02-13 01:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepathext.IsAbs(path) {
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Uses the directory of the entrypoint (Taskfile), not the current working directory
|
|
|
|
// This means that files are included relative to one another
|
2024-10-05 20:40:22 -04:00
|
|
|
parent := node.Dir()
|
|
|
|
if node.Parent() != nil {
|
|
|
|
parent = node.Parent().Dir()
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepathext.SmartJoin(parent, path), nil
|
2024-01-25 12:36:31 +00:00
|
|
|
}
|
2024-06-28 18:07:43 +02:00
|
|
|
|
2025-04-19 12:12:08 +01:00
|
|
|
func (node *HTTPNode) CacheKey() string {
|
|
|
|
checksum := strings.TrimRight(checksum([]byte(node.Location())), "=")
|
2025-04-28 16:16:10 +00:00
|
|
|
dir, filename := filepath.Split(node.URL.Path)
|
2025-04-19 12:12:08 +01:00
|
|
|
lastDir := filepath.Base(dir)
|
|
|
|
prefix := filename
|
|
|
|
// Means it's not "", nor "." nor "/", so it's a valid directory
|
|
|
|
if len(lastDir) > 1 {
|
2025-04-28 16:16:10 +00:00
|
|
|
prefix = fmt.Sprintf("%s.%s", lastDir, filename)
|
2025-04-19 12:12:08 +01:00
|
|
|
}
|
2025-04-28 16:16:10 +00:00
|
|
|
return fmt.Sprintf("http.%s.%s.%s", node.URL.Host, prefix, checksum)
|
2024-06-28 18:07:43 +02:00
|
|
|
}
|