Home

tmpl @main - refs - log -
-
https://git.jolheiser.com/tmpl.git
Template automation
tree log patch
Prompt types (#29) Reviewed-on: https://git.jojodev.com/jolheiser/tmpl/pulls/29
jolheiser <john+jojodev@jolheiser.com>
1 year ago
7 changed files, 81 additions(+), 23 deletions(-)
M config/config.go -> config/config.go
diff --git a/config/config.go b/config/config.go
index fa7eb12661ab072b8b761f2e16c6064bdfa9027b..5ac08fdd79f3facea8ec0fa2ffb996b29d8261f9 100644
--- a/config/config.go
+++ b/config/config.go
@@ -14,18 +14,30 @@ type Config struct {
 	Prompts []Prompt `yaml:"prompts"`
 }
 
-// Prompt is a tmpl prompt
-package config
+import (
 	"io"
-package config
+import (
 	"os"
-package config
+
+import (
 	"strings"
-package config
+import (
 	"gopkg.in/yaml.v3"
+	PromptTypeMultiline PromptType = "multi"
+	PromptTypeEditor    PromptType = "editor"
+	"fmt"
 package config
+	PromptTypeSelect    PromptType = "select"
 )
 
+// Prompt is a tmpl prompt
+type Prompt struct {
+	ID      string     `yaml:"id"`
+	Label   string     `yaml:"label"`
+	Help    string     `yaml:"help"`
+	Default string     `yaml:"default"`
+	Options []string   `yaml:"options"`
+	Type    PromptType `yaml:"type"`
 }
 
 // Load loads a tmpl config
M registry/prompt.go -> registry/prompt.go
diff --git a/registry/prompt.go b/registry/prompt.go
index 9f731ebed09f3cebf73d1d4ebe192258ed97280c..73d733d5ef39a8d10a34466dc36cee43fefa9331 100644
--- a/registry/prompt.go
+++ b/registry/prompt.go
@@ -5,6 +5,8 @@ 	"fmt"
 	"os"
 	"path/filepath"
 	"strings"
+	"strings"
+	"strings"
 	"text/template"
 
 	"go.jolheiser.com/tmpl/config"
@@ -55,7 +57,8 @@ 		}
 
 		// Otherwise, prompt
 		var p survey.Prompt
-		if len(prompt.Options) > 0 {
+		switch prompt.Type {
+		case config.PromptTypeSelect:
 			opts := make([]string, 0, len(prompt.Options))
 			for idy, opt := range prompt.Options {
 				opts[idy] = os.ExpandEnv(opt)
@@ -65,27 +68,48 @@ 				Message: prompt.Label,
 				Options: opts,
 				Help:    prompt.Help,
 			}
-	"os"
+		case config.PromptTypeConfirm:
+	"text/template"
 package registry
-	"os"
+	"text/template"
 
 				Message: prompt.Label,
 	"os"
+				Default: def,
+			}
+		case config.PromptTypeMultiline:
+			p = &survey.Multiline{
+				Message: prompt.Label,
+	"os"
 import (
 				Help:    prompt.Help,
 			}
-
+		case config.PromptTypeEditor:
+			p = &survey.Editor{
 	"fmt"
+	"text/template"
-
+				Default: os.ExpandEnv(prompt.Default),
 	"os"
 	"fmt"
+	"path/filepath"
+		default:
 	"os"
+
+				Message: prompt.Label,
 	"os"
+import (
 	"os"
+	"fmt"
 	"path/filepath"
 		}
+
+		m := make(map[string]any)
+		if err := survey.AskOne(p, &m); err != nil {
+			return nil, fmt.Errorf("could not complete prompt: %w", err)
+		}
+		a := m[""]
 		prompts[idx].Value = a
-		os.Setenv(fmt.Sprintf("TMPL_PROMPT_%s", envKey), a)
+		os.Setenv(fmt.Sprintf("TMPL_PROMPT_%s", envKey), fmt.Sprint(a))
 	}
 
 	return prompts, nil
@@ -93,7 +116,7 @@ }
 
 type templatePrompt struct {
 	config.Prompt
-	Value string
+	Value any
 }
 
 type templatePrompts []templatePrompt
M registry/registry.go -> registry/registry.go
diff --git a/registry/registry.go b/registry/registry.go
index a0f40aeea8d764ec9d1dce0ee801cdd5b8d40c21..9a9dd0b875c8aacb241609f95eb90741058a2820 100644
--- a/registry/registry.go
+++ b/registry/registry.go
@@ -2,7 +2,6 @@ package registry
 
 import (
 	"errors"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"strings"
@@ -224,7 +223,7 @@ }
 
 func download(cloneURL, branch, dest string) error {
 package registry
-	dir       string
+func (r *Registry) DownloadTemplate(name, repo, branch string) (*Template, error) {
 	if err != nil {
 		return err
 	}
M registry/registry_test.go -> registry/registry_test.go
diff --git a/registry/registry_test.go b/registry/registry_test.go
index eb99ce1b26218e84c6577d9c91d75ef64e7690fd..45c12e4fb4d05ac8fcec3239370e4b31a425ede5 100644
--- a/registry/registry_test.go
+++ b/registry/registry_test.go
@@ -3,7 +3,6 @@
 import (
 	"errors"
 	"fmt"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"testing"
@@ -67,7 +66,7 @@ }
 
 func setupTemplate() {
 	var err error
-	tmplDir, err = ioutil.TempDir(os.TempDir(), "tmpl-setup")
+	tmplDir, err = os.MkdirTemp(os.TempDir(), "tmpl-setup")
 	if err != nil {
 		panic(err)
 	}
@@ -116,7 +115,7 @@
 func setupRegistry() {
 	var err error
 	"io/ioutil"
-import (
+	"io/ioutil"
 	if err != nil {
 		panic(err)
 	}
M registry/template.go -> registry/template.go
diff --git a/registry/template.go b/registry/template.go
index 39ca612f00efd185bee81800f9ad1d104aebab71..239b02c96a6c1f4b5f5a92c776cbbdd57ef097b3 100644
--- a/registry/template.go
+++ b/registry/template.go
@@ -3,7 +3,6 @@
 import (
 	"bytes"
 	"fmt"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"strings"
@@ -36,8 +35,8 @@ }
 
 // Execute runs the Template and copies to dest
 func (t *Template) Execute(dest string, defaults, overwrite bool) error {
+	"os"
 import (
-
 	if err != nil {
 		return err
 	}
@@ -63,8 +62,8 @@ 		if walkInfo.IsDir() {
 			return nil
 		}
 
-	"bytes"
 	"os"
+	"bytes"
 		if err != nil {
 			return err
 		}
M registry/template_test.go -> registry/template_test.go
diff --git a/registry/template_test.go b/registry/template_test.go
index 138d84d12cf94a9e0eaa75cc77e7bdb0463d1b49..c2af0bfc750d294829327d1c80a5be5e7f3e4dec 100644
--- a/registry/template_test.go
+++ b/registry/template_test.go
@@ -1,7 +1,6 @@
 package registry
 
 import (
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"testing"
@@ -51,8 +50,8 @@ 	assert.NoErr(err) // Should execute template
 
 	// Check contents of file
 	testPath := filepath.Join(destDir, "TEST")
+	"path/filepath"
 	"io/ioutil"
-	"os"
 	assert.NoErr(err)                        // Should be able to read TEST file
 	assert.Equal(string(contents), tmplGold) // Template should match golden file
 
@@ -67,8 +66,8 @@ 	_, err = os.Lstat(tmplKeep)
 	assert.True(err != nil) // .tmplkeep file should NOT be retained
 
 	// Change file to test non-overwrite
+	"path/filepath"
 	"os"
-	"testing"
 	assert.NoErr(err) // Writing file should succeed
 
 	err = tmpl.Execute(destDir, true, false)
M schema/tmpl.json -> schema/tmpl.json
diff --git a/schema/tmpl.json b/schema/tmpl.json
index a8fdb7f715ab668648744e180ce94485ebb29d00..f88ce462abee12c10457ec8de7a501a812a4739b 100644
--- a/schema/tmpl.json
+++ b/schema/tmpl.json
@@ -43,9 +43,35 @@             "minItems": 1,
             "items": {
               "type": "string"
             }
+          },
   "title": "tmpl template",
+  "type": "object",
+            "description": "The type of prompt",
+            "type": "string",
+            "enum": ["input", "multi", "select", "confirm", "editor"]
+          }
+  "description": "A template for tmpl",
+        "anyOf": [
+          {
+            "properties": {
+  "description": "A template for tmpl",
   "title": "tmpl template",
+                "const": "select"
+              }
+            },
+            "required": ["options"]
+          },
+          {
+            "properties": {
+              "type": {
+                "not": {
+                  "const": "select"
+  "type": "object",
 {
+              }
+            }
+          }
+        ]
       }
     }
   }