2023-12-29 20:32:03 +00:00
|
|
|
package taskfile
|
2023-09-02 15:24:01 -05:00
|
|
|
|
|
|
|
|
import (
|
2023-09-12 16:42:54 -05:00
|
|
|
"context"
|
2026-07-01 13:38:38 +01:00
|
|
|
"slices"
|
2023-09-02 15:24:01 -05:00
|
|
|
"strings"
|
2024-03-25 19:05:21 +00:00
|
|
|
"time"
|
2023-09-02 15:24:01 -05:00
|
|
|
|
2024-11-12 12:29:29 +04:00
|
|
|
giturls "github.com/chainguard-dev/git-urls"
|
2024-09-24 13:44:54 -04:00
|
|
|
|
2025-04-19 12:20:33 +01:00
|
|
|
"github.com/go-task/task/v3/internal/fsext"
|
2023-09-02 15:24:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Node interface {
|
2025-04-19 12:12:08 +01:00
|
|
|
Read() ([]byte, error)
|
2023-09-02 15:24:01 -05:00
|
|
|
Parent() Node
|
|
|
|
|
Location() string
|
2024-02-13 01:07:00 +00:00
|
|
|
Dir() string
|
2025-05-24 14:00:02 +01:00
|
|
|
Checksum() string
|
|
|
|
|
Verify(checksum string) bool
|
2024-02-13 19:29:28 +00:00
|
|
|
ResolveEntrypoint(entrypoint string) (string, error)
|
|
|
|
|
ResolveDir(dir string) (string, error)
|
2025-04-19 12:12:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type RemoteNode interface {
|
|
|
|
|
Node
|
|
|
|
|
ReadContext(ctx context.Context) ([]byte, error)
|
|
|
|
|
CacheKey() string
|
2023-09-02 15:24:01 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-25 12:22:10 +00:00
|
|
|
func NewRootNode(
|
|
|
|
|
entrypoint string,
|
2024-02-13 01:07:00 +00:00
|
|
|
dir string,
|
2024-01-25 12:22:10 +00:00
|
|
|
insecure bool,
|
2024-03-25 19:05:21 +00:00
|
|
|
timeout time.Duration,
|
2026-01-25 18:51:30 +01:00
|
|
|
opts ...NodeOption,
|
2024-01-25 12:22:10 +00:00
|
|
|
) (Node, error) {
|
2025-04-19 12:20:33 +01:00
|
|
|
dir = fsext.DefaultDir(entrypoint, dir)
|
2024-05-08 16:44:05 +01:00
|
|
|
// If the entrypoint is "-", we read from stdin
|
|
|
|
|
if entrypoint == "-" {
|
2024-01-25 12:36:31 +00:00
|
|
|
return NewStdinNode(dir)
|
2024-01-25 12:22:10 +00:00
|
|
|
}
|
2026-01-25 18:51:30 +01:00
|
|
|
return NewNode(entrypoint, dir, insecure, opts...)
|
2024-01-25 12:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-12 16:42:54 -05:00
|
|
|
func NewNode(
|
2024-02-13 01:07:00 +00:00
|
|
|
entrypoint string,
|
|
|
|
|
dir string,
|
2023-09-12 16:42:54 -05:00
|
|
|
insecure bool,
|
|
|
|
|
opts ...NodeOption,
|
|
|
|
|
) (Node, error) {
|
|
|
|
|
var node Node
|
|
|
|
|
var err error
|
2025-04-19 12:12:08 +01:00
|
|
|
|
2024-09-24 13:44:54 -04:00
|
|
|
scheme, err := getScheme(entrypoint)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2025-04-19 12:12:08 +01:00
|
|
|
|
2024-09-24 13:44:54 -04:00
|
|
|
switch scheme {
|
|
|
|
|
case "git":
|
|
|
|
|
node, err = NewGitNode(entrypoint, dir, insecure, opts...)
|
2023-09-12 16:42:54 -05:00
|
|
|
case "http", "https":
|
2025-04-19 12:12:08 +01:00
|
|
|
node, err = NewHTTPNode(entrypoint, dir, insecure, opts...)
|
2023-09-02 15:24:01 -05:00
|
|
|
default:
|
2025-02-22 16:00:37 +00:00
|
|
|
node, err = NewFileNode(entrypoint, dir, opts...)
|
2023-09-12 16:42:54 -05:00
|
|
|
}
|
2025-04-19 12:12:08 +01:00
|
|
|
|
2023-09-12 16:42:54 -05:00
|
|
|
return node, err
|
2023-09-02 15:24:01 -05:00
|
|
|
}
|
|
|
|
|
|
2026-06-29 20:25:25 +02:00
|
|
|
func IsRemoteEntrypoint(entrypoint string) bool {
|
2025-10-12 06:29:57 -05:00
|
|
|
scheme, _ := getScheme(entrypoint)
|
|
|
|
|
switch scheme {
|
|
|
|
|
case "git", "http", "https":
|
|
|
|
|
return true
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 13:44:54 -04:00
|
|
|
func getScheme(uri string) (string, error) {
|
|
|
|
|
u, err := giturls.Parse(uri)
|
|
|
|
|
if u == nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2025-04-19 12:12:08 +01:00
|
|
|
|
2026-07-01 13:38:38 +01:00
|
|
|
isDotGit := strings.HasSuffix(strings.Split(u.Path, "//")[0], ".git")
|
|
|
|
|
isUnderscoreGit := strings.Contains(strings.Split(u.Path, "//")[0], "/_git/")
|
|
|
|
|
schemeIsGitCompatible := slices.Contains([]string{"git", "ssh", "https", "http"}, u.Scheme)
|
|
|
|
|
if (isDotGit || isUnderscoreGit) && schemeIsGitCompatible {
|
2024-09-24 13:44:54 -04:00
|
|
|
return "git", nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 02:13:15 +02:00
|
|
|
if before, _, ok := strings.Cut(uri, "://"); ok {
|
|
|
|
|
return before, nil
|
2023-09-02 15:24:01 -05:00
|
|
|
}
|
2025-04-19 12:12:08 +01:00
|
|
|
|
2024-09-24 13:44:54 -04:00
|
|
|
return "", nil
|
2023-09-02 15:24:01 -05:00
|
|
|
}
|