Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Validate template syntax when building binary fixes #840 #858

Merged
merged 4 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions buffalo/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ func init() {
xbuildCmd.Flags().StringVar(&buildOptions.LDFlags, "ldflags", "", "set any ldflags to be passed to the go build")
xbuildCmd.Flags().BoolVarP(&buildOptions.Debug, "debug", "d", false, "print debugging information")
xbuildCmd.Flags().BoolVarP(&buildOptions.Compress, "compress", "c", true, "compress static files in the binary")
xbuildCmd.Flags().BoolVar(&buildOptions.SkipTemplateValidation, "skip-template-validation", false, "skip validating plush templates")
xbuildCmd.Flags().StringVarP(&buildOptions.Environment, "environment", "", "development", "set the environment for the binary")
}
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
17 changes: 9 additions & 8 deletions buffalo/cmd/build/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
// Options for a build
type Options struct {
meta.App
ExtractAssets bool `json:"extract_assets"`
WithAssets bool `json:"with_assets"`
LDFlags string `json:"ld_flags"`
Tags []string `json:"tags"`
Static bool `json:"static"`
Debug bool `json:"debug"`
Compress bool `json:"compress"`
Environment string `json:"environment"`
ExtractAssets bool `json:"extract_assets"`
WithAssets bool `json:"with_assets"`
LDFlags string `json:"ld_flags"`
Tags []string `json:"tags"`
Static bool `json:"static"`
Debug bool `json:"debug"`
Compress bool `json:"compress"`
Environment string `json:"environment"`
SkipTemplateValidation bool `json:"skip_template_validation"`
}

func (o Options) String() string {
Expand Down
46 changes: 46 additions & 0 deletions buffalo/cmd/build/templates.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
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 {
if b.SkipTemplateValidation {
return nil
}
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
}