1
0
mirror of https://github.com/go-task/task.git synced 2025-05-13 22:16:31 +02:00
task/taskfile/node.go

113 lines
2.3 KiB
Go
Raw Normal View History

package taskfile
import (
"context"
2024-01-25 12:22:10 +00:00
"os"
"path/filepath"
"strings"
"time"
giturls "github.com/chainguard-dev/git-urls"
"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/experiments"
)
type Node interface {
Read() ([]byte, error)
Parent() Node
Location() string
2024-02-13 01:07:00 +00:00
Dir() string
2024-02-13 19:29:28 +00:00
ResolveEntrypoint(entrypoint string) (string, error)
ResolveDir(dir string) (string, error)
}
type RemoteNode interface {
Node
ReadContext(ctx context.Context) ([]byte, error)
CacheKey() string
}
2024-01-25 12:22:10 +00:00
func NewRootNode(
entrypoint string,
2024-02-13 01:07:00 +00:00
dir string,
2024-01-25 12:22:10 +00:00
insecure bool,
timeout time.Duration,
2024-01-25 12:22:10 +00:00
) (Node, error) {
dir = getDefaultDir(entrypoint, dir)
2024-05-08 16:44:05 +01:00
// If the entrypoint is "-", we read from stdin
if entrypoint == "-" {
return NewStdinNode(dir)
2024-01-25 12:22:10 +00:00
}
return NewNode(entrypoint, dir, insecure)
2024-01-25 12:22:10 +00:00
}
func NewNode(
2024-02-13 01:07:00 +00:00
entrypoint string,
dir string,
insecure bool,
opts ...NodeOption,
) (Node, error) {
var node Node
var err error
scheme, err := getScheme(entrypoint)
if err != nil {
return nil, err
}
switch scheme {
case "git":
node, err = NewGitNode(entrypoint, dir, insecure, opts...)
case "http", "https":
node, err = NewHTTPNode(entrypoint, dir, insecure, opts...)
default:
node, err = NewFileNode(entrypoint, dir, opts...)
}
if _, isRemote := node.(RemoteNode); isRemote && !experiments.RemoteTaskfiles.Enabled() {
2023-12-29 20:42:30 +00:00
return nil, errors.New("task: Remote taskfiles are not enabled. You can read more about this experiment and how to enable it at https://taskfile.dev/experiments/remote-taskfiles")
}
return node, err
}
func getScheme(uri string) (string, error) {
u, err := giturls.Parse(uri)
if u == nil {
return "", err
}
if strings.HasSuffix(strings.Split(u.Path, "//")[0], ".git") && (u.Scheme == "git" || u.Scheme == "ssh" || u.Scheme == "https" || u.Scheme == "http") {
return "git", nil
}
if i := strings.Index(uri, "://"); i != -1 {
return uri[:i], nil
}
return "", nil
}
func getDefaultDir(entrypoint, dir string) string {
// If the entrypoint and dir are empty, we default the directory to the current working directory
if dir == "" {
if entrypoint == "" {
wd, err := os.Getwd()
if err != nil {
return ""
}
dir = wd
}
return dir
}
// If the directory is set, ensure it is an absolute path
var err error
dir, err = filepath.Abs(dir)
if err != nil {
return ""
}
return dir
}