github.com/gobuffalo/buffalo-cli/v2@v2.0.0-alpha.15.0.20200919213536-a7350c8e6799/cli/internal/plugins/plush/validator/validator.go (about)

     1  package validator
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/gobuffalo/buffalo-cli/v2/cli/cmds/build"
    12  	"github.com/gobuffalo/plugins"
    13  	"github.com/gobuffalo/plush/v4"
    14  )
    15  
    16  var _ build.BeforeBuilder = &Validator{}
    17  var _ plugins.Plugin = Validator{}
    18  
    19  type Validator struct{}
    20  
    21  func (b Validator) PluginName() string {
    22  	return "plush/validator"
    23  }
    24  
    25  func (b *Validator) BeforeBuild(ctx context.Context, root string, args []string) error {
    26  	root = filepath.Join(root, "templates")
    27  	err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
    28  		if err != nil {
    29  			return err
    30  		}
    31  
    32  		if info.IsDir() {
    33  			return nil
    34  		}
    35  
    36  		base := filepath.Base(path)
    37  		if !strings.Contains(base, ".plush") {
    38  			return nil
    39  		}
    40  
    41  		b, err := ioutil.ReadFile(path)
    42  		if err != nil {
    43  			return err
    44  		}
    45  
    46  		if _, err = plush.Parse(string(b)); err != nil {
    47  			return fmt.Errorf("could not parse %s: %v", path, err)
    48  		}
    49  		return nil
    50  	})
    51  	return plugins.Wrap(b, err)
    52  }