2023-12-29 22:32:03 +02:00
package taskfile
2023-09-02 22:24:01 +02:00
import (
2023-09-12 23:42:54 +02:00
"context"
2024-01-25 14:22:10 +02:00
"os"
"path/filepath"
2023-09-02 22:24:01 +02:00
"strings"
2024-03-25 21:05:21 +02:00
"time"
2023-09-02 22:24:01 +02:00
2023-09-12 23:42:54 +02:00
"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"
2023-09-02 22:24:01 +02:00
)
type Node interface {
2023-09-12 23:42:54 +02:00
Read ( ctx context . Context ) ( [ ] byte , error )
2023-09-02 22:24:01 +02:00
Parent ( ) Node
Location ( ) string
2024-02-13 03:07:00 +02:00
Dir ( ) string
2023-09-12 23:42:54 +02:00
Remote ( ) bool
2024-02-13 21:29:28 +02:00
ResolveEntrypoint ( entrypoint string ) ( string , error )
ResolveDir ( dir string ) ( string , error )
2023-09-02 22:24:01 +02:00
}
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 ,
2024-03-25 21:05:21 +02:00
timeout time . Duration ,
2024-01-25 14:22:10 +02:00
) ( Node , error ) {
2024-03-04 13:34:44 +02:00
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 {
2024-01-25 14:36:31 +02:00
return NewStdinNode ( dir )
2024-01-25 14:22:10 +02:00
}
2024-03-25 21:05:21 +02:00
return NewNode ( l , entrypoint , dir , insecure , timeout )
2024-01-25 14:22:10 +02:00
}
2023-09-12 23:42:54 +02:00
func NewNode (
2024-02-13 03:07:00 +02:00
l * logger . Logger ,
entrypoint string ,
dir string ,
2023-09-12 23:42:54 +02:00
insecure bool ,
2024-03-25 21:05:21 +02:00
timeout time . Duration ,
2023-09-12 23:42:54 +02:00
opts ... NodeOption ,
) ( Node , error ) {
var node Node
var err error
2024-02-13 03:07:00 +02:00
switch getScheme ( entrypoint ) {
2023-09-12 23:42:54 +02:00
case "http" , "https" :
2024-03-25 21:05:21 +02:00
node , err = NewHTTPNode ( l , entrypoint , dir , insecure , timeout , opts ... )
2023-09-02 22:24:01 +02:00
default :
2023-09-12 23:42:54 +02:00
// 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 ... )
2023-09-12 23:42:54 +02:00
}
2023-12-23 04:33:12 +02:00
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" )
2023-09-02 22:24:01 +02:00
}
2023-09-12 23:42:54 +02:00
return node , err
2023-09-02 22:24:01 +02:00
}
func getScheme ( uri string ) string {
if i := strings . Index ( uri , "://" ) ; i != - 1 {
return uri [ : i ]
}
return ""
}
2024-03-04 13:34:44 +02:00
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
}