Skip to content

Commit

Permalink
Validate template syntax when building binary fixes gobuffalo#840
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates authored and EC2 Default User committed Jan 21, 2018
1 parent 9054e87 commit 26d1b62
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions buffalo/cmd/build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func New(ctx context.Context, opts Options) *Builder {

b.steps = []func() error{
b.prepTarget,
b.validateTemplates,
b.transformMain,
b.createBuildMain,
b.prepAPackage,
Expand Down
43 changes: 43 additions & 0 deletions buffalo/cmd/build/templates.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
package build

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/gobuffalo/packr"
"github.com/gobuffalo/plush"
"github.com/pkg/errors"
)

var templates = packr.NewBox("./templates")

func (b *Builder) validateTemplates() error {
errs := []string{}
err := filepath.Walk(filepath.Join(b.App.Root, "templates"), func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() {
return nil
}

ext := filepath.Ext(path)
if ext != ".html" && ext != ".md" {
return nil
}

b, err := ioutil.ReadFile(path)
if err != nil {
return errors.WithStack(err)
}

if _, err = plush.Parse(string(b)); err != nil {
errs = append(errs, fmt.Sprintf("template error in file %s: %s", path, err.Error()))
}

return nil
})

if err != nil {
return errors.WithStack(err)
}

if len(errs) > 0 {
return errors.New(strings.Join(errs, "\n"))
}

return nil
}

0 comments on commit 26d1b62

Please sign in to comment.