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

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