lockbox

password manager
Log | Files | Refs | README | LICENSE

commit c3ac881cb98bfaf603b5c890383f4be2620c0005
parent 18d131176b593d1f53af52c556836d2c94e24df0
Author: Sean Enck <sean@ttypty.com>
Date:   Tue, 25 Jul 2023 19:29:37 -0400

minor restructure

Diffstat:
Minternal/backend/query_test.go | 1+
Dinternal/colors/core.go | 12------------
Dinternal/colors/terminal.go | 44--------------------------------------------
Dinternal/colors/terminal_test.go | 66------------------------------------------------------------------
Ainternal/totp/colors.go | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ainternal/totp/colors_test.go | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Minternal/totp/core.go | 3+--
Mtests/run.sh | 1+
8 files changed, 123 insertions(+), 124 deletions(-)

diff --git a/internal/backend/query_test.go b/internal/backend/query_test.go @@ -74,6 +74,7 @@ func TestGet(t *testing.T) { } func TestValueModes(t *testing.T) { + os.Clearenv() setupInserts(t) q, err := fullSetup(t, true).Get("test/test/abc", backend.BlankValue) if err != nil { diff --git a/internal/colors/core.go b/internal/colors/core.go @@ -1,12 +0,0 @@ -// Package colors manages definitions within lockbox for colorization. -package colors - -const ( - // Red will get red terminal coloring. - Red = iota -) - -type ( - // Color are terminal colors for dumb terminal coloring. - Color int -) diff --git a/internal/colors/terminal.go b/internal/colors/terminal.go @@ -1,44 +0,0 @@ -// Package colors handles terminal handling for lockbox. -package colors - -import ( - "errors" - - "github.com/enckse/lockbox/internal/inputs" -) - -const ( - termBeginRed = "\033[1;31m" - termEndRed = "\033[0m" -) - -type ( - // Terminal represents terminal coloring information. - Terminal struct { - Start string - End string - } -) - -// NewTerminal will retrieve start/end terminal coloration indicators. -func NewTerminal(color Color) (Terminal, error) { - if color != Red { - return Terminal{}, errors.New("bad color") - } - interactive, err := inputs.IsInteractive() - if err != nil { - return Terminal{}, err - } - colors := interactive - if colors { - isColored, err := inputs.IsNoColorEnabled() - if err != nil { - return Terminal{}, err - } - colors = !isColored - } - if colors { - return Terminal{Start: termBeginRed, End: termEndRed}, nil - } - return Terminal{}, nil -} diff --git a/internal/colors/terminal_test.go b/internal/colors/terminal_test.go @@ -1,66 +0,0 @@ -package colors_test - -import ( - "os" - "testing" - - "github.com/enckse/lockbox/internal/colors" -) - -func TestHasColoring(t *testing.T) { - os.Setenv("LOCKBOX_INTERACTIVE", "yes") - os.Setenv("LOCKBOX_NOCOLOR", "no") - term, err := colors.NewTerminal(colors.Red) - if err != nil { - t.Errorf("color was valid: %v", err) - } - if term.Start != "\033[1;31m" || term.End != "\033[0m" { - t.Error("bad resulting color") - } -} - -func TestBadColor(t *testing.T) { - _, err := colors.NewTerminal(colors.Color(5)) - if err == nil || err.Error() != "bad color" { - t.Errorf("invalid color error: %v", err) - } -} - -func TestNoColoring(t *testing.T) { - os.Setenv("LOCKBOX_INTERACTIVE", "no") - os.Setenv("LOCKBOX_NOCOLOR", "yes") - term, err := colors.NewTerminal(colors.Red) - if err != nil { - t.Errorf("color was valid: %v", err) - } - if term.Start != "" || term.End != "" { - t.Error("should have no color") - } - os.Setenv("LOCKBOX_INTERACTIVE", "yes") - os.Setenv("LOCKBOX_NOCOLOR", "yes") - term, err = colors.NewTerminal(colors.Red) - if err != nil { - t.Errorf("color was valid: %v", err) - } - if term.Start != "" || term.End != "" { - t.Error("should have no color") - } - os.Setenv("LOCKBOX_INTERACTIVE", "no") - os.Setenv("LOCKBOX_NOCOLOR", "no") - term, err = colors.NewTerminal(colors.Red) - if err != nil { - t.Errorf("color was valid: %v", err) - } - if term.Start != "" || term.End != "" { - t.Error("should have no color") - } - os.Setenv("LOCKBOX_INTERACTIVE", "yes") - os.Setenv("LOCKBOX_NOCOLOR", "no") - term, err = colors.NewTerminal(colors.Red) - if err != nil { - t.Errorf("color was valid: %v", err) - } - if term.Start == "" || term.End == "" { - t.Error("should have color") - } -} diff --git a/internal/totp/colors.go b/internal/totp/colors.go @@ -0,0 +1,54 @@ +// Package totp manages definitions within lockbox for colorization. +package totp + +import ( + "errors" + + "github.com/enckse/lockbox/internal/inputs" +) + +const ( + // Red will get red terminal coloring. + Red = iota +) + +type ( + // Color are terminal colors for dumb terminal coloring. + Color int +) + +const ( + termBeginRed = "\033[1;31m" + termEndRed = "\033[0m" +) + +type ( + // Terminal represents terminal coloring information. + Terminal struct { + Start string + End string + } +) + +// NewTerminal will retrieve start/end terminal coloration indicators. +func NewTerminal(color Color) (Terminal, error) { + if color != Red { + return Terminal{}, errors.New("bad color") + } + interactive, err := inputs.IsInteractive() + if err != nil { + return Terminal{}, err + } + colors := interactive + if colors { + isColored, err := inputs.IsNoColorEnabled() + if err != nil { + return Terminal{}, err + } + colors = !isColored + } + if colors { + return Terminal{Start: termBeginRed, End: termEndRed}, nil + } + return Terminal{}, nil +} diff --git a/internal/totp/colors_test.go b/internal/totp/colors_test.go @@ -0,0 +1,66 @@ +package totp_test + +import ( + "os" + "testing" + + "github.com/enckse/lockbox/internal/totp" +) + +func TestHasColoring(t *testing.T) { + os.Setenv("LOCKBOX_INTERACTIVE", "yes") + os.Setenv("LOCKBOX_NOCOLOR", "no") + term, err := totp.NewTerminal(totp.Red) + if err != nil { + t.Errorf("color was valid: %v", err) + } + if term.Start != "\033[1;31m" || term.End != "\033[0m" { + t.Error("bad resulting color") + } +} + +func TestBadColor(t *testing.T) { + _, err := totp.NewTerminal(totp.Color(5)) + if err == nil || err.Error() != "bad color" { + t.Errorf("invalid color error: %v", err) + } +} + +func TestNoColoring(t *testing.T) { + os.Setenv("LOCKBOX_INTERACTIVE", "no") + os.Setenv("LOCKBOX_NOCOLOR", "yes") + term, err := totp.NewTerminal(totp.Red) + if err != nil { + t.Errorf("color was valid: %v", err) + } + if term.Start != "" || term.End != "" { + t.Error("should have no color") + } + os.Setenv("LOCKBOX_INTERACTIVE", "yes") + os.Setenv("LOCKBOX_NOCOLOR", "yes") + term, err = totp.NewTerminal(totp.Red) + if err != nil { + t.Errorf("color was valid: %v", err) + } + if term.Start != "" || term.End != "" { + t.Error("should have no color") + } + os.Setenv("LOCKBOX_INTERACTIVE", "no") + os.Setenv("LOCKBOX_NOCOLOR", "no") + term, err = totp.NewTerminal(totp.Red) + if err != nil { + t.Errorf("color was valid: %v", err) + } + if term.Start != "" || term.End != "" { + t.Error("should have no color") + } + os.Setenv("LOCKBOX_INTERACTIVE", "yes") + os.Setenv("LOCKBOX_NOCOLOR", "no") + term, err = totp.NewTerminal(totp.Red) + if err != nil { + t.Errorf("color was valid: %v", err) + } + if term.Start == "" || term.End == "" { + t.Error("should have color") + } +} diff --git a/internal/totp/core.go b/internal/totp/core.go @@ -13,7 +13,6 @@ import ( "github.com/enckse/lockbox/internal/app" "github.com/enckse/lockbox/internal/backend" "github.com/enckse/lockbox/internal/cli" - "github.com/enckse/lockbox/internal/colors" "github.com/enckse/lockbox/internal/inputs" "github.com/enckse/lockbox/internal/platform" "github.com/enckse/lockbox/internal/system" @@ -110,7 +109,7 @@ func (args *Arguments) display(opts Options) error { if !interactive && clip { return errors.New("clipboard not available in non-interactive mode") } - coloring, err := colors.NewTerminal(colors.Red) + coloring, err := NewTerminal(Red) if err != nil { return err } diff --git a/tests/run.sh b/tests/run.sh @@ -14,6 +14,7 @@ _execute() { export LOCKBOX_INTERACTIVE=no export LOCKBOX_READONLY=no export LOCKBOX_KEYMODE=plaintext + export LOCKBOX_JSON_DATA_OUTPUT_HASH_LENGTH=0 echo test2 |${LB_BINARY} insert keys/k/one2 echo test |${LB_BINARY} insert keys/k/one echo test |${LB_BINARY} insert key/a/one