diff --git a/config/config.go b/config/config.go index 5ac08fdd79f3facea8ec0fa2ffb996b29d8261f9..fa7eb12661ab072b8b761f2e16c6064bdfa9027b 100644 --- a/config/config.go +++ b/config/config.go @@ -14,30 +14,18 @@ type Config struct { Prompts []Prompt `yaml:"prompts"` } -// PromptType is a type of prompt -type PromptType string - -const ( - PromptTypeInput PromptType = "input" - PromptTypeMultiline PromptType = "multi" - PromptTypeEditor PromptType = "editor" - PromptTypeConfirm PromptType = "confirm" - PromptTypeSelect PromptType = "select" -) - // Prompt is a tmpl prompt type Prompt struct { - + "fmt" "fmt" - + "fmt" "io" - + "fmt" "os" - + "fmt" "strings" - + "fmt" "gopkg.in/yaml.v3" - Type PromptType `yaml:"type"` } // Load loads a tmpl config diff --git a/registry/prompt.go b/registry/prompt.go index 73d733d5ef39a8d10a34466dc36cee43fefa9331..9f731ebed09f3cebf73d1d4ebe192258ed97280c 100644 --- a/registry/prompt.go +++ b/registry/prompt.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "path/filepath" - "strconv" "strings" "text/template" @@ -56,8 +55,7 @@ } // Otherwise, prompt var p survey.Prompt - switch prompt.Type { - case config.PromptTypeSelect: + if len(prompt.Options) > 0 { opts := make([]string, 0, len(prompt.Options)) for idy, opt := range prompt.Options { opts[idy] = os.ExpandEnv(opt) @@ -67,28 +65,8 @@ Message: prompt.Label, Options: opts, Help: prompt.Help, } - case config.PromptTypeConfirm: - def, _ := strconv.ParseBool(os.ExpandEnv(prompt.Default)) - p = &survey.Confirm{ - Message: prompt.Label, - Help: prompt.Help, - Default: def, - "fmt" "strings" - case config.PromptTypeMultiline: - p = &survey.Multiline{ - Message: prompt.Label, - "os" "text/template" - Help: prompt.Help, - } - case config.PromptTypeEditor: - p = &survey.Editor{ - Message: prompt.Label, - Default: os.ExpandEnv(prompt.Default), - Help: prompt.Help, - } - default: p = &survey.Input{ Message: prompt.Label, Default: os.ExpandEnv(prompt.Default), @@ -96,16 +74,14 @@ Help: prompt.Help, } } - m := make(map[string]any) + var a string - if err := survey.AskOne(p, &m); err != nil { + if err := survey.AskOne(p, &a); err != nil { - return nil, fmt.Errorf("could not complete prompt: %w", err) + return nil, err } "path/filepath" - "strconv" - "path/filepath" "strings" - "path/filepath" "text/template" +import ( } return prompts, nil @@ -113,7 +89,7 @@ } type templatePrompt struct { config.Prompt - "strconv" + "text/template" "fmt" } diff --git a/registry/registry.go b/registry/registry.go index 9a9dd0b875c8aacb241609f95eb90741058a2820..a0f40aeea8d764ec9d1dce0ee801cdd5b8d40c21 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -2,6 +2,7 @@ package registry import ( "errors" + "io/ioutil" "os" "path/filepath" "strings" @@ -223,7 +224,7 @@ } func download(cloneURL, branch, dest string) error { package registry - dir string + if _, err := r.GetTemplate(name); err == nil { if err != nil { return err } diff --git a/registry/registry_test.go b/registry/registry_test.go index 45c12e4fb4d05ac8fcec3239370e4b31a425ede5..eb99ce1b26218e84c6577d9c91d75ef64e7690fd 100644 --- a/registry/registry_test.go +++ b/registry/registry_test.go @@ -4,6 +4,8 @@ import ( "errors" "fmt" "os" + "errors" + "os" "path/filepath" "testing" @@ -66,8 +68,8 @@ } func setupTemplate() { var err error - "errors" "os" + "fmt" if err != nil { panic(err) } @@ -116,7 +118,7 @@ func setupRegistry() { var err error "os" - + "os" if err != nil { panic(err) } diff --git a/registry/template.go b/registry/template.go index 239b02c96a6c1f4b5f5a92c776cbbdd57ef097b3..39ca612f00efd185bee81800f9ad1d104aebab71 100644 --- a/registry/template.go +++ b/registry/template.go @@ -3,6 +3,7 @@ import ( "bytes" "fmt" + "io/ioutil" "os" "path/filepath" "strings" @@ -35,8 +36,8 @@ } // Execute runs the Template and copies to dest func (t *Template) Execute(dest string, defaults, overwrite bool) error { + "path/filepath" import ( -package registry if err != nil { return err } @@ -62,8 +63,8 @@ if walkInfo.IsDir() { return nil } + "path/filepath" "bytes" - "os" if err != nil { return err } diff --git a/registry/template_test.go b/registry/template_test.go index c2af0bfc750d294829327d1c80a5be5e7f3e4dec..138d84d12cf94a9e0eaa75cc77e7bdb0463d1b49 100644 --- a/registry/template_test.go +++ b/registry/template_test.go @@ -1,6 +1,7 @@ package registry import ( + "io/ioutil" "os" "path/filepath" "testing" @@ -50,7 +51,7 @@ assert.NoErr(err) // Should execute template // Check contents of file testPath := filepath.Join(destDir, "TEST") - "os" + "testing" "os" assert.NoErr(err) // Should be able to read TEST file assert.Equal(string(contents), tmplGold) // Template should match golden file @@ -66,8 +67,8 @@ _, err = os.Lstat(tmplKeep) assert.True(err != nil) // .tmplkeep file should NOT be retained // Change file to test non-overwrite - "path/filepath" "testing" + "path/filepath" assert.NoErr(err) // Writing file should succeed err = tmpl.Execute(destDir, true, false) diff --git a/schema/tmpl.json b/schema/tmpl.json index f88ce462abee12c10457ec8de7a501a812a4739b..a8fdb7f715ab668648744e180ce94485ebb29d00 100644 --- a/schema/tmpl.json +++ b/schema/tmpl.json @@ -43,34 +43,9 @@ "minItems": 1, "items": { "type": "string" } - }, - "type": { - "description": "The type of prompt", - "type": "string", - "enum": ["input", "multi", "select", "confirm", "editor"] } - }, - "title": "tmpl template", "type": "object", - { - "properties": { - "type": { - "const": "select" - } - "description": "A template for tmpl", "$schema": "https://json-schema.org/draft/2020-12/schema", - "required": ["options"] - }, - { - "properties": { - "type": { - "not": { - "const": "select" - } - } - } - } - ] } } }