2018-07-22 21:05:47 +02:00
package read
import (
2023-09-12 23:42:54 +02:00
"context"
2018-07-22 21:05:47 +02:00
"fmt"
"os"
"path/filepath"
"runtime"
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"
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"
2022-12-06 02:58:20 +02:00
"github.com/go-task/task/v3/internal/sysinfo"
2021-01-07 16:48:33 +02:00
"github.com/go-task/task/v3/internal/templater"
"github.com/go-task/task/v3/taskfile"
2018-07-22 21:05:47 +02:00
)
2019-01-21 11:56:14 +02:00
var (
2020-08-16 00:12:39 +02:00
// ErrIncludedTaskfilesCantHaveDotenvs is returned when a included Taskfile contains dotenvs
2020-08-04 00:18:38 +02:00
ErrIncludedTaskfilesCantHaveDotenvs = errors . New ( "task: Included Taskfiles can't have dotenv declarations. Please, move the dotenv declaration to the main Taskfile" )
2021-12-04 17:37:52 +02:00
2022-02-19 23:24:43 +02:00
defaultTaskfiles = [ ] string {
"Taskfile.yml" ,
2023-06-17 19:38:53 +02:00
"taskfile.yml" ,
2022-02-19 23:24:43 +02:00
"Taskfile.yaml" ,
2023-06-17 19:38:53 +02:00
"taskfile.yaml" ,
2022-02-19 23:24:43 +02:00
"Taskfile.dist.yml" ,
2023-06-17 19:38:53 +02:00
"taskfile.dist.yml" ,
2022-02-19 23:24:43 +02:00
"Taskfile.dist.yaml" ,
2023-06-17 19:38:53 +02:00
"taskfile.dist.yaml" ,
2022-02-19 23:24:43 +02:00
}
2019-01-21 11:56:14 +02:00
)
2018-10-13 22:52:09 +02:00
2023-09-12 23:42:54 +02:00
func readTaskfile (
node Node ,
download ,
offline bool ,
tempDir string ,
l * logger . Logger ,
) ( * taskfile . Taskfile , error ) {
var b [ ] byte
2023-09-14 22:52:56 +02:00
var err error
var cache * Cache
2023-09-14 23:57:46 +02:00
2023-09-14 22:52:56 +02:00
if node . Remote ( ) {
cache , err = NewCache ( tempDir )
if err != nil {
return nil , err
}
2023-09-12 23:42:54 +02:00
}
// If the file is remote, check if we have a cached copy
// If we're told to download, skip the cache
if node . Remote ( ) && ! download {
if b , err = cache . read ( node ) ; ! errors . Is ( err , os . ErrNotExist ) && err != nil {
return nil , err
}
if b != nil {
l . VerboseOutf ( logger . Magenta , "task: [%s] Fetched cached copy\n" , node . Location ( ) )
}
}
// If the file is remote, we found nothing in the cache and we're not
// allowed to download it then we can't do anything.
if node . Remote ( ) && b == nil && offline {
if b == nil && offline {
return nil , & errors . TaskfileCacheNotFound { URI : node . Location ( ) }
}
}
// If we still don't have a copy, get the file in the usual way
if b == nil {
b , err = node . Read ( context . Background ( ) )
if err != nil {
return nil , err
}
// If the node was remote, we need to check the checksum
if node . Remote ( ) {
l . VerboseOutf ( logger . Magenta , "task: [%s] Fetched remote copy\n" , node . Location ( ) )
// Get the checksums
checksum := checksum ( b )
cachedChecksum := cache . readChecksum ( node )
// If the checksum doesn't exist, prompt the user to continue
if cachedChecksum == "" {
if cont , err := l . Prompt ( logger . Yellow , fmt . Sprintf ( "The task you are attempting to run depends on the remote Taskfile at %q.\n--- Make sure you trust the source of this Taskfile before continuing ---\nContinue?" , node . Location ( ) ) , "n" , "y" , "yes" ) ; err != nil {
return nil , err
} else if ! cont {
return nil , & errors . TaskfileNotTrustedError { URI : node . Location ( ) }
}
} else if checksum != cachedChecksum {
// If there is a cached hash, but it doesn't match the expected hash, prompt the user to continue
if cont , err := l . Prompt ( logger . Yellow , fmt . Sprintf ( "The Taskfile at %q has changed since you last used it!\n--- Make sure you trust the source of this Taskfile before continuing ---\nContinue?" , node . Location ( ) ) , "n" , "y" , "yes" ) ; err != nil {
return nil , err
} else if ! cont {
return nil , & errors . TaskfileNotTrustedError { URI : node . Location ( ) }
}
}
// If the hash has changed (or is new), store it in the cache
if checksum != cachedChecksum {
if err := cache . writeChecksum ( node , checksum ) ; err != nil {
return nil , err
}
}
}
}
// If the file is remote and we need to cache it
if node . Remote ( ) && download {
l . VerboseOutf ( logger . Magenta , "task: [%s] Caching downloaded file\n" , node . Location ( ) )
// Cache the file for later
if err = cache . write ( node , b ) ; err != nil {
return nil , err
}
}
var t taskfile . Taskfile
if err := yaml . Unmarshal ( b , & t ) ; err != nil {
return nil , & errors . TaskfileInvalidError { URI : filepathext . TryAbsToRel ( node . Location ( ) ) , Err : err }
}
t . Location = node . Location ( )
return & t , nil
}
2018-07-22 21:05:47 +02:00
// Taskfile reads a Taskfile for a given directory
2021-12-04 17:37:52 +02:00
// Uses current dir when dir is left empty. Uses Taskfile.yml
// or Taskfile.yaml when entrypoint is left empty
2023-09-12 23:42:54 +02:00
func Taskfile (
node Node ,
insecure bool ,
download bool ,
offline bool ,
tempDir string ,
l * logger . Logger ,
) ( * taskfile . Taskfile , error ) {
2023-09-02 22:24:01 +02:00
var _taskfile func ( Node ) ( * taskfile . Taskfile , error )
_taskfile = func ( node Node ) ( * taskfile . Taskfile , error ) {
2023-09-12 23:42:54 +02:00
t , err := readTaskfile ( node , download , offline , tempDir , l )
2021-12-04 17:37:52 +02:00
if err != nil {
2023-09-02 22:24:01 +02:00
return nil , err
2021-12-04 17:37:52 +02:00
}
2022-07-26 00:10:16 +02:00
2023-09-02 22:24:01 +02:00
// Annotate any included Taskfile reference with a base directory for resolving relative paths
if node , isFileNode := node . ( * FileNode ) ; isFileNode {
_ = t . Includes . Range ( func ( key string , includedFile taskfile . IncludedTaskfile ) error {
// Set the base directory for resolving relative paths, but only if not already set
if includedFile . BaseDir == "" {
includedFile . BaseDir = node . Dir
t . Includes . Set ( key , includedFile )
}
return nil
} )
2022-07-26 00:10:16 +02:00
}
2023-09-02 22:24:01 +02:00
err = t . Includes . Range ( func ( namespace string , includedTask taskfile . IncludedTaskfile ) error {
if t . Version . Compare ( taskfile . V3 ) >= 0 {
tr := templater . Templater { Vars : t . Vars , RemoveNoValue : true }
includedTask = taskfile . IncludedTaskfile {
Taskfile : tr . Replace ( includedTask . Taskfile ) ,
Dir : tr . Replace ( includedTask . Dir ) ,
Optional : includedTask . Optional ,
Internal : includedTask . Internal ,
Aliases : includedTask . Aliases ,
AdvancedImport : includedTask . AdvancedImport ,
Vars : includedTask . Vars ,
BaseDir : includedTask . BaseDir ,
}
if err := tr . Err ( ) ; err != nil {
return err
}
2020-05-17 21:03:03 +02:00
}
2023-09-02 22:24:01 +02:00
2023-09-12 23:42:54 +02:00
uri , err := includedTask . FullTaskfilePath ( )
if err != nil {
return err
}
includeReaderNode , err := NewNode ( uri , insecure ,
WithParent ( node ) ,
WithOptional ( includedTask . Optional ) ,
)
2023-09-02 22:24:01 +02:00
if err != nil {
if includedTask . Optional {
return nil
}
2021-01-01 23:27:50 +02:00
return err
2020-05-17 21:03:03 +02:00
}
2023-09-02 22:24:01 +02:00
if err := checkCircularIncludes ( includeReaderNode ) ; err != nil {
return err
}
2022-07-26 00:10:16 +02:00
2023-09-02 22:24:01 +02:00
includedTaskfile , err := _taskfile ( includeReaderNode )
if err != nil {
if includedTask . Optional {
return nil
}
return err
2021-09-25 14:40:03 +02:00
}
2021-12-04 17:37:52 +02:00
2023-09-02 22:24:01 +02:00
if t . Version . Compare ( taskfile . V3 ) >= 0 && len ( includedTaskfile . Dotenv ) > 0 {
return ErrIncludedTaskfilesCantHaveDotenvs
}
2022-01-15 05:38:37 +02:00
2023-09-02 22:24:01 +02:00
if includedTask . AdvancedImport {
dir , err := includedTask . FullDirPath ( )
if err != nil {
return err
}
2022-01-16 06:34:59 +02:00
2023-09-02 22:24:01 +02:00
// nolint: errcheck
includedTaskfile . Vars . Range ( func ( k string , v taskfile . Var ) error {
o := v
o . Dir = dir
includedTaskfile . Vars . Set ( k , o )
return nil
} )
// nolint: errcheck
includedTaskfile . Env . Range ( func ( k string , v taskfile . Var ) error {
o := v
o . Dir = dir
includedTaskfile . Env . Set ( k , o )
return nil
} )
for _ , task := range includedTaskfile . Tasks . Values ( ) {
task . Dir = filepathext . SmartJoin ( dir , task . Dir )
if task . IncludeVars == nil {
task . IncludeVars = & taskfile . Vars { }
}
task . IncludeVars . Merge ( includedTask . Vars )
task . IncludedTaskfileVars = includedTaskfile . Vars
task . IncludedTaskfile = & includedTask
}
2022-01-15 05:38:37 +02:00
}
2020-01-29 09:03:06 +02:00
2023-09-02 22:24:01 +02:00
if err = taskfile . Merge ( t , includedTaskfile , & includedTask , namespace ) ; err != nil {
2022-07-26 00:10:16 +02:00
return err
}
2023-09-02 22:24:01 +02:00
if includedTaskfile . Tasks . Get ( "default" ) != nil && t . Tasks . Get ( namespace ) == nil {
defaultTaskName := fmt . Sprintf ( "%s:default" , namespace )
task := t . Tasks . Get ( defaultTaskName )
task . Aliases = append ( task . Aliases , namespace )
task . Aliases = append ( task . Aliases , includedTask . Aliases ... )
t . Tasks . Set ( defaultTaskName , task )
}
2021-01-09 17:09:23 +02:00
2023-09-02 22:24:01 +02:00
return nil
} )
if err != nil {
return nil , err
}
if t . Version . Compare ( taskfile . V3 ) < 0 {
if node , isFileNode := node . ( * FileNode ) ; isFileNode {
path := filepathext . SmartJoin ( node . Dir , fmt . Sprintf ( "Taskfile_%s.yml" , runtime . GOOS ) )
if _ , err = os . Stat ( path ) ; err == nil {
osNode := & FileNode {
2023-09-12 23:42:54 +02:00
BaseNode : NewBaseNode ( WithParent ( node ) ) ,
2023-09-02 22:24:01 +02:00
Entrypoint : path ,
Dir : node . Dir ,
}
2023-09-12 23:42:54 +02:00
b , err := osNode . Read ( context . Background ( ) )
2023-09-02 22:24:01 +02:00
if err != nil {
return nil , err
}
2023-09-12 23:42:54 +02:00
var osTaskfile * taskfile . Taskfile
if err := yaml . Unmarshal ( b , & osTaskfile ) ; err != nil {
return nil , & errors . TaskfileInvalidError { URI : filepathext . TryAbsToRel ( node . Location ( ) ) , Err : err }
}
t . Location = node . Location ( )
2023-09-02 22:24:01 +02:00
if err = taskfile . Merge ( t , osTaskfile , nil ) ; err != nil {
return nil , err
}
2023-07-08 16:42:38 +02:00
}
2020-02-15 15:40:42 +02:00
}
2020-01-29 09:03:06 +02:00
}
2023-09-02 22:24:01 +02:00
for _ , task := range t . Tasks . Values ( ) {
// If the task is not defined, create a new one
if task == nil {
task = & taskfile . Task { }
2020-05-17 20:42:27 +02:00
}
2023-09-02 22:24:01 +02:00
// Set the location of the taskfile for each task
if task . Location . Taskfile == "" {
task . Location . Taskfile = t . Location
2020-05-17 20:42:27 +02:00
}
2018-07-22 21:05:47 +02:00
}
2022-12-23 02:27:16 +02:00
2023-09-02 22:24:01 +02:00
return t , nil
2022-10-07 12:18:53 +02:00
}
2023-09-02 22:24:01 +02:00
return _taskfile ( node )
2018-07-22 21:05:47 +02:00
}
2021-12-04 17:37:52 +02:00
2023-09-14 23:57:46 +02:00
// Exists will check if a file at the given path Exists. If it does, it will
2023-09-12 23:42:54 +02:00
// return the path to it. If it does not, it will search the search for any
// files at the given path with any of the default Taskfile files names. If any
// of these match a file, the first matching path will be returned. If no files
// are found, an error will be returned.
2023-09-14 23:57:46 +02:00
func Exists ( path string ) ( string , error ) {
2021-12-04 17:37:52 +02:00
fi , err := os . Stat ( path )
if err != nil {
return "" , err
}
if fi . Mode ( ) . IsRegular ( ) {
2022-08-06 23:19:07 +02:00
return path , nil
2021-12-04 17:37:52 +02:00
}
for _ , n := range defaultTaskfiles {
2022-08-06 23:19:07 +02:00
fpath := filepathext . SmartJoin ( path , n )
2021-12-04 17:37:52 +02:00
if _ , err := os . Stat ( fpath ) ; err == nil {
2022-08-06 23:19:07 +02:00
return fpath , nil
2021-12-04 17:37:52 +02:00
}
}
2023-09-02 22:24:01 +02:00
return "" , errors . TaskfileNotFoundError { URI : path , Walk : false }
2021-12-04 17:37:52 +02:00
}
2022-01-16 06:34:59 +02:00
2023-09-14 23:57:46 +02:00
// ExistsWalk will check if a file at the given path exists by calling the
2023-09-12 23:42:54 +02:00
// exists function. If a file is not found, it will walk up the directory tree
// calling the exists function until it finds a file or reaches the root
// directory. On supported operating systems, it will also check if the user ID
// of the directory changes and abort if it does.
2023-09-14 23:57:46 +02:00
func ExistsWalk ( path string ) ( string , error ) {
2022-12-06 02:58:20 +02:00
origPath := path
owner , err := sysinfo . Owner ( path )
if err != nil {
return "" , err
}
for {
2023-09-14 23:57:46 +02:00
fpath , err := Exists ( path )
2022-12-06 02:58:20 +02:00
if err == nil {
return fpath , nil
}
// Get the parent path/user id
parentPath := filepath . Dir ( path )
parentOwner , err := sysinfo . Owner ( parentPath )
if err != nil {
return "" , err
}
// Error if we reached the root directory and still haven't found a file
// OR if the user id of the directory changes
if path == parentPath || ( parentOwner != owner ) {
2023-09-02 22:24:01 +02:00
return "" , errors . TaskfileNotFoundError { URI : origPath , Walk : false }
2022-12-06 02:58:20 +02:00
}
owner = parentOwner
path = parentPath
}
}
2023-09-02 22:24:01 +02:00
func checkCircularIncludes ( node Node ) error {
2022-01-16 06:34:59 +02:00
if node == nil {
2022-02-04 05:12:58 +02:00
return errors . New ( "task: failed to check for include cycle: node was nil" )
2022-01-16 06:34:59 +02:00
}
2023-09-02 22:24:01 +02:00
if node . Parent ( ) == nil {
2022-02-04 05:12:58 +02:00
return errors . New ( "task: failed to check for include cycle: node.Parent was nil" )
2022-01-16 06:34:59 +02:00
}
2023-03-31 21:13:29 +02:00
curNode := node
2023-09-02 22:24:01 +02:00
location := node . Location ( )
for curNode . Parent ( ) != nil {
curNode = curNode . Parent ( )
curLocation := curNode . Location ( )
if curLocation == location {
2022-02-04 05:12:58 +02:00
return fmt . Errorf ( "task: include cycle detected between %s <--> %s" ,
2023-09-02 22:24:01 +02:00
curLocation ,
node . Parent ( ) . Location ( ) ,
2022-01-16 06:34:59 +02:00
)
}
}
return nil
}