lockbox

password manager
Log | Files | Refs | README | LICENSE

commit d609ba9b24c906e44356acf3c5686b44a01b6a2b
parent 5fef86fa1e9774907a3c27b46b7f6ac70d014163
Author: Sean Enck <sean@ttypty.com>
Date:   Sat,  5 Oct 2024 18:59:32 -0400

allow templating fields of gen to have additional components

Diffstat:
Minternal/app/pwgen.go | 11+++++++++--
Minternal/app/pwgen_test.go | 6+++---
Minternal/config/vars.go | 4++--
3 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/internal/app/pwgen.go b/internal/app/pwgen.go @@ -103,10 +103,17 @@ func GeneratePassword(cmd CommandOptions) error { choices[n] = x } } - var selected []string + type word struct { + Text string + Length int + } + var selected []word cnt := 0 + totalLength := 0 for cnt < length { - selected = append(selected, choices[cnt]) + w := word{choices[cnt], totalLength} + selected = append(selected, w) + totalLength += len(w.Text) cnt++ } tmpl, err := template.New("t").Parse(tmplString) diff --git a/internal/app/pwgen_test.go b/internal/app/pwgen_test.go @@ -90,9 +90,9 @@ func TestGenerate(t *testing.T) { testPasswordGen(t, "a-a-a-a") // NOTE: this allows templating below in golang 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}}") + os.Setenv("LOCKBOX_PWGEN_TEMPLATE", "{{range ${DOLLAR}idx, ${DOLLAR}val := .}}{{if lt ${DOLLAR}idx 5}}-{{end}}{{ ${DOLLAR}val.Text }}{{ ${DOLLAR}val.Length }}{{end}}") + testPasswordGen(t, "-a0-a1-a2-a3") + os.Setenv("LOCKBOX_PWGEN_TEMPLATE", "{{range [%]idx, [%]val := .}}{{if lt [%]idx 5}}-{{end}}{{ [%]val.Text }}{{end}}") testPasswordGen(t, "-a-a-a-a") os.Unsetenv("LOCKBOX_PWGEN_TEMPLATE") os.Setenv("LOCKBOX_PWGEN_TITLE", "yes") 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(fmt.Sprintf("{{range %sidx, %sval := .}}{{if %sidx}}-{{end}}{{%sval}}{{end}}", TemplateVariable, TemplateVariable, TemplateVariable, TemplateVariable), + environmentDefault: newDefaultedEnvironment(fmt.Sprintf("{{range %si, %sval := .}}{{if %si}}-{{end}}{{%sval.Text}}{{end}}", TemplateVariable, TemplateVariable, TemplateVariable, TemplateVariable), environmentBase{ subKey: "TEMPLATE", cat: genCategory, - 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), + 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). Fields available are Text and Length (current length of all words)", TemplateVariable), }), allowed: []string{"<go template>"}, canDefault: true,