lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 08b41f0e7fa897548fc90890f91af4eac9bec71b
parent 8e40f50a580e0ab8e93252967cdf6d8292df5986
Author: Sean Enck <sean@ttypty.com>
Date:   Sun, 11 Aug 2024 11:37:47 -0400

rework platforms to be more dynamically defined

Diffstat:
Minternal/config/core.go | 50+++++++++++++++++++++++++++++++++++---------------
Minternal/config/core_test.go | 4++--
Minternal/config/vars.go | 8++++----
Minternal/platform/clipboard.go | 8++++----
Minternal/platform/clipboard_test.go | 6+++---
5 files changed, 48 insertions(+), 28 deletions(-)

diff --git a/internal/config/core.go b/internal/config/core.go @@ -8,6 +8,7 @@ import ( "os" "os/exec" "path/filepath" + "reflect" "sort" "strconv" "strings" @@ -24,14 +25,6 @@ const ( detectEnvironment = "detect" noEnvironment = "none" envFile = "lockbox.env" - // MacOSPlatform is the macos indicator for platform - MacOSPlatform = "macos" - // LinuxWaylandPlatform for linux+wayland - LinuxWaylandPlatform = "linux-wayland" - // LinuxXPlatform for linux+X - LinuxXPlatform = "linux-x" - // WindowsLinuxPlatform for WSL subsystems - WindowsLinuxPlatform = "wsl" unknownPlatform = "" // ReKeyKeyFileFlag is the flag used for rekey to set the keyfile ReKeyKeyFileFlag = "keyfile" @@ -47,7 +40,7 @@ const ( var ( detectEnvironmentPaths = []string{filepath.Join(".config", envFile), filepath.Join(".config", "lockbox", envFile)} exampleColorWindows = []string{strings.Join([]string{exampleColorWindow, exampleColorWindow, exampleColorWindow + "..."}, colorWindowDelimiter)} - registry = []printer{} + registeredEnv = []printer{} ) type ( @@ -116,6 +109,13 @@ type ( NoKey bool KeyFile string } + // PlatformTypes defines systems lockbox is known to run on or can run on + PlatformTypes struct { + MacOSPlatform SystemPlatform + LinuxWaylandPlatform SystemPlatform + LinuxXPlatform SystemPlatform + WindowsLinuxPlatform SystemPlatform + } ) func shlex(in string) ([]string, error) { @@ -267,18 +267,18 @@ func NewPlatform() (SystemPlatform, error) { parts := strings.Split(raw, " ") switch parts[0] { case "darwin": - return MacOSPlatform, nil + return Platforms.MacOSPlatform, nil case "linux": if strings.Contains(raw, "microsoft-standard-wsl") { - return WindowsLinuxPlatform, nil + return Platforms.WindowsLinuxPlatform, nil } if strings.TrimSpace(getExpand("WAYLAND_DISPLAY")) == "" { if strings.TrimSpace(getExpand("DISPLAY")) == "" { return unknownPlatform, errors.New("unable to detect linux clipboard mode") } - return LinuxXPlatform, nil + return Platforms.LinuxXPlatform, nil } - return LinuxWaylandPlatform, nil + return Platforms.LinuxWaylandPlatform, nil } return unknownPlatform, errors.New("unable to detect clipboard mode") } @@ -358,7 +358,7 @@ func IsUnset(k, v string) (bool, error) { func Environ() []string { var results []string for _, k := range os.Environ() { - for _, r := range registry { + for _, r := range registeredEnv { key := r.self().key() if key == EnvConfig.key() { continue @@ -490,6 +490,26 @@ func wrap(in string, maxLength int) string { } func environmentRegister[T printer](obj T) T { - registry = append(registry, obj) + registeredEnv = append(registeredEnv, obj) + return obj +} + +// List will get the displayable list of platforms +func (p PlatformTypes) List() []string { + v := reflect.ValueOf(p) + var vals []string + for i := 0; i < v.NumField(); i++ { + vals = append(vals, string(v.Field(i).Interface().(SystemPlatform))) + } + sort.Strings(vals) + return vals +} + +func newPlatformsTypes() PlatformTypes { + obj := PlatformTypes{} + obj.MacOSPlatform = "macos" + obj.LinuxWaylandPlatform = "linux-wayland" + obj.LinuxXPlatform = "linux-x" + obj.WindowsLinuxPlatform = "wsl" return obj } diff --git a/internal/config/core_test.go b/internal/config/core_test.go @@ -10,7 +10,7 @@ import ( ) func TestPlatformSet(t *testing.T) { - if len(config.Platforms) != 4 { + if len(config.Platforms.List()) != 4 { t.Error("invalid platform set") } } @@ -48,7 +48,7 @@ func TestKeyValue(t *testing.T) { } func TestNewPlatform(t *testing.T) { - for _, item := range config.Platforms { + for _, item := range config.Platforms.List() { os.Setenv("LOCKBOX_PLATFORM", item) s, err := config.NewPlatform() if err != nil { diff --git a/internal/config/vars.go b/internal/config/vars.go @@ -32,8 +32,8 @@ const ( ) var ( - // Platforms represent the platforms that lockbox understands to run on - Platforms = []string{MacOSPlatform, WindowsLinuxPlatform, LinuxXPlatform, LinuxWaylandPlatform} + // Platforms are the known platforms for lockbox + Platforms = newPlatformsTypes() // TOTPDefaultColorWindow is the default coloring rules for totp TOTPDefaultColorWindow = []ColorWindow{{Start: 0, End: 5}, {Start: 30, End: 35}} // TOTPDefaultBetween is the default color window as a string @@ -122,7 +122,7 @@ var ( EnvPlatform = environmentRegister(EnvironmentString{environmentBase: environmentBase{ subKey: "PLATFORM", desc: "Override the detected platform.", - }, defaultValue: detectedValue, allowed: Platforms, canDefault: false}) + }, defaultValue: detectedValue, allowed: Platforms.List(), canDefault: false}) // EnvStore is the location of the keepass file/store EnvStore = environmentRegister(EnvironmentString{environmentBase: environmentBase{ subKey: "STORE", @@ -230,7 +230,7 @@ func GetReKey(args []string) (ReKeyArgs, error) { // ListEnvironmentVariables will print information about env variables func ListEnvironmentVariables() []string { var results []string - for _, item := range registry { + for _, item := range registeredEnv { env := item.self() value, allow := item.values() if len(value) == 0 { diff --git a/internal/platform/clipboard.go b/internal/platform/clipboard.go @@ -65,16 +65,16 @@ func NewClipboard() (Clipboard, error) { var copying []string var pasting []string switch sys { - case config.MacOSPlatform: + case config.Platforms.MacOSPlatform: copying = []string{"pbcopy"} pasting = []string{"pbpaste"} - case config.LinuxXPlatform: + case config.Platforms.LinuxXPlatform: copying = []string{"xclip"} pasting = []string{"xclip", "-o"} - case config.LinuxWaylandPlatform: + case config.Platforms.LinuxWaylandPlatform: copying = []string{"wl-copy"} pasting = []string{"wl-paste"} - case config.WindowsLinuxPlatform: + case config.Platforms.WindowsLinuxPlatform: copying = []string{"clip.exe"} pasting = []string{"powershell.exe", "-command", "Get-Clipboard"} default: diff --git a/internal/platform/clipboard_test.go b/internal/platform/clipboard_test.go @@ -22,7 +22,7 @@ func TestNoClipboard(t *testing.T) { func TestMaxTime(t *testing.T) { os.Setenv("LOCKBOX_NOCLIP", "no") os.Setenv("LOCKBOX_CLIP_OSC52", "no") - os.Setenv("LOCKBOX_PLATFORM", string(config.LinuxWaylandPlatform)) + os.Setenv("LOCKBOX_PLATFORM", string(config.Platforms.LinuxWaylandPlatform)) os.Setenv("LOCKBOX_CLIP_MAX", "") c, err := platform.NewClipboard() if err != nil { @@ -55,7 +55,7 @@ func TestClipboardInstances(t *testing.T) { os.Setenv("LOCKBOX_NOCLIP", "no") os.Setenv("LOCKBOX_CLIP_MAX", "") os.Setenv("LOCKBOX_CLIP_OSC52", "no") - for _, item := range config.Platforms { + for _, item := range config.Platforms.List() { os.Setenv("LOCKBOX_PLATFORM", item) _, err := platform.NewClipboard() if err != nil { @@ -80,7 +80,7 @@ func TestOSC52(t *testing.T) { func TestArgsOverride(t *testing.T) { os.Setenv("LOCKBOX_CLIP_PASTE", "abc xyz 111") os.Setenv("LOCKBOX_CLIP_OSC52", "no") - os.Setenv("LOCKBOX_PLATFORM", string(config.WindowsLinuxPlatform)) + os.Setenv("LOCKBOX_PLATFORM", string(config.Platforms.WindowsLinuxPlatform)) c, _ := platform.NewClipboard() cmd, args, ok := c.Args(true) if cmd != "clip.exe" || len(args) != 0 || !ok {