2018-02-17 18:22:18 +02:00
|
|
|
package taskfile
|
|
|
|
|
2020-05-17 20:42:27 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2022-12-31 18:48:49 +02:00
|
|
|
"time"
|
2022-12-19 03:11:31 +02:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
2020-05-17 20:42:27 +02:00
|
|
|
)
|
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
// Taskfile represents a Taskfile.yml
|
|
|
|
type Taskfile struct {
|
2018-03-04 20:50:03 +02:00
|
|
|
Version string
|
|
|
|
Expansions int
|
2022-02-20 00:31:27 +02:00
|
|
|
Output Output
|
2019-09-09 03:26:24 +02:00
|
|
|
Method string
|
2021-01-01 23:27:50 +02:00
|
|
|
Includes *IncludedTaskfiles
|
2023-01-14 21:41:56 +02:00
|
|
|
Set []string
|
|
|
|
Shopt []string
|
2020-03-29 21:54:59 +02:00
|
|
|
Vars *Vars
|
|
|
|
Env *Vars
|
2018-03-04 20:50:03 +02:00
|
|
|
Tasks Tasks
|
2019-12-08 02:44:09 +02:00
|
|
|
Silent bool
|
2020-08-04 00:18:38 +02:00
|
|
|
Dotenv []string
|
2020-08-17 21:25:17 +02:00
|
|
|
Run string
|
2022-12-31 18:48:49 +02:00
|
|
|
Interval time.Duration
|
2018-02-17 18:22:18 +02:00
|
|
|
}
|
|
|
|
|
2022-12-19 03:11:31 +02:00
|
|
|
func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error {
|
|
|
|
switch node.Kind {
|
2022-09-08 19:22:44 +02:00
|
|
|
|
2022-12-19 03:11:31 +02:00
|
|
|
case yaml.MappingNode:
|
|
|
|
var taskfile struct {
|
|
|
|
Version string
|
|
|
|
Expansions int
|
|
|
|
Output Output
|
|
|
|
Method string
|
|
|
|
Includes *IncludedTaskfiles
|
2023-01-14 21:41:56 +02:00
|
|
|
Set []string
|
|
|
|
Shopt []string
|
2022-12-19 03:11:31 +02:00
|
|
|
Vars *Vars
|
|
|
|
Env *Vars
|
|
|
|
Tasks Tasks
|
|
|
|
Silent bool
|
|
|
|
Dotenv []string
|
|
|
|
Run string
|
2022-12-31 18:48:49 +02:00
|
|
|
Interval time.Duration
|
2022-12-19 03:11:31 +02:00
|
|
|
}
|
|
|
|
if err := node.Decode(&taskfile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tf.Version = taskfile.Version
|
|
|
|
tf.Expansions = taskfile.Expansions
|
|
|
|
tf.Output = taskfile.Output
|
|
|
|
tf.Method = taskfile.Method
|
|
|
|
tf.Includes = taskfile.Includes
|
2023-01-14 21:41:56 +02:00
|
|
|
tf.Set = taskfile.Set
|
|
|
|
tf.Shopt = taskfile.Shopt
|
2022-12-19 03:11:31 +02:00
|
|
|
tf.Vars = taskfile.Vars
|
|
|
|
tf.Env = taskfile.Env
|
|
|
|
tf.Tasks = taskfile.Tasks
|
|
|
|
tf.Silent = taskfile.Silent
|
|
|
|
tf.Dotenv = taskfile.Dotenv
|
|
|
|
tf.Run = taskfile.Run
|
|
|
|
tf.Interval = taskfile.Interval
|
|
|
|
if tf.Expansions <= 0 {
|
|
|
|
tf.Expansions = 2
|
|
|
|
}
|
|
|
|
if tf.Vars == nil {
|
|
|
|
tf.Vars = &Vars{}
|
|
|
|
}
|
|
|
|
if tf.Env == nil {
|
|
|
|
tf.Env = &Vars{}
|
|
|
|
}
|
|
|
|
return nil
|
2018-02-17 18:22:18 +02:00
|
|
|
}
|
2022-09-08 19:22:44 +02:00
|
|
|
|
2022-12-19 03:11:31 +02:00
|
|
|
return fmt.Errorf("yaml: line %d: cannot unmarshal %s into taskfile", node.Line, node.ShortTag())
|
2018-02-17 18:22:18 +02:00
|
|
|
}
|
2020-05-17 20:42:27 +02:00
|
|
|
|
|
|
|
// ParsedVersion returns the version as a float64
|
|
|
|
func (tf *Taskfile) ParsedVersion() (float64, error) {
|
|
|
|
v, err := strconv.ParseFloat(tf.Version, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf(`task: Could not parse taskfile version "%s": %v`, tf.Version, err)
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|