commit 2e66dd61afbb2963c1d773aa59d88c8b96d3411b
parent d609ba9b24c906e44356acf3c5686b44a01b6a2b
Author: Sean Enck <sean@ttypty.com>
Date: Sat, 5 Oct 2024 19:36:54 -0400
handle start/end position
Diffstat:
3 files changed, 16 insertions(+), 7 deletions(-)
diff --git a/internal/app/pwgen.go b/internal/app/pwgen.go
@@ -103,17 +103,26 @@ func GeneratePassword(cmd CommandOptions) error {
choices[n] = x
}
}
+ type position struct {
+ Start int
+ End int
+ }
type word struct {
- Text string
- Length int
+ Text string
+ Position position
}
var selected []word
cnt := 0
totalLength := 0
for cnt < length {
- w := word{choices[cnt], totalLength}
+ choice := choices[cnt]
+ textLength := len(choice)
+ pos := position{}
+ pos.Start = totalLength
+ pos.End = pos.Start + textLength
+ w := word{choices[cnt], pos}
selected = append(selected, w)
- totalLength += len(w.Text)
+ totalLength += textLength
cnt++
}
tmpl, err := template.New("t").Parse(tmplString)
diff --git a/internal/app/pwgen_test.go b/internal/app/pwgen_test.go
@@ -90,8 +90,8 @@ 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.Text }}{{ ${DOLLAR}val.Length }}{{end}}")
- testPasswordGen(t, "-a0-a1-a2-a3")
+ os.Setenv("LOCKBOX_PWGEN_TEMPLATE", "{{range ${DOLLAR}idx, ${DOLLAR}val := .}}{{if lt ${DOLLAR}idx 5}}-{{end}}{{ ${DOLLAR}val.Text }}{{ ${DOLLAR}val.Position.Start }}{{ ${DOLLAR}val.Position.End }}{{end}}")
+ testPasswordGen(t, "-a01-a12-a23-a34")
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")
diff --git a/internal/config/vars.go b/internal/config/vars.go
@@ -359,7 +359,7 @@ This value can NOT be an expansion itself.`,
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). Fields available are Text and Length (current length of all words)", 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, Position.Start, and Position.End.", TemplateVariable),
}),
allowed: []string{"<go template>"},
canDefault: true,