Skip to content

Commit

Permalink
parse: fix format for multi error report (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored Jun 20, 2020
1 parent 7bc4df4 commit 16035fe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
14 changes: 6 additions & 8 deletions cmd/envsubst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func main() {
fmt.Fprint(os.Stderr, fmt.Sprintf(usage))
}
flag.Parse()
// Reader
var reader *bufio.Reader
if *input != "" {
file, err := os.Open(*input)
Expand All @@ -50,7 +49,7 @@ func main() {
}
reader = bufio.NewReader(os.Stdin)
}
// Collect data
// Collect input data.
var data string
for {
line, err := reader.ReadString('\n')
Expand All @@ -63,9 +62,10 @@ func main() {
}
data += line
}
// Writer
var file *os.File
var err error
var (
err error
file *os.File
)
if *output != "" {
file, err = os.Create(*output)
if err != nil {
Expand All @@ -80,9 +80,7 @@ func main() {
parserMode = parse.Quick
}
restrictions := &parse.Restrictions{*noUnset, *noEmpty}

result, err := (*&parse.Parser{Name: "string", Env: os.Environ(), Restrict: restrictions, Mode: parserMode}).Parse(data)

result, err := (&parse.Parser{Name: "string", Env: os.Environ(), Restrict: restrictions, Mode: parserMode}).Parse(data)
if err != nil {
errorAndExit(err)
}
Expand Down
34 changes: 21 additions & 13 deletions parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ package parse

import (
"errors"
"fmt"
"strings"
)

// A mode value is a set of flags (or 0). They control parser behavior.
type Mode int

type Restrictions struct {
NoUnset bool
NoEmpty bool
}

// Mode for parser behaviour
const (
Quick Mode = iota // stop parsing after first error encoutered and return
AllErrors // report all errors
)

// The restrictions option controls the parsring restriction.
type Restrictions struct {
NoUnset bool
NoEmpty bool
}

// Restrictions specifier
var (
Relaxed = &Restrictions{false, false}
Expand Down Expand Up @@ -54,33 +55,40 @@ func New(name string, env []string, r *Restrictions) *Parser {
func (p *Parser) Parse(text string) (string, error) {
p.lex = lex(text)
// Build internal array of all unset or empty vars here
var allErrors string
var errs []error
// clean parse state
p.nodes = make([]Node, 0)
p.peekCount = 0
if err := p.parse(); err != nil {
switch *&p.Mode {
switch p.Mode {
case Quick:
return "", err
case AllErrors:
allErrors += fmt.Sprintf("%s\n", err.Error())
errs = append(errs, err)
}
}
var out string
for _, node := range p.nodes {
s, err := node.String()
if err != nil {
switch *&p.Mode {
switch p.Mode {
case Quick:
return "", err
case AllErrors:
allErrors += fmt.Sprintf("%s\n", err.Error())
errs = append(errs, err)
}
}
out += s
}
if allErrors != "" {
return "", fmt.Errorf("%s", allErrors)
if len(errs) > 0 {
var b strings.Builder
for i, err := range errs {
if i > 0 {
b.WriteByte('\n')
}
b.WriteString(err.Error())
}
return "", errors.New(b.String())
}
return out, nil
}
Expand Down
2 changes: 1 addition & 1 deletion parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ var parseTests = []parseTest{
}

var negativeParseTests = []parseTest{
{"$NOTSET and EMPTY are displayed as in full error output", "${NOTSET} and $EMPTY", "variable ${NOTSET} not set\nvariable ${EMPTY} set but empty\n", errAllFull},
{"$NOTSET and EMPTY are displayed as in full error output", "${NOTSET} and $EMPTY", "variable ${NOTSET} not set\nvariable ${EMPTY} set but empty", errAllFull},
}

func TestParse(t *testing.T) {
Expand Down

0 comments on commit 16035fe

Please sign in to comment.