1
0
mirror of https://github.com/go-task/task.git synced 2025-06-29 00:51:43 +02:00

Improve YAML parse error reporting

Fixes #467
This commit is contained in:
Andrey Nering
2021-04-17 17:12:39 -03:00
parent 43a1f1314e
commit 2d66a2f0f3
5 changed files with 43 additions and 70 deletions

View File

@ -2,6 +2,10 @@
## Unreleased
- Improve error reporting when parsing YAML: in some situations where you
would just see an generic error, you'll now see the actual error with
more detail: the YAML line the failed to parse, for exemple
[#467](https://github.com/go-task/task/issues/467).
- A JSON Schema was published [here](https://json.schemastore.org/taskfile.json)
- and is automatically being used by some editors like Visual Studio Code
([#135](https://github.com/go-task/task/issues/135)).

View File

@ -1,7 +1,6 @@
package taskfile
import (
"errors"
"strings"
)
@ -20,13 +19,6 @@ type Dep struct {
Vars *Vars
}
var (
// ErrCantUnmarshalCmd is returned for invalid command YAML
ErrCantUnmarshalCmd = errors.New("task: can't unmarshal cmd value")
// ErrCantUnmarshalDep is returned for invalid dependency YAML
ErrCantUnmarshalDep = errors.New("task: can't unmarshal dep value")
)
// UnmarshalYAML implements yaml.Unmarshaler interface
func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error {
var cmd string
@ -53,13 +45,13 @@ func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error {
Task string
Vars *Vars
}
if err := unmarshal(&taskCall); err == nil {
if err := unmarshal(&taskCall); err != nil {
return err
}
c.Task = taskCall.Task
c.Vars = taskCall.Vars
return nil
}
return ErrCantUnmarshalCmd
}
// UnmarshalYAML implements yaml.Unmarshaler interface
func (d *Dep) UnmarshalYAML(unmarshal func(interface{}) error) error {
@ -72,10 +64,10 @@ func (d *Dep) UnmarshalYAML(unmarshal func(interface{}) error) error {
Task string
Vars *Vars
}
if err := unmarshal(&taskCall); err == nil {
if err := unmarshal(&taskCall); err != nil {
return err
}
d.Task = taskCall.Task
d.Vars = taskCall.Vars
return nil
}
return ErrCantUnmarshalDep
}

View File

@ -6,11 +6,6 @@ import (
"gopkg.in/yaml.v3"
)
var (
// ErrCantUnmarshalIncludedTaskfile is returned for invalid var YAML.
ErrCantUnmarshalIncludedTaskfile = errors.New("task: can't unmarshal included value")
)
// IncludedTaskfile represents information about included tasksfile
type IncludedTaskfile struct {
Taskfile string
@ -98,12 +93,11 @@ func (it *IncludedTaskfile) UnmarshalYAML(unmarshal func(interface{}) error) err
Taskfile string
Dir string
}
if err := unmarshal(&includedTaskfile); err == nil {
if err := unmarshal(&includedTaskfile); err != nil {
return err
}
it.Dir = includedTaskfile.Dir
it.Taskfile = includedTaskfile.Taskfile
it.AdvancedImport = true
return nil
}
return ErrCantUnmarshalIncludedTaskfile
}

View File

@ -1,9 +1,5 @@
package taskfile
import (
"errors"
)
// Tasks represents a group of tasks
type Tasks map[string]*Task
@ -28,11 +24,6 @@ type Task struct {
IgnoreError bool
}
var (
// ErrCantUnmarshalTask is returned for invalid task YAML
ErrCantUnmarshalTask = errors.New("task: can't unmarshal task value")
)
func (t *Task) Name() string {
if t.Label != "" {
return t.Label
@ -71,7 +62,9 @@ func (t *Task) UnmarshalYAML(unmarshal func(interface{}) error) error {
Prefix string
IgnoreError bool `yaml:"ignore_error"`
}
if err := unmarshal(&task); err == nil {
if err := unmarshal(&task); err != nil {
return err
}
t.Cmds = task.Cmds
t.Deps = task.Deps
t.Label = task.Label
@ -88,9 +81,5 @@ func (t *Task) UnmarshalYAML(unmarshal func(interface{}) error) error {
t.Method = task.Method
t.Prefix = task.Prefix
t.IgnoreError = task.IgnoreError
return nil
}
return ErrCantUnmarshalTask
}

View File

@ -7,11 +7,6 @@ import (
"gopkg.in/yaml.v3"
)
var (
// ErrCantUnmarshalVar is returned for invalid var YAML.
ErrCantUnmarshalVar = errors.New("task: can't unmarshal var value")
)
// Vars is a string[string] variables map.
type Vars struct {
Keys []string
@ -124,10 +119,9 @@ func (v *Var) UnmarshalYAML(unmarshal func(interface{}) error) error {
var sh struct {
Sh string
}
if err := unmarshal(&sh); err == nil {
if err := unmarshal(&sh); err != nil {
return err
}
v.Sh = sh.Sh
return nil
}
return ErrCantUnmarshalVar
}