1
0
mirror of https://github.com/go-task/task.git synced 2024-12-16 10:59:23 +02:00
task/taskfile/node.go

93 lines
2.0 KiB
Go
Raw Normal View History

package taskfile
import (
"context"
2024-01-25 14:22:10 +02:00
"os"
"path/filepath"
"strings"
"time"
"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/experiments"
2024-02-13 03:07:00 +02:00
"github.com/go-task/task/v3/internal/logger"
)
type Node interface {
Read(ctx context.Context) ([]byte, error)
Parent() Node
Location() string
2024-02-13 03:07:00 +02:00
Dir() string
Remote() bool
2024-02-13 21:29:28 +02:00
ResolveEntrypoint(entrypoint string) (string, error)
ResolveDir(dir string) (string, error)
}
2024-01-25 14:22:10 +02:00
func NewRootNode(
2024-02-13 03:07:00 +02:00
l *logger.Logger,
2024-01-25 14:22:10 +02:00
entrypoint string,
2024-02-13 03:07:00 +02:00
dir string,
2024-01-25 14:22:10 +02:00
insecure bool,
timeout time.Duration,
2024-01-25 14:22:10 +02:00
) (Node, error) {
dir = getDefaultDir(entrypoint, dir)
2024-01-25 14:22:10 +02:00
// Check if there is something to read on STDIN
stat, _ := os.Stdin.Stat()
if (stat.Mode()&os.ModeCharDevice) == 0 && stat.Size() > 0 {
return NewStdinNode(dir)
2024-01-25 14:22:10 +02:00
}
return NewNode(l, entrypoint, dir, insecure, timeout)
2024-01-25 14:22:10 +02:00
}
func NewNode(
2024-02-13 03:07:00 +02:00
l *logger.Logger,
entrypoint string,
dir string,
insecure bool,
timeout time.Duration,
opts ...NodeOption,
) (Node, error) {
var node Node
var err error
2024-02-13 03:07:00 +02:00
switch getScheme(entrypoint) {
case "http", "https":
node, err = NewHTTPNode(l, entrypoint, dir, insecure, timeout, opts...)
default:
// If no other scheme matches, we assume it's a file
2024-02-13 03:07:00 +02:00
node, err = NewFileNode(l, entrypoint, dir, opts...)
}
if node.Remote() && !experiments.RemoteTaskfiles.Enabled {
2023-12-29 22:42:30 +02: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 {
if i := strings.Index(uri, "://"); i != -1 {
return uri[:i]
}
return ""
}
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
}