2023-12-29 22:32:03 +02:00
|
|
|
package taskfile
|
2023-09-12 23:42:54 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2024-02-13 03:07:00 +02:00
|
|
|
"path/filepath"
|
2024-03-25 21:05:21 +02:00
|
|
|
"time"
|
2023-09-12 23:42:54 +02:00
|
|
|
|
|
|
|
"github.com/go-task/task/v3/errors"
|
2024-02-13 03:07:00 +02:00
|
|
|
"github.com/go-task/task/v3/internal/execext"
|
|
|
|
"github.com/go-task/task/v3/internal/filepathext"
|
|
|
|
"github.com/go-task/task/v3/internal/logger"
|
2023-09-12 23:42:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// An HTTPNode is a node that reads a Taskfile from a remote location via HTTP.
|
|
|
|
type HTTPNode struct {
|
|
|
|
*BaseNode
|
2024-09-07 21:54:05 +02:00
|
|
|
URL *url.URL
|
|
|
|
logger *logger.Logger
|
|
|
|
timeout time.Duration
|
2023-09-12 23:42:54 +02:00
|
|
|
}
|
|
|
|
|
2024-03-25 21:05:21 +02:00
|
|
|
func NewHTTPNode(
|
|
|
|
l *logger.Logger,
|
|
|
|
entrypoint string,
|
|
|
|
dir string,
|
|
|
|
insecure bool,
|
|
|
|
timeout time.Duration,
|
|
|
|
opts ...NodeOption,
|
|
|
|
) (*HTTPNode, error) {
|
2024-03-04 20:00:28 +02:00
|
|
|
base := NewBaseNode(dir, opts...)
|
2024-02-13 03:07:00 +02:00
|
|
|
url, err := url.Parse(entrypoint)
|
2023-09-12 23:42:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if url.Scheme == "http" && !insecure {
|
2024-02-13 03:07:00 +02:00
|
|
|
return nil, &errors.TaskfileNotSecureError{URI: entrypoint}
|
|
|
|
}
|
2024-09-07 21:54:05 +02:00
|
|
|
|
2023-09-12 23:42:54 +02:00
|
|
|
return &HTTPNode{
|
|
|
|
BaseNode: base,
|
|
|
|
URL: url,
|
2024-09-07 21:54:05 +02:00
|
|
|
timeout: timeout,
|
|
|
|
logger: l,
|
2023-09-12 23:42:54 +02:00
|
|
|
}, 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) {
|
2024-09-07 21:54:05 +02:00
|
|
|
url, err := RemoteExists(ctx, node.logger, node.URL, node.timeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node.URL = url
|
2023-09-12 23:42:54 +02:00
|
|
|
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 {
|
2024-09-07 21:54:05 +02:00
|
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
return nil, &errors.TaskfileNetworkTimeoutError{URI: node.URL.String(), Timeout: node.timeout}
|
|
|
|
}
|
2023-09-12 23:42:54 +02:00
|
|
|
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
|
|
|
|
}
|
2024-01-25 14:36:31 +02:00
|
|
|
|
2024-02-13 21:29:28 +02:00
|
|
|
func (node *HTTPNode) ResolveEntrypoint(entrypoint string) (string, error) {
|
2024-02-13 21:28:42 +02:00
|
|
|
ref, err := url.Parse(entrypoint)
|
2024-02-13 03:07:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return node.URL.ResolveReference(ref).String(), nil
|
|
|
|
}
|
|
|
|
|
2024-02-13 21:29:28 +02:00
|
|
|
func (node *HTTPNode) ResolveDir(dir string) (string, error) {
|
2024-02-13 21:28:42 +02:00
|
|
|
path, err := execext.Expand(dir)
|
2024-02-13 03:07:00 +02: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-06 02:40:22 +02:00
|
|
|
parent := node.Dir()
|
|
|
|
if node.Parent() != nil {
|
|
|
|
parent = node.Parent().Dir()
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepathext.SmartJoin(parent, path), nil
|
2024-01-25 14:36:31 +02:00
|
|
|
}
|
2024-06-28 18:07:43 +02:00
|
|
|
|
|
|
|
func (node *HTTPNode) FilenameAndLastDir() (string, string) {
|
|
|
|
dir, filename := filepath.Split(node.URL.Path)
|
|
|
|
return filepath.Base(dir), filename
|
|
|
|
}
|