lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 1eaf82cb895227ff970eef5237af22026158cbd6
parent f442abbe0c07611ec5d5c0ed2c82f8b5e68bc438
Author: Sean Enck <sean@ttypty.com>
Date:   Mon,  2 Jun 2025 22:18:21 -0400

text position handling belongs in config because that is where it is called

Diffstat:
Minternal/app/pwgen.go | 5++---
Minternal/config/core.go | 29+++++++++++++++++++++++++++++
Minternal/config/core_test.go | 7+++++++
Minternal/config/vars.go | 3+--
Minternal/util/reflect.go | 32--------------------------------
Minternal/util/reflect_test.go | 7-------
6 files changed, 39 insertions(+), 44 deletions(-)

diff --git a/internal/app/pwgen.go b/internal/app/pwgen.go @@ -12,7 +12,6 @@ import ( "text/template" "git.sr.ht/~enckse/lockbox/internal/config" - "git.sr.ht/~enckse/lockbox/internal/util" ) // GeneratePassword generates a password @@ -72,13 +71,13 @@ func GeneratePassword(cmd CommandOptions) error { if found == 0 { return errors.New("no sources given") } - var selected []util.Word + var selected []config.Word var cnt int64 totalLength := 0 for cnt < length { choice := choices[rand.Intn(found)] textLength := len(choice) - selected = append(selected, util.Word{Text: choice, Position: util.Position{Start: totalLength, End: totalLength + textLength}}) + selected = append(selected, config.Word{Text: choice, Position: config.Position{Start: totalLength, End: totalLength + textLength}}) totalLength += textLength cnt++ } diff --git a/internal/config/core.go b/internal/config/core.go @@ -6,6 +6,7 @@ import ( "net/url" "os" "path/filepath" + "reflect" "strconv" "strings" "time" @@ -80,6 +81,16 @@ type ( display() metaData self() environmentBase } + // Position is the start/end of a word in a greater set + Position struct { + Start int + End int + } + // Word is the text and position in a greater position + Word struct { + Text string + Position Position + } ) // NewConfigFiles will get the list of candidate config files @@ -150,3 +161,21 @@ func CanColor() (bool, error) { } return colors, nil } + +func readNested(v reflect.Type, root string) []string { + var fields []string + for i := range v.NumField() { + field := v.Field(i) + if field.Type.Kind() == reflect.Struct { + fields = append(fields, readNested(field.Type, fmt.Sprintf("%s.", field.Name))...) + } else { + fields = append(fields, fmt.Sprintf("%s%s", root, field.Name)) + } + } + return fields +} + +// TextPositionFields is the displayable set of templated fields +func TextPositionFields() string { + return strings.Join(readNested(reflect.TypeOf(Word{}), ""), ", ") +} diff --git a/internal/config/core_test.go b/internal/config/core_test.go @@ -62,3 +62,10 @@ func TestCanColor(t *testing.T) { t.Error("should NOT be able to color") } } + +func TestTextFields(t *testing.T) { + v := config.TextPositionFields() + if v != "Text, Position.Start, Position.End" { + t.Errorf("unexpected fields: %s", v) + } +} diff --git a/internal/config/vars.go b/internal/config/vars.go @@ -7,7 +7,6 @@ import ( "git.sr.ht/~enckse/lockbox/internal/output" "git.sr.ht/~enckse/lockbox/internal/platform" - "git.sr.ht/~enckse/lockbox/internal/util" ) var ( @@ -265,7 +264,7 @@ Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode), environmentDefault: newDefaultedEnvironment("{{range $i, $val := .}}{{if $i}}-{{end}}{{$val.Text}}{{end}}", environmentBase{ key: genCategory + "TEMPLATE", - description: fmt.Sprintf("The go text template to use to format the chosen words into a password. Available fields: %s.", util.TextPositionFields()), + description: fmt.Sprintf("The go text template to use to format the chosen words into a password. Available fields: %s.", TextPositionFields()), }), allowed: []string{"<go template>"}, flags: []stringsFlags{canDefaultFlag}, diff --git a/internal/util/reflect.go b/internal/util/reflect.go @@ -5,20 +5,6 @@ import ( "fmt" "reflect" "sort" - "strings" -) - -type ( - // Position is the start/end of a word in a greater set - Position struct { - Start int - End int - } - // Word is the text and position in a greater position - Word struct { - Text string - Position Position - } ) // ListFields will get the values of strings on an "all string" struct @@ -31,21 +17,3 @@ func ListFields(p any) []string { sort.Strings(vals) return vals } - -func readNested(v reflect.Type, root string) []string { - var fields []string - for i := range v.NumField() { - field := v.Field(i) - if field.Type.Kind() == reflect.Struct { - fields = append(fields, readNested(field.Type, fmt.Sprintf("%s.", field.Name))...) - } else { - fields = append(fields, fmt.Sprintf("%s%s", root, field.Name)) - } - } - return fields -} - -// TextPositionFields is the displayable set of templated fields -func TextPositionFields() string { - return strings.Join(readNested(reflect.TypeOf(Word{}), ""), ", ") -} diff --git a/internal/util/reflect_test.go b/internal/util/reflect_test.go @@ -18,10 +18,3 @@ func TestListFields(t *testing.T) { t.Errorf("invalid fields: %v", fields) } } - -func TestTextFields(t *testing.T) { - v := util.TextPositionFields() - if v != "Text, Position.Start, Position.End" { - t.Errorf("unexpected fields: %s", v) - } -}