1
0
mirror of https://github.com/go-task/task.git synced 2025-03-19 21:17:46 +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,12 +45,12 @@ func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error {
Task string
Vars *Vars
}
if err := unmarshal(&taskCall); err == nil {
c.Task = taskCall.Task
c.Vars = taskCall.Vars
return nil
if err := unmarshal(&taskCall); err != nil {
return err
}
return ErrCantUnmarshalCmd
c.Task = taskCall.Task
c.Vars = taskCall.Vars
return nil
}
// UnmarshalYAML implements yaml.Unmarshaler interface
@ -72,10 +64,10 @@ func (d *Dep) UnmarshalYAML(unmarshal func(interface{}) error) error {
Task string
Vars *Vars
}
if err := unmarshal(&taskCall); err == nil {
d.Task = taskCall.Task
d.Vars = taskCall.Vars
return nil
if err := unmarshal(&taskCall); err != nil {
return err
}
return ErrCantUnmarshalDep
d.Task = taskCall.Task
d.Vars = taskCall.Vars
return nil
}

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 {
it.Dir = includedTaskfile.Dir
it.Taskfile = includedTaskfile.Taskfile
it.AdvancedImport = true
return nil
if err := unmarshal(&includedTaskfile); err != nil {
return err
}
return ErrCantUnmarshalIncludedTaskfile
it.Dir = includedTaskfile.Dir
it.Taskfile = includedTaskfile.Taskfile
it.AdvancedImport = true
return nil
}

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,26 +62,24 @@ func (t *Task) UnmarshalYAML(unmarshal func(interface{}) error) error {
Prefix string
IgnoreError bool `yaml:"ignore_error"`
}
if err := unmarshal(&task); err == nil {
t.Cmds = task.Cmds
t.Deps = task.Deps
t.Label = task.Label
t.Desc = task.Desc
t.Summary = task.Summary
t.Sources = task.Sources
t.Generates = task.Generates
t.Status = task.Status
t.Preconditions = task.Preconditions
t.Dir = task.Dir
t.Vars = task.Vars
t.Env = task.Env
t.Silent = task.Silent
t.Method = task.Method
t.Prefix = task.Prefix
t.IgnoreError = task.IgnoreError
return nil
if err := unmarshal(&task); err != nil {
return err
}
return ErrCantUnmarshalTask
t.Cmds = task.Cmds
t.Deps = task.Deps
t.Label = task.Label
t.Desc = task.Desc
t.Summary = task.Summary
t.Sources = task.Sources
t.Generates = task.Generates
t.Status = task.Status
t.Preconditions = task.Preconditions
t.Dir = task.Dir
t.Vars = task.Vars
t.Env = task.Env
t.Silent = task.Silent
t.Method = task.Method
t.Prefix = task.Prefix
t.IgnoreError = task.IgnoreError
return nil
}

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 {
v.Sh = sh.Sh
return nil
if err := unmarshal(&sh); err != nil {
return err
}
return ErrCantUnmarshalVar
v.Sh = sh.Sh
return nil
}