Compare commits

..

No commits in common. "10689e98438fefdd6ead81efa169bb96f94b0632" and "4981a14e14908c4b15ff4a22a600b61a49508126" have entirely different histories.

9 changed files with 28 additions and 100 deletions

2
FAQ.md
View File

@ -113,7 +113,7 @@ I realize that many users will be using GitHub, and most will likely still be us
## Backup and Restore
1. The simplest solution is to make a copy of your `registry.yaml` (default: `~/.tmpl/registry.yaml`).
1. The simplest solution is to make a copy of your `registry.toml` (default: `~/.tmpl/registry.toml`).
* Once in the new location, you will need to use `tmpl restore`.
2. Alternatively, you can copy/paste the entire registry (default: `~/.tmpl`) and skip the restore step.

View File

@ -2,8 +2,6 @@ package cmd
import (
"fmt"
"os"
"path"
"strings"
"go.jolheiser.com/tmpl/env"
@ -17,7 +15,7 @@ var Download = &cli.Command{
Name: "download",
Usage: "Download a template",
Description: "Download a template and save it to the local registry",
ArgsUsage: "[repository URL] <name>",
ArgsUsage: "[repository URL] [name]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "branch",
@ -31,7 +29,7 @@ var Download = &cli.Command{
}
func runDownload(ctx *cli.Context) error {
if ctx.NArg() < 1 {
if ctx.NArg() < 2 {
return cli.ShowCommandHelp(ctx, ctx.Command.Name)
}
@ -69,7 +67,7 @@ func runDownload(ctx *cli.Context) error {
cloneURL += ".git"
}
t, err := reg.DownloadTemplate(deriveName(ctx), cloneURL, ctx.String("branch"))
t, err := reg.DownloadTemplate(ctx.Args().Get(1), cloneURL, ctx.String("branch"))
if err != nil {
return err
}
@ -77,19 +75,3 @@ func runDownload(ctx *cli.Context) error {
log.Info().Msgf("Added new template %q", t.Name)
return nil
}
func deriveName(ctx *cli.Context) string {
if ctx.NArg() > 1 {
return ctx.Args().Get(1)
}
envBranch, envSet := os.LookupEnv("TMPL_BRANCH")
flagBranch, flagSet := ctx.String("branch"), ctx.IsSet("branch")
if flagSet {
if !envSet || envBranch != flagBranch {
return flagBranch
}
}
return path.Base(ctx.Args().First())
}

View File

@ -14,25 +14,13 @@ 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 {
ID string `yaml:"id"`
Label string `yaml:"label"`
Help string `yaml:"help"`
Default string `yaml:"default"`
Options []string `yaml:"options"`
Type PromptType `yaml:"type"`
ID string `yaml:"id"`
Label string `yaml:"label"`
Help string `yaml:"help"`
Default string `yaml:"default"`
Options []string `yaml:"options"`
}
// Load loads a tmpl config

View File

@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"text/template"
@ -56,8 +55,7 @@ func prompt(dir string, defaults bool) (templatePrompts, error) {
// 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,26 +65,7 @@ func prompt(dir string, defaults bool) (templatePrompts, error) {
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,
}
case config.PromptTypeMultiline:
p = &survey.Multiline{
Message: prompt.Label,
Default: os.ExpandEnv(prompt.Default),
Help: prompt.Help,
}
case config.PromptTypeEditor:
p = &survey.Editor{
Message: prompt.Label,
Default: os.ExpandEnv(prompt.Default),
Help: prompt.Help,
}
default:
} else {
p = &survey.Input{
Message: prompt.Label,
Default: os.ExpandEnv(prompt.Default),
@ -94,13 +73,12 @@ func prompt(dir string, defaults bool) (templatePrompts, error) {
}
}
m := make(map[string]any)
if err := survey.AskOne(p, &m); err != nil {
return nil, fmt.Errorf("could not complete prompt: %w", err)
var a string
if err := survey.AskOne(p, &a); err != nil {
return nil, err
}
a := m[""]
prompts[idx].Value = a
os.Setenv(fmt.Sprintf("TMPL_PROMPT_%s", envKey), fmt.Sprint(a))
os.Setenv(fmt.Sprintf("TMPL_PROMPT_%s", envKey), a)
}
return prompts, nil
@ -108,7 +86,7 @@ func prompt(dir string, defaults bool) (templatePrompts, error) {
type templatePrompt struct {
config.Prompt
Value any
Value string
}
type templatePrompts []templatePrompt

View File

@ -2,6 +2,7 @@ package registry
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -222,7 +223,7 @@ func create(regFile string) error {
}
func download(cloneURL, branch, dest string) error {
tmp, err := os.MkdirTemp(os.TempDir(), "tmpl")
tmp, err := ioutil.TempDir(os.TempDir(), "tmpl")
if err != nil {
return err
}

View File

@ -3,6 +3,7 @@ package registry
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -66,7 +67,7 @@ func testGetFail(t *testing.T) {
func setupTemplate() {
var err error
tmplDir, err = os.MkdirTemp(os.TempDir(), "tmpl-setup")
tmplDir, err = ioutil.TempDir(os.TempDir(), "tmpl-setup")
if err != nil {
panic(err)
}
@ -114,7 +115,7 @@ func setupTemplate() {
func setupRegistry() {
var err error
regDir, err = os.MkdirTemp(os.TempDir(), "tmpl-reg")
regDir, err = ioutil.TempDir(os.TempDir(), "tmpl-reg")
if err != nil {
panic(err)
}

View File

@ -3,6 +3,7 @@ package registry
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -35,7 +36,7 @@ func (t *Template) ArchivePath() string {
// Execute runs the Template and copies to dest
func (t *Template) Execute(dest string, defaults, overwrite bool) error {
tmp, err := os.MkdirTemp(os.TempDir(), "tmpl")
tmp, err := ioutil.TempDir(os.TempDir(), "tmpl")
if err != nil {
return err
}
@ -61,7 +62,7 @@ func (t *Template) Execute(dest string, defaults, overwrite bool) error {
return nil
}
contents, err := os.ReadFile(walkPath)
contents, err := ioutil.ReadFile(walkPath)
if err != nil {
return err
}

View File

@ -1,6 +1,7 @@
package registry
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -50,7 +51,7 @@ func testExecute(t *testing.T) {
// Check contents of file
testPath := filepath.Join(destDir, "TEST")
contents, err := os.ReadFile(testPath)
contents, err := ioutil.ReadFile(testPath)
assert.NoErr(err) // Should be able to read TEST file
assert.Equal(string(contents), tmplGold) // Template should match golden file
@ -65,7 +66,7 @@ func testExecute(t *testing.T) {
assert.True(err != nil) // .tmplkeep file should NOT be retained
// Change file to test non-overwrite
err = os.WriteFile(testPath, []byte(tmplNewGold), os.ModePerm)
err = ioutil.WriteFile(testPath, []byte(tmplNewGold), os.ModePerm)
assert.NoErr(err) // Writing file should succeed
err = tmpl.Execute(destDir, true, false)

View File

@ -43,32 +43,8 @@
"items": {
"type": "string"
}
},
"type": {
"description": "The type of prompt",
"type": "string",
"enum": ["input", "multi", "select", "confirm", "editor"]
}
},
"anyOf": [
{
"properties": {
"type": {
"const": "select"
}
},
"required": ["options"]
},
{
"properties": {
"type": {
"not": {
"const": "select"
}
}
}
}
]
}
}
}
}