lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 819627a83a2ba898993f0ce03b362f657f6d80a3
parent 9147839be29a09faea850eb4bc90d2befd44c130
Author: Sean Enck <sean@ttypty.com>
Date:   Sun,  8 Dec 2024 17:13:08 -0500

cleanup metadata/print interface

Diffstat:
Minternal/config/core.go | 3+--
Minternal/config/env.go | 87+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
Minternal/config/toml.go | 22+++++++++++-----------
Minternal/config/vars.go | 8++++----
4 files changed, 70 insertions(+), 50 deletions(-)

diff --git a/internal/config/core.go b/internal/config/core.go @@ -61,9 +61,8 @@ var ( type ( printer interface { - values() (string, []string) + display() metaData self() environmentBase - toml() (tomlType, string, bool) } ) diff --git a/internal/config/env.go b/internal/config/env.go @@ -34,7 +34,7 @@ type ( canDefault bool allowed []string isArray bool - expand bool + canExpand bool } // EnvironmentArray are settings that are parsed as shell commands EnvironmentArray struct { @@ -46,6 +46,13 @@ type ( allowed string fxn func(string, string) string } + metaData struct { + value string + allowed []string + tomlType tomlType + tomlValue string + canExpand bool + } ) func (e environmentBase) Key() string { @@ -110,53 +117,67 @@ func (e EnvironmentFormatter) Get(value string) string { return e.fxn(e.Key(), value) } -func (e EnvironmentString) values() (string, []string) { - return e.value, e.allowed +func (e EnvironmentString) display() metaData { + var t tomlType + t = tomlString + v := "\"\"" + if e.isArray { + t = tomlArray + v = "[]" + } + return metaData{ + value: e.value, + allowed: e.allowed, + tomlType: t, + tomlValue: v, + canExpand: e.canExpand, + } } func (e environmentBase) self() environmentBase { return e } -func (e EnvironmentBool) values() (string, []string) { +func (e EnvironmentBool) display() metaData { val := NoValue if e.value { val = YesValue } - return val, []string{YesValue, NoValue} -} - -func (e EnvironmentInt) values() (string, []string) { - return fmt.Sprintf("%d", e.value), []string{"<integer>"} -} - -func (e EnvironmentFormatter) values() (string, []string) { - return strings.ReplaceAll(strings.ReplaceAll(EnvTOTPFormat.Get("%s"), "%25s", "%s"), "&", " \\\n &"), []string{e.allowed} -} - -func (e EnvironmentArray) values() (string, []string) { - return detectedValue, []string{commandArgsExample} -} - -func (e EnvironmentInt) toml() (tomlType, string, bool) { - return tomlInt, "0", false -} - -func (e EnvironmentBool) toml() (tomlType, string, bool) { - return tomlBool, YesValue, false + return metaData{ + value: val, + allowed: []string{YesValue, NoValue}, + tomlType: tomlBool, + tomlValue: YesValue, + canExpand: false, + } } -func (e EnvironmentString) toml() (tomlType, string, bool) { - if e.isArray { - return tomlArray, "[]", e.expand +func (e EnvironmentInt) display() metaData { + return metaData{ + value: fmt.Sprintf("%d", e.value), + allowed: []string{"<integer>"}, + tomlType: tomlInt, + tomlValue: "0", + canExpand: false, } - return tomlString, "\"\"", e.expand } -func (e EnvironmentArray) toml() (tomlType, string, bool) { - return tomlArray, "[]", true +func (e EnvironmentFormatter) display() metaData { + return metaData{ + value: strings.ReplaceAll(strings.ReplaceAll(EnvTOTPFormat.Get("%s"), "%25s", "%s"), "&", " \\\n &"), + allowed: []string{e.allowed}, + tomlType: tomlString, + tomlValue: "\"\"", + canExpand: false, + } } -func (e EnvironmentFormatter) toml() (tomlType, string, bool) { - return tomlString, "\"\"", false +func (e EnvironmentArray) display() metaData { + return metaData{ + value: detectedValue, + allowed: []string{commandArgsExample}, + tomlType: tomlArray, + tomlValue: "[]", + canExpand: true, + } } diff --git a/internal/config/toml.go b/internal/config/toml.go @@ -54,14 +54,14 @@ func DefaultTOML() (string, error) { default: sub = strings.Join(parts[1:], "_") } - _, field, _ := item.toml() + md := item.display() text, err := generateDetailText(item) if err != nil { return "", err } sub = fmt.Sprintf(`%s %s = %s -`, text, sub, field) +`, text, sub, md.tomlValue) had, ok := unmapped[key] if !ok { had = []string{} @@ -102,7 +102,8 @@ func DefaultTOML() (string, error) { func generateDetailText(data printer) (string, error) { env := data.self() - value, allow := data.values() + md := data.display() + value := md.value if len(value) == 0 { value = "(unset)" } @@ -113,16 +114,15 @@ func generateDetailText(data printer) (string, error) { if r != "" { requirement = r } - t, _, expands := data.toml() var text []string for _, line := range []string{ fmt.Sprintf("description:\n%s\n", description), fmt.Sprintf("requirement: %s", requirement), - fmt.Sprintf("option: %s", strings.Join(allow, "|")), + fmt.Sprintf("option: %s", strings.Join(md.allowed, "|")), fmt.Sprintf("%s name: %s", commands.Env, key), fmt.Sprintf("default: %s", value), - fmt.Sprintf("expands: %s", strconv.FormatBool(expands)), - fmt.Sprintf("type: %s", t), + fmt.Sprintf("expands: %s", strconv.FormatBool(md.canExpand)), + fmt.Sprintf("type: %s", md.tomlType), "", "NOTE: the following value is NOT a default, it is an empty TOML placeholder", } { @@ -151,10 +151,10 @@ func LoadConfig(r io.Reader, loader Loader) error { if !ok { return fmt.Errorf("unknown key: %s (%s)", k, export) } - isType, _, expand := env.toml() - switch isType { + md := env.display() + switch md.tomlType { case tomlArray: - array, err := parseStringArray(v, expand) + array, err := parseStringArray(v, md.canExpand) if err != nil { return err } @@ -180,7 +180,7 @@ func LoadConfig(r io.Reader, loader Loader) error { if !ok { return fmt.Errorf("non-string found where expected: %v", v) } - if expand { + if md.canExpand { s = os.Expand(s, os.Getenv) } store.SetString(export, s) diff --git a/internal/config/vars.go b/internal/config/vars.go @@ -131,7 +131,7 @@ var ( // EnvStore is the location of the keepass file/store EnvStore = environmentRegister( EnvironmentString{ - expand: true, + canExpand: true, environmentDefault: newDefaultedEnvironment("", environmentBase{ key: "STORE", @@ -144,7 +144,7 @@ var ( // EnvHookDir is the directory of hooks to execute EnvHookDir = environmentRegister( EnvironmentString{ - expand: true, + canExpand: true, environmentDefault: newDefaultedEnvironment("", environmentBase{ key: hookCategory + "DIRECTORY", @@ -180,7 +180,7 @@ and '%s' allows for multiple windows.`, util.TimeWindowSpan, util.TimeWindowDeli // EnvKeyFile is an keyfile for the database EnvKeyFile = environmentRegister( EnvironmentString{ - expand: true, + canExpand: true, environmentDefault: newDefaultedEnvironment("", environmentBase{ key: credsCategory + "KEY_FILE", @@ -232,7 +232,7 @@ Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode), }) envPassword = environmentRegister( EnvironmentString{ - expand: true, + canExpand: true, environmentDefault: newDefaultedEnvironment("", environmentBase{ requirement: requiredKeyOrKeyFile,