lockbox

password manager
Log | Files | Refs | README | LICENSE

commit e2b1fd146d79ed86b6a13bde847270816e7cbdf5
parent 4f54140c40be9cc1af820d420fcde838dedd5111
Author: Sean Enck <sean@ttypty.com>
Date:   Fri, 31 Mar 2023 20:01:02 -0400

new parsers

Diffstat:
Ainternal/inputs/json.go | 37+++++++++++++++++++++++++++++++++++++
Ainternal/inputs/json_test.go | 35+++++++++++++++++++++++++++++++++++
2 files changed, 72 insertions(+), 0 deletions(-)

diff --git a/internal/inputs/json.go b/internal/inputs/json.go @@ -0,0 +1,37 @@ +// Package inputs handles user inputs/UI elements for JSON +package inputs + +import ( + "fmt" + "strings" + + "github.com/enckse/pgl/os/env" +) + +type ( + // JSONOutputMode is the output mode definition + JSONOutputMode uint +) + +const ( + // JSONBlankMode will results in an empty field + JSONBlankMode JSONOutputMode = iota + // JSONHashMode will use a common hasher to hash the raw value + JSONHashMode + // JSONRawMode will display the raw value + JSONRawMode +) + +// ParseJSONOutput handles detecting the JSON output mode +func ParseJSONOutput() (JSONOutputMode, error) { + val := strings.ToLower(strings.TrimSpace(env.GetOrDefault(JSONDataOutputEnv, JSONDataOutputHash))) + switch val { + case JSONDataOutputHash: + return JSONHashMode, nil + case JSONDataOutputBlank: + return JSONBlankMode, nil + case JSONDataOutputRaw: + return JSONRawMode, nil + } + return JSONBlankMode, fmt.Errorf("invalid JSON output mode: %s", val) +} diff --git a/internal/inputs/json_test.go b/internal/inputs/json_test.go @@ -0,0 +1,35 @@ +package inputs_test + +import ( + "os" + "testing" + + "github.com/enckse/lockbox/internal/inputs" +) + +func TestParseJSONMode(t *testing.T) { + defer os.Clearenv() + m, err := inputs.ParseJSONOutput() + if m != inputs.JSONHashMode || err != nil { + t.Error("invalid mode read") + } + os.Setenv("LOCKBOX_JSON_DATA_OUTPUT", "hAsH ") + m, err = inputs.ParseJSONOutput() + if m != inputs.JSONHashMode || err != nil { + t.Error("invalid mode read") + } + os.Setenv("LOCKBOX_JSON_DATA_OUTPUT", "EMPTY") + m, err = inputs.ParseJSONOutput() + if m != inputs.JSONBlankMode || err != nil { + t.Error("invalid mode read") + } + os.Setenv("LOCKBOX_JSON_DATA_OUTPUT", " PLAINtext ") + m, err = inputs.ParseJSONOutput() + if m != inputs.JSONRawMode || err != nil { + t.Error("invalid mode read") + } + os.Setenv("LOCKBOX_JSON_DATA_OUTPUT", "a") + if _, err = inputs.ParseJSONOutput(); err == nil || err.Error() != "invalid JSON output mode: a" { + t.Errorf("invalid error: %v", err) + } +}