Prompt for template variables values based on the available manifest

This commit is contained in:
2020-02-21 14:29:26 +01:00
parent f5d5afa82d
commit d9e446553c
7 changed files with 217 additions and 73 deletions

View File

@ -5,10 +5,9 @@ import (
"net/url"
"os"
"gitlab.com/wpetit/scaffold/internal/template"
"forge.cadoles.com/wpetit/scaffold/internal/template"
"github.com/manifoldco/promptui"
"gitlab.com/wpetit/scaffold/internal/project"
"forge.cadoles.com/wpetit/scaffold/internal/project"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
@ -65,23 +64,36 @@ func newProjectAction(c *cli.Context) error {
return errors.Wrap(err, "could not fetch project")
}
manifestFile := c.String("manifest")
templateData := template.Data{}
manifestStat, err := vfs.Stat(manifestFile)
if err != nil && !os.IsNotExist(err) {
return errors.Wrap(err, "could not stat manifest file")
}
manifestPath := c.String("manifest")
templateData := make(map[string]interface{})
if os.IsNotExist(err) {
log.Println("Could not find scaffold manifest.")
} else {
if manifestStat.IsDir() {
return errors.New("scaffold manifest is not a file")
manifestFile, err := vfs.Open(manifestPath)
if err != nil {
if !os.IsNotExist(err) {
return errors.Wrapf(err, "could not read manifest '%s'", manifestPath)
}
log.Println("Could not find scaffold manifest.")
}
if manifestFile != nil {
defer manifestFile.Close()
log.Println("Loading template scaffold manifest...")
manifest, err := template.Load(manifestFile)
if err != nil {
return errors.Wrapf(err, "could not load manifest '%s'", manifestPath)
}
if err := manifest.Validate(); err != nil {
return errors.Wrapf(err, "could not validate manifest '%s'", manifestPath)
}
if err := template.Prompt(manifest, templateData); err != nil {
return errors.Wrap(err, "could not prompt for template data")
}
}
directory := c.String("directory")
@ -89,51 +101,6 @@ func newProjectAction(c *cli.Context) error {
return template.CopyDir(vfs, ".", directory, &template.Option{
TemplateData: templateData,
TemplateExt: ".gotpl",
IgnorePatterns: []string{manifestFile},
IgnorePatterns: []string{manifestPath},
})
}
func promptForProjectName() (string, error) {
validate := func(input string) error {
if input == "" {
return errors.New("Project name cannot be empty")
}
return nil
}
prompt := promptui.Prompt{
Label: "Project Name",
Validate: validate,
}
return prompt.Run()
}
func promptForProjectNamespace() (string, error) {
validate := func(input string) error {
if input == "" {
return errors.New("Project namespace cannot be empty")
}
return nil
}
prompt := promptui.Prompt{
Label: "Project namespace",
Validate: validate,
}
return prompt.Run()
}
func promptForProjectType() (string, error) {
prompt := promptui.Select{
Label: "Project Type",
Items: []string{"web"},
}
_, result, err := prompt.Run()
return result, err
}