1
0
mirror of https://github.com/go-task/task.git synced 2025-05-13 22:16:31 +02:00

feat: unexport all node implementation fields

This commit is contained in:
Pete Davison 2025-05-01 18:13:51 +00:00
parent 39d172f1f4
commit d47bad9071
No known key found for this signature in database
7 changed files with 32 additions and 32 deletions

View File

@ -1,19 +1,19 @@
package taskfile package taskfile
type ( type (
NodeOption func(*BaseNode) NodeOption func(*baseNode)
// BaseNode is a generic node that implements the Parent() methods of the // baseNode is a generic node that implements the Parent() methods of the
// NodeReader interface. It does not implement the Read() method and it // NodeReader interface. It does not implement the Read() method and it
// designed to be embedded in other node types so that this boilerplate code // designed to be embedded in other node types so that this boilerplate code
// does not need to be repeated. // does not need to be repeated.
BaseNode struct { baseNode struct {
parent Node parent Node
dir string dir string
} }
) )
func NewBaseNode(dir string, opts ...NodeOption) *BaseNode { func NewBaseNode(dir string, opts ...NodeOption) *baseNode {
node := &BaseNode{ node := &baseNode{
parent: nil, parent: nil,
dir: dir, dir: dir,
} }
@ -27,15 +27,15 @@ func NewBaseNode(dir string, opts ...NodeOption) *BaseNode {
} }
func WithParent(parent Node) NodeOption { func WithParent(parent Node) NodeOption {
return func(node *BaseNode) { return func(node *baseNode) {
node.parent = parent node.parent = parent
} }
} }
func (node *BaseNode) Parent() Node { func (node *baseNode) Parent() Node {
return node.parent return node.parent
} }
func (node *BaseNode) Dir() string { func (node *baseNode) Dir() string {
return node.dir return node.dir
} }

View File

@ -11,13 +11,13 @@ import (
const remoteCacheDir = "remote" const remoteCacheDir = "remote"
type CacheNode struct { type CacheNode struct {
*BaseNode *baseNode
source RemoteNode source RemoteNode
} }
func NewCacheNode(source RemoteNode, dir string) *CacheNode { func NewCacheNode(source RemoteNode, dir string) *CacheNode {
return &CacheNode{ return &CacheNode{
BaseNode: &BaseNode{ baseNode: &baseNode{
dir: filepath.Join(dir, remoteCacheDir), dir: filepath.Join(dir, remoteCacheDir),
}, },
source: source, source: source,

View File

@ -13,8 +13,8 @@ import (
// A FileNode is a node that reads a taskfile from the local filesystem. // A FileNode is a node that reads a taskfile from the local filesystem.
type FileNode struct { type FileNode struct {
*BaseNode *baseNode
Entrypoint string entrypoint string
} }
func NewFileNode(entrypoint, dir string, opts ...NodeOption) (*FileNode, error) { func NewFileNode(entrypoint, dir string, opts ...NodeOption) (*FileNode, error) {
@ -25,13 +25,13 @@ func NewFileNode(entrypoint, dir string, opts ...NodeOption) (*FileNode, error)
return nil, err return nil, err
} }
return &FileNode{ return &FileNode{
BaseNode: base, baseNode: base,
Entrypoint: entrypoint, entrypoint: entrypoint,
}, nil }, nil
} }
func (node *FileNode) Location() string { func (node *FileNode) Location() string {
return node.Entrypoint return node.entrypoint
} }
func (node *FileNode) Read() ([]byte, error) { func (node *FileNode) Read() ([]byte, error) {
@ -63,7 +63,7 @@ func (node *FileNode) ResolveEntrypoint(entrypoint string) (string, error) {
// NOTE: Uses the directory of the entrypoint (Taskfile), not the current working directory // NOTE: Uses the directory of the entrypoint (Taskfile), not the current working directory
// This means that files are included relative to one another // This means that files are included relative to one another
entrypointDir := filepath.Dir(node.Entrypoint) entrypointDir := filepath.Dir(node.entrypoint)
return filepathext.SmartJoin(entrypointDir, path), nil return filepathext.SmartJoin(entrypointDir, path), nil
} }
@ -79,6 +79,6 @@ func (node *FileNode) ResolveDir(dir string) (string, error) {
// NOTE: Uses the directory of the entrypoint (Taskfile), not the current working directory // NOTE: Uses the directory of the entrypoint (Taskfile), not the current working directory
// This means that files are included relative to one another // This means that files are included relative to one another
entrypointDir := filepath.Dir(node.Entrypoint) entrypointDir := filepath.Dir(node.entrypoint)
return filepathext.SmartJoin(entrypointDir, path), nil return filepathext.SmartJoin(entrypointDir, path), nil
} }

View File

@ -21,8 +21,8 @@ import (
// An GitNode is a node that reads a Taskfile from a remote location via Git. // An GitNode is a node that reads a Taskfile from a remote location via Git.
type GitNode struct { type GitNode struct {
*BaseNode *baseNode
URL *url.URL url *url.URL
rawUrl string rawUrl string
ref string ref string
path string path string
@ -52,8 +52,8 @@ func NewGitNode(
return nil, &errors.TaskfileNotSecureError{URI: u.Redacted()} return nil, &errors.TaskfileNotSecureError{URI: u.Redacted()}
} }
return &GitNode{ return &GitNode{
BaseNode: base, baseNode: base,
URL: u, url: u,
rawUrl: rawUrl, rawUrl: rawUrl,
ref: ref, ref: ref,
path: path, path: path,
@ -76,7 +76,7 @@ func (node *GitNode) ReadContext(_ context.Context) ([]byte, error) {
fs := memfs.New() fs := memfs.New()
storer := memory.NewStorage() storer := memory.NewStorage()
_, err := git.Clone(storer, fs, &git.CloneOptions{ _, err := git.Clone(storer, fs, &git.CloneOptions{
URL: node.URL.String(), URL: node.url.String(),
ReferenceName: plumbing.ReferenceName(node.ref), ReferenceName: plumbing.ReferenceName(node.ref),
SingleBranch: true, SingleBranch: true,
Depth: 1, Depth: 1,
@ -99,7 +99,7 @@ func (node *GitNode) ReadContext(_ context.Context) ([]byte, error) {
func (node *GitNode) ResolveEntrypoint(entrypoint string) (string, error) { func (node *GitNode) ResolveEntrypoint(entrypoint string) (string, error) {
dir, _ := filepath.Split(node.path) dir, _ := filepath.Split(node.path)
resolvedEntrypoint := fmt.Sprintf("%s//%s", node.URL, filepath.Join(dir, entrypoint)) resolvedEntrypoint := fmt.Sprintf("%s//%s", node.url, filepath.Join(dir, entrypoint))
if node.ref != "" { if node.ref != "" {
return fmt.Sprintf("%s?ref=%s", resolvedEntrypoint, node.ref), nil return fmt.Sprintf("%s?ref=%s", resolvedEntrypoint, node.ref), nil
} }
@ -130,7 +130,7 @@ func (node *GitNode) CacheKey() string {
if len(lastDir) > 1 { if len(lastDir) > 1 {
prefix = fmt.Sprintf("%s.%s", lastDir, prefix) prefix = fmt.Sprintf("%s.%s", lastDir, prefix)
} }
return fmt.Sprintf("git.%s.%s.%s", node.URL.Host, prefix, checksum) return fmt.Sprintf("git.%s.%s.%s", node.url.Host, prefix, checksum)
} }
func splitURLOnDoubleSlash(u *url.URL) (string, string) { func splitURLOnDoubleSlash(u *url.URL) (string, string) {

View File

@ -15,7 +15,7 @@ func TestGitNode_ssh(t *testing.T) {
assert.Equal(t, "main", node.ref) assert.Equal(t, "main", node.ref)
assert.Equal(t, "Taskfile.yml", node.path) assert.Equal(t, "Taskfile.yml", node.path)
assert.Equal(t, "ssh://git@github.com/foo/bar.git//Taskfile.yml?ref=main", node.Location()) assert.Equal(t, "ssh://git@github.com/foo/bar.git//Taskfile.yml?ref=main", node.Location())
assert.Equal(t, "ssh://git@github.com/foo/bar.git", node.URL.String()) assert.Equal(t, "ssh://git@github.com/foo/bar.git", node.url.String())
entrypoint, err := node.ResolveEntrypoint("common.yml") entrypoint, err := node.ResolveEntrypoint("common.yml")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "ssh://git@github.com/foo/bar.git//common.yml?ref=main", entrypoint) assert.Equal(t, "ssh://git@github.com/foo/bar.git//common.yml?ref=main", entrypoint)
@ -29,7 +29,7 @@ func TestGitNode_sshWithDir(t *testing.T) {
assert.Equal(t, "main", node.ref) assert.Equal(t, "main", node.ref)
assert.Equal(t, "directory/Taskfile.yml", node.path) assert.Equal(t, "directory/Taskfile.yml", node.path)
assert.Equal(t, "ssh://git@github.com/foo/bar.git//directory/Taskfile.yml?ref=main", node.Location()) assert.Equal(t, "ssh://git@github.com/foo/bar.git//directory/Taskfile.yml?ref=main", node.Location())
assert.Equal(t, "ssh://git@github.com/foo/bar.git", node.URL.String()) assert.Equal(t, "ssh://git@github.com/foo/bar.git", node.url.String())
entrypoint, err := node.ResolveEntrypoint("common.yml") entrypoint, err := node.ResolveEntrypoint("common.yml")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "ssh://git@github.com/foo/bar.git//directory/common.yml?ref=main", entrypoint) assert.Equal(t, "ssh://git@github.com/foo/bar.git//directory/common.yml?ref=main", entrypoint)
@ -43,7 +43,7 @@ func TestGitNode_https(t *testing.T) {
assert.Equal(t, "main", node.ref) assert.Equal(t, "main", node.ref)
assert.Equal(t, "Taskfile.yml", node.path) assert.Equal(t, "Taskfile.yml", node.path)
assert.Equal(t, "https://github.com/foo/bar.git//Taskfile.yml?ref=main", node.Location()) assert.Equal(t, "https://github.com/foo/bar.git//Taskfile.yml?ref=main", node.Location())
assert.Equal(t, "https://github.com/foo/bar.git", node.URL.String()) assert.Equal(t, "https://github.com/foo/bar.git", node.url.String())
entrypoint, err := node.ResolveEntrypoint("common.yml") entrypoint, err := node.ResolveEntrypoint("common.yml")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "https://github.com/foo/bar.git//common.yml?ref=main", entrypoint) assert.Equal(t, "https://github.com/foo/bar.git//common.yml?ref=main", entrypoint)
@ -57,7 +57,7 @@ func TestGitNode_httpsWithDir(t *testing.T) {
assert.Equal(t, "main", node.ref) assert.Equal(t, "main", node.ref)
assert.Equal(t, "directory/Taskfile.yml", node.path) assert.Equal(t, "directory/Taskfile.yml", node.path)
assert.Equal(t, "https://github.com/foo/bar.git//directory/Taskfile.yml?ref=main", node.Location()) assert.Equal(t, "https://github.com/foo/bar.git//directory/Taskfile.yml?ref=main", node.Location())
assert.Equal(t, "https://github.com/foo/bar.git", node.URL.String()) assert.Equal(t, "https://github.com/foo/bar.git", node.url.String())
entrypoint, err := node.ResolveEntrypoint("common.yml") entrypoint, err := node.ResolveEntrypoint("common.yml")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "https://github.com/foo/bar.git//directory/common.yml?ref=main", entrypoint) assert.Equal(t, "https://github.com/foo/bar.git//directory/common.yml?ref=main", entrypoint)

View File

@ -16,7 +16,7 @@ import (
// An HTTPNode is a node that reads a Taskfile from a remote location via HTTP. // An HTTPNode is a node that reads a Taskfile from a remote location via HTTP.
type HTTPNode struct { type HTTPNode struct {
*BaseNode *baseNode
URL *url.URL // stores url pointing actual remote file. (e.g. with Taskfile.yml) URL *url.URL // stores url pointing actual remote file. (e.g. with Taskfile.yml)
} }
@ -35,7 +35,7 @@ func NewHTTPNode(
return nil, &errors.TaskfileNotSecureError{URI: url.Redacted()} return nil, &errors.TaskfileNotSecureError{URI: url.Redacted()}
} }
return &HTTPNode{ return &HTTPNode{
BaseNode: base, baseNode: base,
URL: url, URL: url,
}, nil }, nil
} }

View File

@ -12,12 +12,12 @@ import (
// A StdinNode is a node that reads a taskfile from the standard input stream. // A StdinNode is a node that reads a taskfile from the standard input stream.
type StdinNode struct { type StdinNode struct {
*BaseNode *baseNode
} }
func NewStdinNode(dir string) (*StdinNode, error) { func NewStdinNode(dir string) (*StdinNode, error) {
return &StdinNode{ return &StdinNode{
BaseNode: NewBaseNode(dir), baseNode: NewBaseNode(dir),
}, nil }, nil
} }