Skip to content

Commit

Permalink
prepare more commands and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobmoellerdev committed Jul 22, 2024
1 parent 1247851 commit 381596a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lvmdevices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"testing"
)

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()

Expand Down
22 changes: 18 additions & 4 deletions path.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
}
})

0 comments on commit 381596a

Please sign in to comment.