2023-12-29 22:32:03 +02:00
|
|
|
package taskfile
|
2018-07-22 21:05:47 +02:00
|
|
|
|
|
|
|
import (
|
2023-09-12 23:42:54 +02:00
|
|
|
"context"
|
2018-07-22 21:05:47 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-11-17 22:51:10 +02:00
|
|
|
"time"
|
2018-07-22 21:05:47 +02:00
|
|
|
|
2024-01-02 01:12:28 +02:00
|
|
|
"github.com/dominikbraun/graph"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2023-09-12 23:42:54 +02:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
2023-04-15 22:22:25 +02: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 23:19:07 +02:00
|
|
|
"github.com/go-task/task/v3/internal/filepathext"
|
2023-09-12 23:42:54 +02:00
|
|
|
"github.com/go-task/task/v3/internal/logger"
|
2021-01-07 16:48:33 +02:00
|
|
|
"github.com/go-task/task/v3/internal/templater"
|
2023-12-29 22:32:03 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile/ast"
|
2018-07-22 21:05:47 +02:00
|
|
|
)
|
|
|
|
|
2024-01-04 12:14:33 +02:00
|
|
|
const (
|
|
|
|
taskfileUntrustedPrompt = `The task you are attempting to run depends on the remote Taskfile at %q.
|
2024-01-27 23:45:13 +02:00
|
|
|
--- Make sure you trust the source of this Taskfile before continuing ---
|
|
|
|
Continue?`
|
2024-01-04 12:14:33 +02:00
|
|
|
taskfileChangedPrompt = `The Taskfile at %q has changed since you last used it!
|
2024-01-27 23:45:13 +02:00
|
|
|
--- Make sure you trust the source of this Taskfile before continuing ---
|
|
|
|
Continue?`
|
2024-01-04 12:14:33 +02:00
|
|
|
)
|
|
|
|
|
2024-01-02 01:12:28 +02:00
|
|
|
// A Reader will recursively read Taskfiles from a given source using a directed
|
|
|
|
// acyclic graph (DAG).
|
|
|
|
type Reader struct {
|
|
|
|
graph *ast.TaskfileGraph
|
|
|
|
node Node
|
|
|
|
insecure bool
|
|
|
|
download bool
|
|
|
|
offline bool
|
|
|
|
timeout time.Duration
|
|
|
|
tempDir string
|
|
|
|
logger *logger.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewReader(
|
2023-09-12 23:42:54 +02:00
|
|
|
node Node,
|
|
|
|
insecure bool,
|
|
|
|
download bool,
|
|
|
|
offline bool,
|
2023-11-17 22:51:10 +02:00
|
|
|
timeout time.Duration,
|
2023-09-12 23:42:54 +02:00
|
|
|
tempDir string,
|
2024-01-02 01:12:28 +02:00
|
|
|
logger *logger.Logger,
|
|
|
|
) *Reader {
|
|
|
|
return &Reader{
|
|
|
|
graph: ast.NewTaskfileGraph(),
|
|
|
|
node: node,
|
|
|
|
insecure: insecure,
|
|
|
|
download: download,
|
|
|
|
offline: offline,
|
|
|
|
timeout: timeout,
|
|
|
|
tempDir: tempDir,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 00:10:16 +02:00
|
|
|
|
2024-01-02 01:12:28 +02: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 20:21:40 +02:00
|
|
|
|
2024-01-02 01:12:28 +02: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 06:29:03 +02: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-02 01:12:28 +02: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 06:29:03 +02:00
|
|
|
include = &ast.Include{
|
2024-01-04 02:17:30 +02:00
|
|
|
Namespace: include.Namespace,
|
2024-03-10 19:11:07 +02:00
|
|
|
Taskfile: templater.Replace(include.Taskfile, cache),
|
|
|
|
Dir: templater.Replace(include.Dir, cache),
|
2024-01-04 02:04:53 +02:00
|
|
|
Optional: include.Optional,
|
|
|
|
Internal: include.Internal,
|
2024-08-26 23:17:39 +02:00
|
|
|
Flatten: include.Flatten,
|
2024-01-04 02:04:53 +02:00
|
|
|
Aliases: include.Aliases,
|
|
|
|
AdvancedImport: include.AdvancedImport,
|
|
|
|
Vars: include.Vars,
|
2023-12-29 22:26:02 +02:00
|
|
|
}
|
2024-03-10 19:11:07 +02:00
|
|
|
if err := cache.Err(); err != nil {
|
2023-12-29 22:26:02 +02:00
|
|
|
return err
|
2020-05-17 21:03:03 +02:00
|
|
|
}
|
2023-09-02 22:24:01 +02:00
|
|
|
|
2024-02-13 21:29:28 +02:00
|
|
|
entrypoint, err := node.ResolveEntrypoint(include.Taskfile)
|
2023-09-12 23:42:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-19 03:09:07 +02:00
|
|
|
include.Dir, err = node.ResolveDir(include.Dir)
|
2024-02-13 03:07:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-19 03:09:07 +02:00
|
|
|
includeNode, err := NewNode(r.logger, entrypoint, include.Dir, r.insecure, r.timeout,
|
2023-09-12 23:42:54 +02:00
|
|
|
WithParent(node),
|
|
|
|
)
|
2023-09-02 22:24:01 +02:00
|
|
|
if err != nil {
|
2023-09-06 02:11:13 +02:00
|
|
|
if include.Optional {
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-02 22:24:01 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-07-26 00:10:16 +02:00
|
|
|
|
2024-01-02 01:12:28 +02:00
|
|
|
// Recurse into the included Taskfile
|
|
|
|
if err := r.include(includeNode); err != nil {
|
2023-09-02 22:24:01 +02:00
|
|
|
return err
|
2021-09-25 14:40:03 +02:00
|
|
|
}
|
2021-12-04 17:37:52 +02:00
|
|
|
|
2024-01-02 01:12:28 +02:00
|
|
|
// Create an edge between the Taskfiles
|
2024-04-21 21:40:26 +02:00
|
|
|
r.graph.Lock()
|
|
|
|
defer r.graph.Unlock()
|
2024-04-21 17:28:02 +02: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 17:36:18 +02:00
|
|
|
graph.EdgeWeight(1),
|
2024-04-21 17:28:02 +02:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// If the edge already exists
|
2024-04-21 17:36:18 +02:00
|
|
|
edgeData := append(edge.Properties.Data.([]*ast.Include), include)
|
2024-04-21 17:28:02 +02:00
|
|
|
err = r.graph.UpdateEdge(
|
|
|
|
node.Location(),
|
|
|
|
includeNode.Location(),
|
2024-04-21 17:36:18 +02:00
|
|
|
graph.EdgeData(edgeData),
|
|
|
|
graph.EdgeWeight(len(edgeData)),
|
2024-04-21 17:28:02 +02:00
|
|
|
)
|
2023-09-06 01:55:56 +02:00
|
|
|
}
|
2023-09-06 02:00:36 +02:00
|
|
|
if errors.Is(err, graph.ErrEdgeCreatesCycle) {
|
|
|
|
return errors.TaskfileCycleError{
|
|
|
|
Source: node.Location(),
|
|
|
|
Destination: includeNode.Location(),
|
|
|
|
}
|
|
|
|
}
|
2023-09-06 01:55:56 +02:00
|
|
|
return err
|
2023-09-02 22:24:01 +02:00
|
|
|
})
|
2024-01-02 01:12:28 +02:00
|
|
|
return nil
|
|
|
|
})
|
2022-12-23 02:27:16 +02:00
|
|
|
|
2024-01-02 01:12:28 +02:00
|
|
|
// Wait for all the go routines to finish
|
|
|
|
return g.Wait()
|
2018-07-22 21:05:47 +02:00
|
|
|
}
|
2021-12-04 17:37:52 +02:00
|
|
|
|
2024-01-02 01:12:28 +02:00
|
|
|
func (r *Reader) readNode(node Node) (*ast.Taskfile, error) {
|
2024-01-04 12:12:16 +02:00
|
|
|
var b []byte
|
|
|
|
var err error
|
|
|
|
var cache *Cache
|
2021-12-04 17:37:52 +02:00
|
|
|
|
2024-01-04 12:12:16 +02:00
|
|
|
if node.Remote() {
|
2024-01-02 01:12:28 +02:00
|
|
|
cache, err = NewCache(r.tempDir)
|
2024-01-04 12:12:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-12-04 17:37:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 12:12:16 +02:00
|
|
|
// If the file is remote and we're in offline mode, check if we have a cached copy
|
2024-01-02 01:12:28 +02:00
|
|
|
if node.Remote() && r.offline {
|
2024-01-04 12:12:16 +02:00
|
|
|
if b, err = cache.read(node); errors.Is(err, os.ErrNotExist) {
|
2024-01-12 00:30:52 +02:00
|
|
|
return nil, &errors.TaskfileCacheNotFoundError{URI: node.Location()}
|
2024-01-04 12:12:16 +02:00
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
2022-12-06 02:58:20 +02:00
|
|
|
}
|
2024-01-02 01:12:28 +02:00
|
|
|
r.logger.VerboseOutf(logger.Magenta, "task: [%s] Fetched cached copy\n", node.Location())
|
2024-01-04 12:12:16 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
downloaded := false
|
2024-01-02 01:12:28 +02:00
|
|
|
ctx, cf := context.WithTimeout(context.Background(), r.timeout)
|
2024-01-04 12:12:16 +02:00
|
|
|
defer cf()
|
|
|
|
|
|
|
|
// Read the file
|
|
|
|
b, err = node.Read(ctx)
|
2024-09-07 21:54:05 +02:00
|
|
|
var taskfileNetworkTimeoutError *errors.TaskfileNetworkTimeoutError
|
2024-01-04 12:12:16 +02:00
|
|
|
// If we timed out then we likely have a network issue
|
2024-09-07 21:54:05 +02:00
|
|
|
if node.Remote() && errors.As(err, &taskfileNetworkTimeoutError) {
|
2024-01-04 12:12:16 +02:00
|
|
|
// If a download was requested, then we can't use a cached copy
|
2024-01-02 01:12:28 +02:00
|
|
|
if r.download {
|
|
|
|
return nil, &errors.TaskfileNetworkTimeoutError{URI: node.Location(), Timeout: r.timeout}
|
2024-01-04 12:12:16 +02:00
|
|
|
}
|
|
|
|
// Search for any cached copies
|
|
|
|
if b, err = cache.read(node); errors.Is(err, os.ErrNotExist) {
|
2024-01-02 01:12:28 +02:00
|
|
|
return nil, &errors.TaskfileNetworkTimeoutError{URI: node.Location(), Timeout: r.timeout, CheckedCache: true}
|
2024-01-04 12:12:16 +02:00
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-02 01:12:28 +02:00
|
|
|
r.logger.VerboseOutf(logger.Magenta, "task: [%s] Network timeout. Fetched cached copy\n", node.Location())
|
2024-01-04 12:12:16 +02:00
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
downloaded = true
|
2022-12-06 02:58:20 +02:00
|
|
|
}
|
|
|
|
|
2024-01-04 12:12:16 +02:00
|
|
|
// If the node was remote, we need to check the checksum
|
|
|
|
if node.Remote() && downloaded {
|
2024-01-02 01:12:28 +02:00
|
|
|
r.logger.VerboseOutf(logger.Magenta, "task: [%s] Fetched remote copy\n", node.Location())
|
2024-01-04 12:12:16 +02:00
|
|
|
|
|
|
|
// Get the checksums
|
|
|
|
checksum := checksum(b)
|
|
|
|
cachedChecksum := cache.readChecksum(node)
|
|
|
|
|
2024-01-04 12:14:33 +02:00
|
|
|
var prompt string
|
2024-01-04 12:12:16 +02:00
|
|
|
if cachedChecksum == "" {
|
|
|
|
// If the checksum doesn't exist, prompt the user to continue
|
2024-01-04 12:14:33 +02:00
|
|
|
prompt = fmt.Sprintf(taskfileUntrustedPrompt, node.Location())
|
2024-01-04 12:12:16 +02:00
|
|
|
} else if checksum != cachedChecksum {
|
|
|
|
// If there is a cached hash, but it doesn't match the expected hash, prompt the user to continue
|
2024-01-04 12:14:33 +02:00
|
|
|
prompt = fmt.Sprintf(taskfileChangedPrompt, node.Location())
|
2024-01-04 12:12:16 +02:00
|
|
|
}
|
2024-05-05 17:10:32 +02:00
|
|
|
if prompt != "" {
|
2024-01-02 01:12:28 +02:00
|
|
|
if err := r.logger.Prompt(logger.Yellow, prompt, "n", "y", "yes"); err != nil {
|
2024-01-04 12:12:16 +02:00
|
|
|
return nil, &errors.TaskfileNotTrustedError{URI: node.Location()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the hash has changed (or is new)
|
|
|
|
if checksum != cachedChecksum {
|
|
|
|
// Store the checksum
|
|
|
|
if err := cache.writeChecksum(node, checksum); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Cache the file
|
2024-01-02 01:12:28 +02:00
|
|
|
r.logger.VerboseOutf(logger.Magenta, "task: [%s] Caching downloaded file\n", node.Location())
|
2024-01-04 12:12:16 +02:00
|
|
|
if err = cache.write(node, b); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-12-06 02:58:20 +02:00
|
|
|
}
|
2024-01-04 12:12:16 +02:00
|
|
|
}
|
2022-12-06 02:58:20 +02:00
|
|
|
|
2024-05-12 21:25:54 +02:00
|
|
|
var tf ast.Taskfile
|
|
|
|
if err := yaml.Unmarshal(b, &tf); err != nil {
|
2024-05-16 03:24:02 +02:00
|
|
|
// Decode the taskfile and add the file info the any errors
|
|
|
|
taskfileInvalidErr := &errors.TaskfileDecodeError{}
|
|
|
|
if errors.As(err, &taskfileInvalidErr) {
|
|
|
|
return nil, taskfileInvalidErr.WithFileInfo(node.Location(), b, 2)
|
|
|
|
}
|
2024-01-04 12:12:16 +02:00
|
|
|
return nil, &errors.TaskfileInvalidError{URI: filepathext.TryAbsToRel(node.Location()), Err: err}
|
2022-12-06 02:58:20 +02:00
|
|
|
}
|
2023-09-15 14:06:23 +02:00
|
|
|
|
2024-05-12 21:25:54 +02: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 14:06:23 +02:00
|
|
|
// Set the taskfile/task's locations
|
2024-05-12 21:25:54 +02:00
|
|
|
tf.Location = node.Location()
|
|
|
|
for _, task := range tf.Tasks.Values() {
|
2023-09-15 14:06:23 +02: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 21:25:54 +02:00
|
|
|
task.Location.Taskfile = tf.Location
|
2023-09-15 14:06:23 +02:00
|
|
|
}
|
|
|
|
}
|
2024-01-04 12:12:16 +02:00
|
|
|
|
2024-05-12 21:25:54 +02:00
|
|
|
return &tf, nil
|
2022-12-06 02:58:20 +02:00
|
|
|
}
|