2023-12-29 20:32:03 +00:00
|
|
|
package taskfile
|
2023-09-02 15:24:01 -05:00
|
|
|
|
2023-09-12 16:42:54 -05:00
|
|
|
type (
|
|
|
|
NodeOption func(*BaseNode)
|
|
|
|
// 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.
|
|
|
|
BaseNode struct {
|
|
|
|
parent Node
|
|
|
|
optional bool
|
2024-02-13 01:07:00 +00:00
|
|
|
dir string
|
2023-09-12 16:42:54 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-03-04 18:00:28 +00:00
|
|
|
func NewBaseNode(dir string, opts ...NodeOption) *BaseNode {
|
2023-09-12 16:42:54 -05:00
|
|
|
node := &BaseNode{
|
|
|
|
parent: nil,
|
|
|
|
optional: false,
|
2024-03-04 18:00:28 +00:00
|
|
|
dir: dir,
|
2023-09-12 16:42:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply options
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithParent(parent Node) NodeOption {
|
|
|
|
return func(node *BaseNode) {
|
|
|
|
node.parent = parent
|
|
|
|
}
|
2023-09-02 15:24:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *BaseNode) Parent() Node {
|
|
|
|
return node.parent
|
|
|
|
}
|
|
|
|
|
2023-09-12 16:42:54 -05:00
|
|
|
func WithOptional(optional bool) NodeOption {
|
|
|
|
return func(node *BaseNode) {
|
|
|
|
node.optional = optional
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-02 15:24:01 -05:00
|
|
|
func (node *BaseNode) Optional() bool {
|
|
|
|
return node.optional
|
|
|
|
}
|
2024-02-13 01:07:00 +00:00
|
|
|
|
|
|
|
func (node *BaseNode) Dir() string {
|
|
|
|
return node.dir
|
|
|
|
}
|