Skip to content

Commit f204b96

Browse files
committed
Add taskfile node for byte[] objects.
1 parent 2b713f5 commit f204b96

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

taskfile/node_byte.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package taskfile
2+
3+
import (
4+
"github.com/go-task/task/v3/internal/execext"
5+
"github.com/go-task/task/v3/internal/filepathext"
6+
)
7+
8+
// A ByteNode is a node that reads a taskfile direct from a []byte object.
9+
type ByteNode struct {
10+
*baseNode
11+
data []byte
12+
}
13+
14+
func NewByteNode(data []byte, dir string) (*ByteNode, error) {
15+
return &ByteNode{
16+
baseNode: NewBaseNode(dir),
17+
data: data,
18+
}, nil
19+
}
20+
21+
func (node *ByteNode) Location() string {
22+
return "__bytes__"
23+
}
24+
25+
func (node *ByteNode) Remote() bool {
26+
return true
27+
}
28+
29+
func (node *ByteNode) Read() ([]byte, error) {
30+
return node.data, nil
31+
}
32+
33+
func (node *ByteNode) ResolveEntrypoint(entrypoint string) (string, error) {
34+
// A ByteNode has no presence on the local file system.
35+
return entrypoint, nil
36+
}
37+
38+
func (node *ByteNode) ResolveDir(dir string) (string, error) {
39+
path, err := execext.ExpandLiteral(dir)
40+
if err != nil {
41+
return "", err
42+
}
43+
44+
if filepathext.IsAbs(path) {
45+
return path, nil
46+
}
47+
48+
return filepathext.SmartJoin(node.Dir(), path), nil
49+
}

taskfile/node_byte_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package taskfile
2+
3+
import (
4+
_ "embed"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
//go:embed testdata/node_byte_taskfile.yaml
11+
var taskfileYamlBytes []byte
12+
13+
func TestByteNode(t *testing.T) {
14+
t.Parallel()
15+
workingDir := t.TempDir()
16+
17+
node, err := NewByteNode(taskfileYamlBytes, workingDir)
18+
assert.NoError(t, err)
19+
assert.Equal(t, "__bytes__", node.Location())
20+
assert.Equal(t, workingDir, node.Dir())
21+
assert.True(t, node.Remote())
22+
data, err := node.Read()
23+
assert.NoError(t, err)
24+
assert.Equal(t, taskfileYamlBytes, data)
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: '3'
2+
3+
vars:
4+
GREETING: Hello, World!
5+
6+
tasks:
7+
default:
8+
cmds:
9+
- echo "{{.GREETING}}"
10+
silent: true

0 commit comments

Comments
 (0)