commit 459905af538ef1b84c5a2368ef81a8de10c76dc6
parent 72b0b77e9bb4ea20ed6f59e953371e8d59e0767e
Author: Sean Enck <sean@ttypty.com>
Date: Sat, 5 Oct 2024 11:35:52 -0400
fixing up an expansion workaround
Diffstat:
4 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/internal/app/pwgen.go b/internal/app/pwgen.go
@@ -32,6 +32,7 @@ func GeneratePassword(cmd CommandOptions) error {
return fmt.Errorf("word count must be >= 1")
}
tmplString := config.EnvPasswordGenTemplate.Get()
+ tmplString = strings.ReplaceAll(tmplString, config.TemplateVariable, "$")
wordList, err := config.EnvPasswordGenWordList.Get()
if err != nil {
return err
diff --git a/internal/app/pwgen_test.go b/internal/app/pwgen_test.go
@@ -92,6 +92,8 @@ func TestGenerate(t *testing.T) {
os.Setenv("DOLLAR", "$")
os.Setenv("LOCKBOX_PWGEN_TEMPLATE", "{{range ${DOLLAR}idx, ${DOLLAR}val := .}}{{if lt ${DOLLAR}idx 5}}-{{end}}{{ ${DOLLAR}val }}{{end}}")
testPasswordGen(t, "-a-a-a-a")
+ os.Setenv("LOCKBOX_PWGEN_TEMPLATE", "{{range [%]idx, [%]val := .}}{{if lt [%]idx 5}}-{{end}}{{ [%]val }}{{end}}")
+ testPasswordGen(t, "-a-a-a-a")
os.Unsetenv("LOCKBOX_PWGEN_TEMPLATE")
os.Setenv("LOCKBOX_PWGEN_TITLE", "yes")
os.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s abc axy axY aZZZ aoijafea aoiajfoea afaeoa", pwgenPath))
diff --git a/internal/config/core.go b/internal/config/core.go
@@ -32,6 +32,8 @@ const (
genCategory keyCategory = "PWGEN_"
// YesValue are yes (on) values
YesValue = yes
+ // TemplateVariable is used to handle '$' in shell vars (due to expansion)
+ TemplateVariable = "[%]"
)
var (
diff --git a/internal/config/vars.go b/internal/config/vars.go
@@ -355,11 +355,11 @@ This value can NOT be an expansion itself.`,
// EnvPasswordGenTemplate is the output template for controlling how output words are placed together
EnvPasswordGenTemplate = environmentRegister(
EnvironmentString{
- environmentDefault: newDefaultedEnvironment("{{range $idx, $val := .}}{{if gt $idx 0}}-{{end}}{{ $val }}{{end}}",
+ environmentDefault: newDefaultedEnvironment(fmt.Sprintf("{{range %sidx, %sval := .}}{{if %sidx}}-{{end}}{{%sval}}{{end}}", TemplateVariable, TemplateVariable, TemplateVariable, TemplateVariable),
environmentBase{
subKey: "TEMPLATE",
cat: genCategory,
- desc: "The go text template to use to format the chosen words into a password.",
+ desc: fmt.Sprintf("The go text template to use to format the chosen words into a password (use '%s' to include a '$' to avoid shell expansion issues).", TemplateVariable),
}),
allowed: []string{"<go template>"},
canDefault: true,