-
-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: node reader interface * refactor: rewrite Taskfile() as anon recursive func * chore: NewNodeFromIncludedTaskfile * chore: changelog
- Loading branch information
Showing
7 changed files
with
271 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package read | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/go-task/task/v3/taskfile" | ||
) | ||
|
||
type Node interface { | ||
Read() (*taskfile.Taskfile, error) | ||
Parent() Node | ||
Optional() bool | ||
Location() string | ||
} | ||
|
||
func NewNodeFromIncludedTaskfile(parent Node, includedTaskfile taskfile.IncludedTaskfile) (Node, error) { | ||
switch getScheme(includedTaskfile.Taskfile) { | ||
// TODO: Add support for other schemes. | ||
// If no other scheme matches, we assume it's a file. | ||
// This also allows users to explicitly set a file:// scheme. | ||
default: | ||
path, err := includedTaskfile.FullTaskfilePath() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewFileNode(parent, path, includedTaskfile.Optional) | ||
} | ||
} | ||
|
||
func getScheme(uri string) string { | ||
if i := strings.Index(uri, "://"); i != -1 { | ||
return uri[:i] | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package read | ||
|
||
// BaseNode is a generic node that implements the Parent() and Optional() | ||
// methods of the NodeReader interface. It does not implement the Read() method | ||
// and it designed to be embedded in other node types so that this boilerplate | ||
// code does not need to be repeated. | ||
type BaseNode struct { | ||
parent Node | ||
optional bool | ||
} | ||
|
||
func (node *BaseNode) Parent() Node { | ||
return node.parent | ||
} | ||
|
||
func (node *BaseNode) Optional() bool { | ||
return node.optional | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package read | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/go-task/task/v3/errors" | ||
"github.com/go-task/task/v3/internal/filepathext" | ||
"github.com/go-task/task/v3/taskfile" | ||
) | ||
|
||
// A FileNode is a node that reads a taskfile from the local filesystem. | ||
type FileNode struct { | ||
BaseNode | ||
Dir string | ||
Entrypoint string | ||
} | ||
|
||
func NewFileNode(parent Node, path string, optional bool) (*FileNode, error) { | ||
path, err := exists(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &FileNode{ | ||
BaseNode: BaseNode{ | ||
parent: parent, | ||
optional: optional, | ||
}, | ||
Dir: filepath.Dir(path), | ||
Entrypoint: filepath.Base(path), | ||
}, nil | ||
} | ||
|
||
func (node *FileNode) Location() string { | ||
return filepathext.SmartJoin(node.Dir, node.Entrypoint) | ||
} | ||
|
||
func (node *FileNode) Read() (*taskfile.Taskfile, error) { | ||
if node.Dir == "" { | ||
d, err := os.Getwd() | ||
if err != nil { | ||
return nil, err | ||
} | ||
node.Dir = d | ||
} | ||
|
||
path, err := existsWalk(node.Location()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
node.Dir = filepath.Dir(path) | ||
node.Entrypoint = filepath.Base(path) | ||
|
||
f, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
var t taskfile.Taskfile | ||
if err := yaml.NewDecoder(f).Decode(&t); err != nil { | ||
return nil, &errors.TaskfileInvalidError{URI: filepathext.TryAbsToRel(path), Err: err} | ||
} | ||
|
||
t.Location = path | ||
return &t, nil | ||
} |
Oops, something went wrong.