Skip to content
Snippets Groups Projects
Commit aa7891d6 authored by Tamer Tas's avatar Tamer Tas
Browse files

Validate templates by doing a test run

parent aa1ee39f
No related branches found
No related tags found
No related merge requests found
......@@ -7,8 +7,8 @@ var Root = &cli.Command{
}
func Run() {
// TODO trap c-c to rollback transactions
// TODO use command factories instead of global command variables
Init.PersistentFlags().BoolP("force", "f", false, "Recreate directories if they exist")
Root.AddCommand(Init)
......
......@@ -22,6 +22,8 @@ func TemplateInRegistry(name string) (bool, error) {
return ok, nil
}
// TODO add --use-defaults flag to execute a template without user prompts
// TODO add --use-cache flag to execute a template from previous answers to prompts
var Use = &cli.Command{
Use: "use <template-name> <target-dir>",
Short: "Executes a project template",
......@@ -50,12 +52,12 @@ var Use = &cli.Command{
}
if err := tmpl.Execute(targetDir); err != nil {
// Delete if execute transaction fails
// Deletes the target dir if execute transaction fails
defer os.RemoveAll(targetDir)
exit.Fatal(fmt.Errorf("use: %s", err))
}
exit.OK("Successfully executed the project template %v on %v", tmplName, targetDir)
exit.OK("Successfully executed the project template %v in %v", tmplName, targetDir)
},
}
......@@ -3,8 +3,11 @@ package util
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/tmrts/tmplt/pkg/template"
"github.com/tmrts/tmplt/pkg/util/osutil"
"github.com/tmrts/tmplt/pkg/util/validate"
)
......@@ -45,6 +48,23 @@ func ValidateArgs(args []string, validations []validate.Argument) error {
return nil
}
func testTemplate(path string) error {
tmpDir, err := ioutil.TempDir("", "tmplt-validation-test")
if err != nil {
return err
} else {
defer os.RemoveAll(tmpDir)
}
tmpl, err := template.Get(path)
if err != nil {
return err
}
// TODO add --use-defaults flag to stop asking for user input
return tmpl.Execute(tmpDir)
}
func ValidateTemplate(tmplPath string) (bool, error) {
if exists, err := osutil.DirExists(tmplPath); !exists {
if err != nil {
......@@ -62,5 +82,9 @@ func ValidateTemplate(tmplPath string) (bool, error) {
return false, fmt.Errorf("template should contain %q directory", "template")
}
if err := testTemplate(tmplPath); err != nil {
return false, err
}
return true, nil
}
......@@ -24,6 +24,6 @@ var Validate = &cli.Command{
MustValidateTemplate(templatePath)
exit.OK("Template is valid.")
exit.OK("Template is valid")
},
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment