Fix 00_helper filename by truncating singletons.

- Also add better error output for when a formatting error occurs.
This commit is contained in:
Aaron L 2016-06-19 22:20:38 -07:00
parent 7c2a04ba4d
commit 5fbcb27f3f

View file

@ -2,13 +2,15 @@ package main
import (
"bytes"
"errors"
"fmt"
"go/format"
"io"
"os"
"path/filepath"
"regexp"
"text/template"
"github.com/pkg/errors"
)
var testHarnessStdout io.Writer = os.Stdout
@ -108,6 +110,8 @@ func executeTemplates(e executeTemplateData) error {
}
func executeSingletonTemplates(e executeTemplateData) error {
rgxRemove := regexp.MustCompile(`[0-9]+_`)
for _, template := range e.templates {
resp, err := executeTemplate(template, e.data)
if err != nil {
@ -125,7 +129,7 @@ func executeSingletonTemplates(e executeTemplateData) error {
err = outHandler(
e.state.Config.OutFolder,
fName+e.fileSuffix,
rgxRemove.ReplaceAllString(fName, "")+e.fileSuffix,
e.state.Config.PkgName,
imps,
[][]byte{resp},
@ -200,12 +204,12 @@ func outHandler(outFolder string, fileName string, pkgName string, imps imports,
func executeTemplate(t *template.Template, data *templateData) ([]byte, error) {
var buf bytes.Buffer
if err := t.Execute(&buf, data); err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to execute template")
}
output, err := format.Source(buf.Bytes())
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to format template")
}
return output, nil