2023-12-29 20:32:03 +00:00
|
|
|
package taskfile
|
2018-07-22 16:05:47 -03:00
|
|
|
|
|
|
|
import (
|
2023-09-12 16:42:54 -05:00
|
|
|
"context"
|
2018-07-22 16:05:47 -03:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-09-24 13:21:09 -04:00
|
|
|
"sync"
|
2023-11-17 14:51:10 -06:00
|
|
|
"time"
|
2018-07-22 16:05:47 -03:00
|
|
|
|
2024-01-01 23:12:28 +00:00
|
|
|
"github.com/dominikbraun/graph"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2023-09-12 16:42:54 -05:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
2023-04-15 21:22:25 +01:00
|
|
|
"github.com/go-task/task/v3/errors"
|
2024-04-29 23:27:30 +02:00
|
|
|
"github.com/go-task/task/v3/internal/compiler"
|
2022-08-06 18:19:07 -03:00
|
|
|
"github.com/go-task/task/v3/internal/filepathext"
|
2021-01-07 11:48:33 -03:00
|
|
|
"github.com/go-task/task/v3/internal/templater"
|
2023-12-29 20:32:03 +00:00
|
|
|
"github.com/go-task/task/v3/taskfile/ast"
|
2018-07-22 16:05:47 -03:00
|
|
|
)
|
|
|
|
|
2024-01-04 10:14:33 +00:00
|
|
|
const (
|
|
|
|
taskfileUntrustedPrompt = `The task you are attempting to run depends on the remote Taskfile at %q.
|
2024-01-27 21:45:13 +00:00
|
|
|
--- Make sure you trust the source of this Taskfile before continuing ---
|
|
|
|
Continue?`
|
2024-01-04 10:14:33 +00:00
|
|
|
taskfileChangedPrompt = `The Taskfile at %q has changed since you last used it!
|
2024-01-27 21:45:13 +00:00
|
|
|
--- Make sure you trust the source of this Taskfile before continuing ---
|
|
|
|
Continue?`
|
2024-01-04 10:14:33 +00:00
|
|
|
)
|
|
|
|
|
2025-02-22 16:00:37 +00:00
|
|
|
type (
|
|
|
|
// ReaderDebugFunc is a function that is called when the reader wants to
|
|
|
|
// log debug messages
|
|
|
|
ReaderDebugFunc func(string)
|
|
|
|
// ReaderPromptFunc is a function that is called when the reader wants to
|
|
|
|
// prompt the user in some way
|
|
|
|
ReaderPromptFunc func(string) error
|
|
|
|
// ReaderOption is a function that configures a Reader.
|
|
|
|
ReaderOption func(*Reader)
|
|
|
|
// A Reader will recursively read Taskfiles from a given source using a directed
|
|
|
|
// acyclic graph (DAG).
|
|
|
|
Reader struct {
|
|
|
|
graph *ast.TaskfileGraph
|
|
|
|
node Node
|
|
|
|
insecure bool
|
|
|
|
download bool
|
|
|
|
offline bool
|
|
|
|
timeout time.Duration
|
|
|
|
tempDir string
|
|
|
|
debugFunc ReaderDebugFunc
|
|
|
|
promptFunc ReaderPromptFunc
|
|
|
|
promptMutex sync.Mutex
|
|
|
|
}
|
|
|
|
)
|
2024-01-01 23:12:28 +00:00
|
|
|
|
2025-02-22 16:00:37 +00:00
|
|
|
// NewReader constructs a new Taskfile Reader using the given Node and options.
|
2024-01-01 23:12:28 +00:00
|
|
|
func NewReader(
|
2023-09-12 16:42:54 -05:00
|
|
|
node Node,
|
2025-02-22 16:00:37 +00:00
|
|
|
opts ...ReaderOption,
|
2024-01-01 23:12:28 +00:00
|
|
|
) *Reader {
|
2025-02-22 16:00:37 +00:00
|
|
|
reader := &Reader{
|
2024-09-24 13:21:09 -04:00
|
|
|
graph: ast.NewTaskfileGraph(),
|
|
|
|
node: node,
|
2025-02-22 16:00:37 +00:00
|
|
|
insecure: false,
|
|
|
|
download: false,
|
|
|
|
offline: false,
|
|
|
|
timeout: time.Second * 10,
|
|
|
|
tempDir: os.TempDir(),
|
|
|
|
debugFunc: nil,
|
|
|
|
promptFunc: nil,
|
2024-09-24 13:21:09 -04:00
|
|
|
promptMutex: sync.Mutex{},
|
2024-01-01 23:12:28 +00:00
|
|
|
}
|
2025-02-22 16:00:37 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(reader)
|
|
|
|
}
|
|
|
|
return reader
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithInsecure enables insecure connections when reading remote taskfiles. By
|
|
|
|
// default, insecure connections are rejected.
|
|
|
|
func WithInsecure(insecure bool) ReaderOption {
|
|
|
|
return func(r *Reader) {
|
|
|
|
r.insecure = insecure
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDownload forces the reader to download a fresh copy of the taskfile from
|
|
|
|
// the remote source.
|
|
|
|
func WithDownload(download bool) ReaderOption {
|
|
|
|
return func(r *Reader) {
|
|
|
|
r.download = download
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithOffline stops the reader from being able to make network connections.
|
|
|
|
// It will still be able to read local files and cached copies of remote files.
|
|
|
|
func WithOffline(offline bool) ReaderOption {
|
|
|
|
return func(r *Reader) {
|
|
|
|
r.offline = offline
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTimeout sets the timeout for reading remote taskfiles. By default, the
|
|
|
|
// timeout is set to 10 seconds.
|
|
|
|
func WithTimeout(timeout time.Duration) ReaderOption {
|
|
|
|
return func(r *Reader) {
|
|
|
|
r.timeout = timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTempDir sets the temporary directory to be used by the reader. By
|
|
|
|
// default, the reader uses `os.TempDir()`.
|
|
|
|
func WithTempDir(tempDir string) ReaderOption {
|
|
|
|
return func(r *Reader) {
|
|
|
|
r.tempDir = tempDir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDebugFunc sets the debug function to be used by the reader. If set, this
|
|
|
|
// function will be called with debug messages. This can be useful if the caller
|
|
|
|
// wants to log debug messages from the reader. By default, no debug function is
|
|
|
|
// set and the logs are not written.
|
|
|
|
func WithDebugFunc(debugFunc ReaderDebugFunc) ReaderOption {
|
|
|
|
return func(r *Reader) {
|
|
|
|
r.debugFunc = debugFunc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPromptFunc sets the prompt function to be used by the reader. If set,
|
|
|
|
// this function will be called with prompt messages. The function should
|
|
|
|
// optionally log the message to the user and return nil if the prompt is
|
|
|
|
// accepted and the execution should continue. Otherwise, it should return an
|
|
|
|
// error which describes why the the prompt was rejected. This can then be
|
|
|
|
// caught and used later when calling the Read method. By default, no prompt
|
|
|
|
// function is set and all prompts are automatically accepted.
|
|
|
|
func WithPromptFunc(promptFunc ReaderPromptFunc) ReaderOption {
|
|
|
|
return func(r *Reader) {
|
|
|
|
r.promptFunc = promptFunc
|
|
|
|
}
|
2024-01-01 23:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) Read() (*ast.TaskfileGraph, error) {
|
|
|
|
// Recursively loop through each Taskfile, adding vertices/edges to the graph
|
|
|
|
if err := r.include(r.node); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.graph, nil
|
|
|
|
}
|
|
|
|
|
2025-02-22 16:00:37 +00:00
|
|
|
func (r *Reader) debugf(format string, a ...any) {
|
|
|
|
if r.debugFunc != nil {
|
|
|
|
r.debugFunc(fmt.Sprintf(format, a...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) promptf(format string, a ...any) error {
|
|
|
|
if r.promptFunc != nil {
|
|
|
|
return r.promptFunc(fmt.Sprintf(format, a...))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-01 23:12:28 +00:00
|
|
|
func (r *Reader) include(node Node) error {
|
|
|
|
// Create a new vertex for the Taskfile
|
|
|
|
vertex := &ast.TaskfileVertex{
|
|
|
|
URI: node.Location(),
|
|
|
|
Taskfile: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the included Taskfile to the DAG
|
|
|
|
// If the vertex already exists, we return early since its Taskfile has
|
|
|
|
// already been read and its children explored
|
|
|
|
if err := r.graph.AddVertex(vertex); err == graph.ErrVertexAlreadyExists {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-26 10:10:16 +12:00
|
|
|
|
2024-01-01 23:12:28 +00:00
|
|
|
// Read and parse the Taskfile from the file and add it to the vertex
|
|
|
|
var err error
|
|
|
|
vertex.Taskfile, err = r.readNode(node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-19 13:21:40 -05:00
|
|
|
|
2024-01-01 23:12:28 +00:00
|
|
|
// Create an error group to wait for all included Taskfiles to be read
|
|
|
|
var g errgroup.Group
|
|
|
|
|
|
|
|
// Loop over each included taskfile
|
2024-03-29 04:29:03 +00:00
|
|
|
_ = vertex.Taskfile.Includes.Range(func(namespace string, include *ast.Include) error {
|
2024-04-29 23:27:30 +02:00
|
|
|
vars := compiler.GetEnviron()
|
|
|
|
vars.Merge(vertex.Taskfile.Vars, nil)
|
2024-01-01 23:12:28 +00:00
|
|
|
// Start a goroutine to process each included Taskfile
|
|
|
|
g.Go(func() error {
|
2024-04-29 23:27:30 +02:00
|
|
|
cache := &templater.Cache{Vars: vars}
|
2024-03-29 04:29:03 +00:00
|
|
|
include = &ast.Include{
|
2024-01-04 00:17:30 +00:00
|
|
|
Namespace: include.Namespace,
|
2024-03-10 17:11:07 +00:00
|
|
|
Taskfile: templater.Replace(include.Taskfile, cache),
|
|
|
|
Dir: templater.Replace(include.Dir, cache),
|
2024-01-04 00:04:53 +00:00
|
|
|
Optional: include.Optional,
|
|
|
|
Internal: include.Internal,
|
2024-08-26 23:17:39 +02:00
|
|
|
Flatten: include.Flatten,
|
2024-01-04 00:04:53 +00:00
|
|
|
Aliases: include.Aliases,
|
|
|
|
AdvancedImport: include.AdvancedImport,
|
2024-12-30 10:09:28 +01:00
|
|
|
Excludes: include.Excludes,
|
2024-01-04 00:04:53 +00:00
|
|
|
Vars: include.Vars,
|
2023-12-29 20:26:02 +00:00
|
|
|
}
|
2024-03-10 17:11:07 +00:00
|
|
|
if err := cache.Err(); err != nil {
|
2023-12-29 20:26:02 +00:00
|
|
|
return err
|
2020-05-17 16:03:03 -03:00
|
|
|
}
|
2023-09-02 15:24:01 -05:00
|
|
|
|
2024-02-13 19:29:28 +00:00
|
|
|
entrypoint, err := node.ResolveEntrypoint(include.Taskfile)
|
2023-09-12 16:42:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-19 01:09:07 +00:00
|
|
|
include.Dir, err = node.ResolveDir(include.Dir)
|
2024-02-13 01:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2025-02-22 16:00:37 +00:00
|
|
|
includeNode, err := NewNode(entrypoint, include.Dir, r.insecure, r.timeout,
|
2023-09-12 16:42:54 -05:00
|
|
|
WithParent(node),
|
|
|
|
)
|
2023-09-02 15:24:01 -05:00
|
|
|
if err != nil {
|
2023-09-06 00:11:13 +00:00
|
|
|
if include.Optional {
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-02 15:24:01 -05:00
|
|
|
return err
|
|
|
|
}
|
2022-07-26 10:10:16 +12:00
|
|
|
|
2024-01-01 23:12:28 +00:00
|
|
|
// Recurse into the included Taskfile
|
|
|
|
if err := r.include(includeNode); err != nil {
|
2023-09-02 15:24:01 -05:00
|
|
|
return err
|
2021-09-25 09:40:03 -03:00
|
|
|
}
|
2021-12-04 17:37:52 +02:00
|
|
|
|
2024-01-01 23:12:28 +00:00
|
|
|
// Create an edge between the Taskfiles
|
2024-04-21 19:40:26 +00:00
|
|
|
r.graph.Lock()
|
|
|
|
defer r.graph.Unlock()
|
2024-04-21 15:28:02 +00:00
|
|
|
edge, err := r.graph.Edge(node.Location(), includeNode.Location())
|
|
|
|
if err == graph.ErrEdgeNotFound {
|
|
|
|
// If the edge doesn't exist, create it
|
|
|
|
err = r.graph.AddEdge(
|
|
|
|
node.Location(),
|
|
|
|
includeNode.Location(),
|
|
|
|
graph.EdgeData([]*ast.Include{include}),
|
2024-04-21 15:36:18 +00:00
|
|
|
graph.EdgeWeight(1),
|
2024-04-21 15:28:02 +00:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// If the edge already exists
|
2024-04-21 15:36:18 +00:00
|
|
|
edgeData := append(edge.Properties.Data.([]*ast.Include), include)
|
2024-04-21 15:28:02 +00:00
|
|
|
err = r.graph.UpdateEdge(
|
|
|
|
node.Location(),
|
|
|
|
includeNode.Location(),
|
2024-04-21 15:36:18 +00:00
|
|
|
graph.EdgeData(edgeData),
|
|
|
|
graph.EdgeWeight(len(edgeData)),
|
2024-04-21 15:28:02 +00:00
|
|
|
)
|
2023-09-05 23:55:56 +00:00
|
|
|
}
|
2023-09-06 00:00:36 +00:00
|
|
|
if errors.Is(err, graph.ErrEdgeCreatesCycle) {
|
|
|
|
return errors.TaskfileCycleError{
|
|
|
|
Source: node.Location(),
|
|
|
|
Destination: includeNode.Location(),
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 23:55:56 +00:00
|
|
|
return err
|
2023-09-02 15:24:01 -05:00
|
|
|
})
|
2024-01-01 23:12:28 +00:00
|
|
|
return nil
|
|
|
|
})
|
2022-12-22 21:27:16 -03:00
|
|
|
|
2024-01-01 23:12:28 +00:00
|
|
|
// Wait for all the go routines to finish
|
|
|
|
return g.Wait()
|
2018-07-22 16:05:47 -03:00
|
|
|
}
|
2021-12-04 17:37:52 +02:00
|
|
|
|
2024-01-01 23:12:28 +00:00
|
|
|
func (r *Reader) readNode(node Node) (*ast.Taskfile, error) {
|
2024-10-18 12:13:25 -04:00
|
|
|
b, err := r.loadNodeContent(node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-01-04 10:12:16 +00:00
|
|
|
}
|
2022-12-06 00:58:20 +00:00
|
|
|
|
2024-05-12 20:25:54 +01:00
|
|
|
var tf ast.Taskfile
|
|
|
|
if err := yaml.Unmarshal(b, &tf); err != nil {
|
2024-05-16 02:24:02 +01:00
|
|
|
// Decode the taskfile and add the file info the any errors
|
2025-02-22 15:44:22 +00:00
|
|
|
taskfileDecodeErr := &errors.TaskfileDecodeError{}
|
|
|
|
if errors.As(err, &taskfileDecodeErr) {
|
|
|
|
snippet := NewSnippet(b,
|
|
|
|
SnippetWithLine(taskfileDecodeErr.Line),
|
|
|
|
SnippetWithColumn(taskfileDecodeErr.Column),
|
|
|
|
SnippetWithPadding(2),
|
|
|
|
)
|
|
|
|
return nil, taskfileDecodeErr.WithFileInfo(node.Location(), snippet.String())
|
2024-05-16 02:24:02 +01:00
|
|
|
}
|
2024-01-04 10:12:16 +00:00
|
|
|
return nil, &errors.TaskfileInvalidError{URI: filepathext.TryAbsToRel(node.Location()), Err: err}
|
2022-12-06 00:58:20 +00:00
|
|
|
}
|
2023-09-15 12:06:23 +00:00
|
|
|
|
2024-05-12 20:25:54 +01:00
|
|
|
// Check that the Taskfile is set and has a schema version
|
|
|
|
if tf.Version == nil {
|
|
|
|
return nil, &errors.TaskfileVersionCheckError{URI: node.Location()}
|
|
|
|
}
|
|
|
|
|
2023-09-15 12:06:23 +00:00
|
|
|
// Set the taskfile/task's locations
|
2024-05-12 20:25:54 +01:00
|
|
|
tf.Location = node.Location()
|
|
|
|
for _, task := range tf.Tasks.Values() {
|
2023-09-15 12:06:23 +00:00
|
|
|
// If the task is not defined, create a new one
|
|
|
|
if task == nil {
|
|
|
|
task = &ast.Task{}
|
|
|
|
}
|
|
|
|
// Set the location of the taskfile for each task
|
|
|
|
if task.Location.Taskfile == "" {
|
2024-05-12 20:25:54 +01:00
|
|
|
task.Location.Taskfile = tf.Location
|
2023-09-15 12:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-04 10:12:16 +00:00
|
|
|
|
2024-05-12 20:25:54 +01:00
|
|
|
return &tf, nil
|
2022-12-06 00:58:20 +00:00
|
|
|
}
|
2024-10-18 12:13:25 -04:00
|
|
|
|
|
|
|
func (r *Reader) loadNodeContent(node Node) ([]byte, error) {
|
|
|
|
if !node.Remote() {
|
|
|
|
ctx, cf := context.WithTimeout(context.Background(), r.timeout)
|
|
|
|
defer cf()
|
|
|
|
return node.Read(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
cache, err := NewCache(r.tempDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.offline {
|
|
|
|
// In offline mode try to use cached copy
|
|
|
|
cached, err := cache.read(node)
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
return nil, &errors.TaskfileCacheNotFoundError{URI: node.Location()}
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-02-22 16:00:37 +00:00
|
|
|
r.debugf("task: [%s] Fetched cached copy\n", node.Location())
|
2024-10-18 12:13:25 -04:00
|
|
|
|
|
|
|
return cached, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cf := context.WithTimeout(context.Background(), r.timeout)
|
|
|
|
defer cf()
|
|
|
|
|
|
|
|
b, err := node.Read(ctx)
|
|
|
|
if errors.Is(err, &errors.TaskfileNetworkTimeoutError{}) {
|
|
|
|
// If we timed out then we likely have a network issue
|
|
|
|
|
|
|
|
// If a download was requested, then we can't use a cached copy
|
|
|
|
if r.download {
|
|
|
|
return nil, &errors.TaskfileNetworkTimeoutError{URI: node.Location(), Timeout: r.timeout}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for any cached copies
|
|
|
|
cached, err := cache.read(node)
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
return nil, &errors.TaskfileNetworkTimeoutError{URI: node.Location(), Timeout: r.timeout, CheckedCache: true}
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-02-22 16:00:37 +00:00
|
|
|
r.debugf("task: [%s] Network timeout. Fetched cached copy\n", node.Location())
|
2024-10-18 12:13:25 -04:00
|
|
|
|
|
|
|
return cached, nil
|
|
|
|
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-02-22 16:00:37 +00:00
|
|
|
r.debugf("task: [%s] Fetched remote copy\n", node.Location())
|
2024-10-18 12:13:25 -04:00
|
|
|
|
|
|
|
// Get the checksums
|
|
|
|
checksum := checksum(b)
|
|
|
|
cachedChecksum := cache.readChecksum(node)
|
|
|
|
|
|
|
|
var prompt string
|
|
|
|
if cachedChecksum == "" {
|
|
|
|
// If the checksum doesn't exist, prompt the user to continue
|
2025-02-22 16:00:37 +00:00
|
|
|
prompt = taskfileUntrustedPrompt
|
2024-10-18 12:13:25 -04:00
|
|
|
} else if checksum != cachedChecksum {
|
|
|
|
// If there is a cached hash, but it doesn't match the expected hash, prompt the user to continue
|
2025-02-22 16:00:37 +00:00
|
|
|
prompt = taskfileChangedPrompt
|
2024-10-18 12:13:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if prompt != "" {
|
|
|
|
if err := func() error {
|
|
|
|
r.promptMutex.Lock()
|
|
|
|
defer r.promptMutex.Unlock()
|
2025-02-22 16:00:37 +00:00
|
|
|
return r.promptf(prompt, node.Location())
|
2024-10-18 12:13:25 -04:00
|
|
|
}(); err != nil {
|
|
|
|
return nil, &errors.TaskfileNotTrustedError{URI: node.Location()}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the checksum
|
|
|
|
if err := cache.writeChecksum(node, checksum); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the file
|
2025-02-22 16:00:37 +00:00
|
|
|
r.debugf("task: [%s] Caching downloaded file\n", node.Location())
|
2024-10-18 12:13:25 -04:00
|
|
|
if err = cache.write(node, b); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|