2018-02-17 18:22:18 +02:00
|
|
|
package taskfile
|
2017-07-02 20:30:50 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-07-06 01:10:45 +02:00
|
|
|
// Cmd is a task command
|
2017-07-02 20:30:50 +02:00
|
|
|
type Cmd struct {
|
2017-07-20 01:20:24 +02:00
|
|
|
Cmd string
|
|
|
|
Silent bool
|
|
|
|
Task string
|
|
|
|
Vars Vars
|
2017-07-02 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 01:10:45 +02:00
|
|
|
// Dep is a task dependency
|
2017-07-02 20:30:50 +02:00
|
|
|
type Dep struct {
|
2017-07-06 01:07:24 +02:00
|
|
|
Task string
|
|
|
|
Vars Vars
|
2017-07-02 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2017-07-06 01:10:45 +02:00
|
|
|
// ErrCantUnmarshalCmd is returned for invalid command YAML
|
2017-07-02 20:30:50 +02:00
|
|
|
ErrCantUnmarshalCmd = errors.New("task: can't unmarshal cmd value")
|
2017-07-06 01:10:45 +02:00
|
|
|
// ErrCantUnmarshalDep is returned for invalid dependency YAML
|
2017-07-02 20:30:50 +02:00
|
|
|
ErrCantUnmarshalDep = errors.New("task: can't unmarshal dep value")
|
|
|
|
)
|
|
|
|
|
2017-07-06 01:10:45 +02:00
|
|
|
// UnmarshalYAML implements yaml.Unmarshaler interface
|
2017-07-02 20:30:50 +02:00
|
|
|
func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var cmd string
|
|
|
|
if err := unmarshal(&cmd); err == nil {
|
|
|
|
if strings.HasPrefix(cmd, "^") {
|
|
|
|
c.Task = strings.TrimPrefix(cmd, "^")
|
|
|
|
} else {
|
|
|
|
c.Cmd = cmd
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-20 01:20:24 +02:00
|
|
|
var cmdStruct struct {
|
|
|
|
Cmd string
|
|
|
|
Silent bool
|
|
|
|
}
|
|
|
|
if err := unmarshal(&cmdStruct); err == nil && cmdStruct.Cmd != "" {
|
|
|
|
c.Cmd = cmdStruct.Cmd
|
|
|
|
c.Silent = cmdStruct.Silent
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-02 20:30:50 +02:00
|
|
|
var taskCall struct {
|
2017-07-06 01:07:24 +02:00
|
|
|
Task string
|
|
|
|
Vars Vars
|
2017-07-02 20:30:50 +02:00
|
|
|
}
|
|
|
|
if err := unmarshal(&taskCall); err == nil {
|
|
|
|
c.Task = taskCall.Task
|
2017-07-06 01:07:24 +02:00
|
|
|
c.Vars = taskCall.Vars
|
2017-07-02 20:30:50 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ErrCantUnmarshalCmd
|
|
|
|
}
|
|
|
|
|
2017-07-06 01:10:45 +02:00
|
|
|
// UnmarshalYAML implements yaml.Unmarshaler interface
|
2017-07-02 20:30:50 +02:00
|
|
|
func (d *Dep) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var task string
|
|
|
|
if err := unmarshal(&task); err == nil {
|
|
|
|
d.Task = task
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var taskCall struct {
|
2017-07-06 01:07:24 +02:00
|
|
|
Task string
|
|
|
|
Vars Vars
|
2017-07-02 20:30:50 +02:00
|
|
|
}
|
|
|
|
if err := unmarshal(&taskCall); err == nil {
|
|
|
|
d.Task = taskCall.Task
|
2017-07-06 01:07:24 +02:00
|
|
|
d.Vars = taskCall.Vars
|
2017-07-02 20:30:50 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ErrCantUnmarshalDep
|
|
|
|
}
|