From 381596a40ee8b691b8199e0953069b8460261114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20M=C3=B6ller?= Date: Mon, 22 Jul 2024 11:07:23 +0200 Subject: [PATCH] prepare more commands and tests --- lvmdevices_test.go | 6 ++++++ path.go | 22 ++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lvmdevices_test.go b/lvmdevices_test.go index cd87c92..9ded2e7 100644 --- a/lvmdevices_test.go +++ b/lvmdevices_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "os/exec" "strings" "testing" ) @@ -11,6 +12,11 @@ import ( func TestLVMDevices(t *testing.T) { FailTestIfNotRoot(t) + _, err := exec.LookPath("lvmdevices") + if err != nil { + t.Skip("Skipping test because lvmdevices command is not found") + } + clnt := NewClient() ctx := context.Background() diff --git a/path.go b/path.go index c2e3be6..91a39c2 100644 --- a/path.go +++ b/path.go @@ -1,12 +1,13 @@ package lvm2go import ( + "os/exec" "sync" ) var ( - lvmBinaryPathLock = &sync.RWMutex{} - lvmBinaryPath = "/sbin/lvm" + lvmBinaryPathLock = &sync.Mutex{} + lvmBinaryPath = "" ) // SetLVMPath sets the Path to the lvmBinaryPath command. @@ -20,7 +21,20 @@ func SetLVMPath(path string) { // GetLVMPath returns the Path to the lvmBinaryPath command. func GetLVMPath() string { - lvmBinaryPathLock.RLock() - defer lvmBinaryPathLock.RUnlock() + lvmBinaryPathLock.Lock() + defer lvmBinaryPathLock.Unlock() + + if lvmBinaryPath == "" { + lvmBinaryPath = resolveLVMPathFromHost() + } + return lvmBinaryPath } + +var resolveLVMPathFromHost = sync.OnceValue(func() string { + if path, err := exec.LookPath("lvm"); err != nil { + return "/sbin/lvm" + } else { + return path + } +})