commit 536918d133e3035fa1f14a7eef378c3c1220cba1
parent 92e9c8a7d0e1318611bd6698a0c7970f81e1e916
Author: Sean Enck <sean@ttypty.com>
Date: Sat, 7 Dec 2024 13:38:28 -0500
move platforms to all be under core
Diffstat:
5 files changed, 64 insertions(+), 78 deletions(-)
diff --git a/internal/core/platforms.go b/internal/core/platforms.go
@@ -1,6 +1,13 @@
// Package core defines known platforms
package core
+import (
+ "errors"
+ "os"
+ "os/exec"
+ "strings"
+)
+
// Platforms are the known platforms for lockbox
var Platforms = PlatformTypes{
MacOSPlatform: "macos",
@@ -9,6 +16,8 @@ var Platforms = PlatformTypes{
WindowsLinuxPlatform: "wsl",
}
+const unknownPlatform = ""
+
type (
// SystemPlatform represents the platform lockbox is running on.
SystemPlatform string
@@ -26,3 +35,38 @@ type (
func (p PlatformTypes) List() []string {
return listFields[SystemPlatform](p)
}
+
+// NewPlatform gets a new system platform.
+func NewPlatform(candidate string) (SystemPlatform, error) {
+ env := candidate
+ if env != "" {
+ for _, p := range Platforms.List() {
+ if p == env {
+ return SystemPlatform(p), nil
+ }
+ }
+ return unknownPlatform, errors.New("unknown platform mode")
+ }
+ b, err := exec.Command("uname", "-a").Output()
+ if err != nil {
+ return unknownPlatform, err
+ }
+ raw := strings.ToLower(strings.TrimSpace(string(b)))
+ parts := strings.Split(raw, " ")
+ switch parts[0] {
+ case "darwin":
+ return Platforms.MacOSPlatform, nil
+ case "linux":
+ if strings.Contains(raw, "microsoft-standard-wsl") {
+ return Platforms.WindowsLinuxPlatform, nil
+ }
+ if strings.TrimSpace(os.Getenv("WAYLAND_DISPLAY")) == "" {
+ if strings.TrimSpace(os.Getenv("DISPLAY")) == "" {
+ return unknownPlatform, errors.New("unable to detect linux clipboard mode")
+ }
+ return Platforms.LinuxXPlatform, nil
+ }
+ return Platforms.LinuxWaylandPlatform, nil
+ }
+ return unknownPlatform, errors.New("unable to detect clipboard mode")
+}
diff --git a/internal/core/platforms_test.go b/internal/core/platforms_test.go
@@ -11,3 +11,22 @@ func TestPlatformList(t *testing.T) {
t.Errorf("invalid list result")
}
}
+
+func TestNewPlatform(t *testing.T) {
+ for _, item := range core.Platforms.List() {
+ s, err := core.NewPlatform(item)
+ if err != nil {
+ t.Errorf("invalid clipboard: %v", err)
+ }
+ if s != core.SystemPlatform(item) {
+ t.Error("mismatch on input and resulting detection")
+ }
+ }
+}
+
+func TestNewPlatformUnknown(t *testing.T) {
+ _, err := core.NewPlatform("ifjoajei")
+ if err == nil || err.Error() != "unknown platform mode" {
+ t.Errorf("error expected for platform: %v", err)
+ }
+}
diff --git a/internal/platform/clipboard.go b/internal/platform/clipboard.go
@@ -58,7 +58,7 @@ func NewClipboard() (Clipboard, error) {
c := Clipboard{isOSC52: true}
return c, nil
}
- sys, err := NewPlatform()
+ sys, err := core.NewPlatform(config.EnvPlatform.Get())
if err != nil {
return Clipboard{}, err
}
diff --git a/internal/platform/detect.go b/internal/platform/detect.go
@@ -1,48 +0,0 @@
-package platform
-
-import (
- "errors"
- "os"
- "os/exec"
- "strings"
-
- "github.com/seanenck/lockbox/internal/config"
- "github.com/seanenck/lockbox/internal/core"
-)
-
-const unknownPlatform = ""
-
-// NewPlatform gets a new system platform.
-func NewPlatform() (core.SystemPlatform, error) {
- env := config.EnvPlatform.Get()
- if env != "" {
- for _, p := range core.Platforms.List() {
- if p == env {
- return core.SystemPlatform(p), nil
- }
- }
- return unknownPlatform, errors.New("unknown platform mode")
- }
- b, err := exec.Command("uname", "-a").Output()
- if err != nil {
- return unknownPlatform, err
- }
- raw := strings.ToLower(strings.TrimSpace(string(b)))
- parts := strings.Split(raw, " ")
- switch parts[0] {
- case "darwin":
- return core.Platforms.MacOSPlatform, nil
- case "linux":
- if strings.Contains(raw, "microsoft-standard-wsl") {
- return core.Platforms.WindowsLinuxPlatform, nil
- }
- if strings.TrimSpace(os.Getenv("WAYLAND_DISPLAY")) == "" {
- if strings.TrimSpace(os.Getenv("DISPLAY")) == "" {
- return unknownPlatform, errors.New("unable to detect linux clipboard mode")
- }
- return core.Platforms.LinuxXPlatform, nil
- }
- return core.Platforms.LinuxWaylandPlatform, nil
- }
- return unknownPlatform, errors.New("unable to detect clipboard mode")
-}
diff --git a/internal/platform/detect_test.go b/internal/platform/detect_test.go
@@ -1,29 +0,0 @@
-package platform_test
-
-import (
- "testing"
-
- "github.com/seanenck/lockbox/internal/core"
- "github.com/seanenck/lockbox/internal/platform"
-)
-
-func TestNewPlatform(t *testing.T) {
- for _, item := range core.Platforms.List() {
- t.Setenv("LOCKBOX_PLATFORM", item)
- s, err := platform.NewPlatform()
- if err != nil {
- t.Errorf("invalid clipboard: %v", err)
- }
- if s != core.SystemPlatform(item) {
- t.Error("mismatch on input and resulting detection")
- }
- }
-}
-
-func TestNewPlatformUnknown(t *testing.T) {
- t.Setenv("LOCKBOX_PLATFORM", "afleaj")
- _, err := platform.NewPlatform()
- if err == nil || err.Error() != "unknown platform mode" {
- t.Errorf("error expected for platform: %v", err)
- }
-}