lockbox

password manager
Log | Files | Refs | README | LICENSE

commit c8fb0e37f68a23eac591bd4f3a59fbb452c7a1f6
parent e2b1fd146d79ed86b6a13bde847270816e7cbdf5
Author: Sean Enck <sean@ttypty.com>
Date:   Fri, 31 Mar 2023 20:04:50 -0400

singular type for this operation

Diffstat:
Minternal/app/rekey.go | 2+-
Minternal/backend/query.go | 6+++---
Minternal/inputs/env.go | 8+-------
Minternal/inputs/json.go | 32++++++++++++++++----------------
Minternal/inputs/json_test.go | 8++++----
5 files changed, 25 insertions(+), 31 deletions(-)

diff --git a/internal/app/rekey.go b/internal/app/rekey.go @@ -81,7 +81,7 @@ func ReKey(cmd CommandOptions, r Keyer) error { return nil } - os.Setenv(inputs.JSONDataOutputEnv, inputs.JSONDataOutputRaw) + os.Setenv(inputs.JSONDataOutputEnv, string(inputs.JSONDataOutputRaw)) entries, err := r.JSON() if err != nil { return err diff --git a/internal/backend/query.go b/internal/backend/query.go @@ -118,7 +118,7 @@ func (t *Transaction) QueryCallback(args QueryOptions) ([]QueryEntity, error) { if isSort { sort.Strings(keys) } - jsonMode := inputs.JSONBlankMode + jsonMode := inputs.JSONDataOutputBlank if args.Values == JSONValue { m, err := inputs.ParseJSONOutput() if err != nil { @@ -142,9 +142,9 @@ func (t *Transaction) QueryCallback(args QueryOptions) ([]QueryEntity, error) { case JSONValue: data := "" switch jsonMode { - case inputs.JSONRawMode: + case inputs.JSONDataOutputRaw: data = val - case inputs.JSONHashMode: + case inputs.JSONDataOutputHash: data = fmt.Sprintf("%x", sha512.Sum512([]byte(val))) } t := getValue(e.backing, modTimeKey) diff --git a/internal/inputs/env.go b/internal/inputs/env.go @@ -68,12 +68,6 @@ const ( MaxTOTPTimeDefault = "120" // JSONDataOutputEnv controls how JSON is output JSONDataOutputEnv = prefixKey + "JSON_DATA_OUTPUT" - // JSONDataOutputHash means output data is hashed - JSONDataOutputHash = "hash" - // JSONDataOutputBlank means an empty entry is set - JSONDataOutputBlank = "empty" - // JSONDataOutputRaw means the RAW (unencrypted) value is displayed - JSONDataOutputRaw = "plaintext" ) var isYesNoArgs = []string{env.Yes, env.No} @@ -285,6 +279,6 @@ func ListEnvironmentVariables(showValues bool) []string { results = append(results, e.formatEnvironmentVariable(false, clipOSC52Env, env.No, "enable OSC52 clipboard mode", isYesNoArgs)) results = append(results, e.formatEnvironmentVariable(false, KeyFileEnv, "", "additional keyfile to access/protect the database", []string{"keyfile"})) results = append(results, e.formatEnvironmentVariable(false, ModTimeEnv, ModTimeFormat, fmt.Sprintf("input modification time to set for the entry\n(expected format: %s)", ModTimeFormat), []string{"modtime"})) - results = append(results, e.formatEnvironmentVariable(false, JSONDataOutputEnv, JSONDataOutputHash, fmt.Sprintf("changes what the data field in JSON outputs will contain\nuse '%s' with CAUTION", JSONDataOutputRaw), []string{JSONDataOutputRaw, JSONDataOutputHash, JSONDataOutputBlank})) + results = append(results, e.formatEnvironmentVariable(false, JSONDataOutputEnv, string(JSONDataOutputHash), fmt.Sprintf("changes what the data field in JSON outputs will contain\nuse '%s' with CAUTION", JSONDataOutputRaw), []string{string(JSONDataOutputRaw), string(JSONDataOutputHash), string(JSONDataOutputBlank)})) return results } diff --git a/internal/inputs/json.go b/internal/inputs/json.go @@ -8,30 +8,30 @@ import ( "github.com/enckse/pgl/os/env" ) -type ( - // JSONOutputMode is the output mode definition - JSONOutputMode uint +const ( + // JSONDataOutputHash means output data is hashed + JSONDataOutputHash JSONOutputMode = "hash" + // JSONDataOutputBlank means an empty entry is set + JSONDataOutputBlank JSONOutputMode = "empty" + // JSONDataOutputRaw means the RAW (unencrypted) value is displayed + JSONDataOutputRaw JSONOutputMode = "plaintext" ) -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 +type ( + // JSONOutputMode is the output mode definition + JSONOutputMode string ) // ParseJSONOutput handles detecting the JSON output mode func ParseJSONOutput() (JSONOutputMode, error) { - val := strings.ToLower(strings.TrimSpace(env.GetOrDefault(JSONDataOutputEnv, JSONDataOutputHash))) - switch val { + val := strings.ToLower(strings.TrimSpace(env.GetOrDefault(JSONDataOutputEnv, string(JSONDataOutputHash)))) + switch JSONOutputMode(val) { case JSONDataOutputHash: - return JSONHashMode, nil + return JSONDataOutputHash, nil case JSONDataOutputBlank: - return JSONBlankMode, nil + return JSONDataOutputBlank, nil case JSONDataOutputRaw: - return JSONRawMode, nil + return JSONDataOutputRaw, nil } - return JSONBlankMode, fmt.Errorf("invalid JSON output mode: %s", val) + return JSONDataOutputBlank, fmt.Errorf("invalid JSON output mode: %s", val) } diff --git a/internal/inputs/json_test.go b/internal/inputs/json_test.go @@ -10,22 +10,22 @@ import ( func TestParseJSONMode(t *testing.T) { defer os.Clearenv() m, err := inputs.ParseJSONOutput() - if m != inputs.JSONHashMode || err != nil { + if m != inputs.JSONDataOutputHash || err != nil { t.Error("invalid mode read") } os.Setenv("LOCKBOX_JSON_DATA_OUTPUT", "hAsH ") m, err = inputs.ParseJSONOutput() - if m != inputs.JSONHashMode || err != nil { + if m != inputs.JSONDataOutputHash || err != nil { t.Error("invalid mode read") } os.Setenv("LOCKBOX_JSON_DATA_OUTPUT", "EMPTY") m, err = inputs.ParseJSONOutput() - if m != inputs.JSONBlankMode || err != nil { + if m != inputs.JSONDataOutputBlank || err != nil { t.Error("invalid mode read") } os.Setenv("LOCKBOX_JSON_DATA_OUTPUT", " PLAINtext ") m, err = inputs.ParseJSONOutput() - if m != inputs.JSONRawMode || err != nil { + if m != inputs.JSONDataOutputRaw || err != nil { t.Error("invalid mode read") } os.Setenv("LOCKBOX_JSON_DATA_OUTPUT", "a")