lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 9223cd9a7cfbf6077f5b548334b14d852325ccff
parent 000ff4d91c8c6f1f736284d412d1d3b04ef86d15
Author: Sean Enck <sean@ttypty.com>
Date:   Fri,  6 Dec 2024 18:58:51 -0500

cleaning up implementation

Diffstat:
Minternal/app/core.go | 12++++++++++--
Minternal/app/core_test.go | 2+-
Minternal/config/core.go | 3+--
Minternal/config/toml.go | 14++++++++++----
Minternal/config/toml_test.go | 4++--
5 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/internal/app/core.go b/internal/app/core.go @@ -114,6 +114,7 @@ type ( ReKeyCommand string CompletionsCommand string CompletionsEnv string + ExampleTOML string ReKey struct { KeyFile string NoKey string @@ -251,6 +252,7 @@ func Usage(verbose bool, exe string) ([]string, error) { ReKeyCommand: ReKeyCommand, CompletionsCommand: CompletionsCommand, CompletionsEnv: config.EnvDefaultCompletionKey, + ExampleTOML: config.ExampleTOML, } document.ReKey.KeyFile = setDocFlag(config.ReKeyFlags.KeyFile) document.ReKey.NoKey = config.ReKeyFlags.NoKey @@ -275,10 +277,16 @@ func Usage(verbose bool, exe string) ([]string, error) { if err != nil { return nil, err } - if header == "[environment]" { + switch header { + case "[environment]": env = s - } else { + default: buf.WriteString(s) + if header == "[toml]" { + buf.WriteString("========================================\nconfig.toml\n---\n") + buf.WriteString(config.ExampleTOML) + buf.WriteString("========================================\n\n") + } } } if env == "" { diff --git a/internal/app/core_test.go b/internal/app/core_test.go @@ -13,7 +13,7 @@ func TestUsage(t *testing.T) { t.Errorf("invalid usage, out of date? %d", len(u)) } u, _ = app.Usage(true, "lb") - if len(u) != 124 { + if len(u) != 181 { t.Errorf("invalid verbose usage, out of date? %d", len(u)) } for _, usage := range u { diff --git a/internal/config/core.go b/internal/config/core.go @@ -318,8 +318,7 @@ func ParseColorWindow(windowString string) ([]ColorWindow, error) { return rules, nil } -// NewEnvFiles will get the list of candidate environment files -// it will also set the environment to empty for the caller +// NewConfigFiles will get the list of candidate config files func NewConfigFiles() []string { v := EnvConfig.Get() if v == "" || v == noEnvironment { diff --git a/internal/config/toml.go b/internal/config/toml.go @@ -14,14 +14,17 @@ import ( ) type ( - ConfigLoader func(string) (io.Reader, error) - ShellEnv struct { + // Loader indicates how included files should be sourced + Loader func(string) (io.Reader, error) + // ShellEnv is the output shell environment settings parsed from TOML config + ShellEnv struct { Key string Value string } ) var ( + // ExampleTOML is an example TOML file of viable fields //go:embed "config.toml" ExampleTOML string redirects = map[string]string{ @@ -64,7 +67,8 @@ var ( } ) -func LoadConfig(r io.Reader, loader ConfigLoader) ([]ShellEnv, error) { +// LoadConfig will read the input reader and use the loader to source configuration files +func LoadConfig(r io.Reader, loader Loader) ([]ShellEnv, error) { m := make(map[string]interface{}) if err := overlayConfig(r, true, &m, loader); err != nil { return nil, err @@ -124,7 +128,7 @@ func LoadConfig(r io.Reader, loader ConfigLoader) ([]ShellEnv, error) { return res, nil } -func overlayConfig(r io.Reader, canInclude bool, m *map[string]interface{}, loader ConfigLoader) error { +func overlayConfig(r io.Reader, canInclude bool, m *map[string]interface{}, loader Loader) error { d := toml.NewDecoder(r) if _, err := d.Decode(m); err != nil { return err @@ -204,6 +208,8 @@ func configLoader(path string) (io.Reader, error) { return bytes.NewReader(data), nil } +// LoadConfigFile will load a path as the configuration +// it will also set the environment func LoadConfigFile(path string) error { reader, err := configLoader(path) if err != nil { diff --git a/internal/config/toml_test.go b/internal/config/toml_test.go @@ -178,7 +178,7 @@ max = 1 max = -1 ` r = strings.NewReader(data) - env, err = config.LoadConfig(r, func(p string) (io.Reader, error) { + _, err = config.LoadConfig(r, func(p string) (io.Reader, error) { return nil, nil }) if err == nil || err.Error() != "-1 is negative (not allowed here)" { @@ -259,7 +259,7 @@ format = -1 func TestLoadFile(t *testing.T) { os.Mkdir("testdata", 0o755) - // defer os.RemoveAll("testdata") + defer os.RemoveAll("testdata") defer os.Clearenv() file := filepath.Join("testdata", "config.toml") os.WriteFile(file, []byte(config.ExampleTOML), 0o644)