fix: use exec.LookPath instead of 'command -v' for binary detection
'command' is a shell builtin, not a real binary, so exec.Command always failed with exit 127. Replaced with os/exec.LookPath which correctly resolves binaries in PATH. Also adds capabilities_test.go to verify Probe() matches exec.LookPath results. Version: 0.7.2
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
BINARY=nasctl
|
BINARY=nasctl
|
||||||
VERSION?=0.7.1
|
VERSION?=0.7.2
|
||||||
GO?=go
|
GO?=go
|
||||||
LDFLAGS=-s -w -X github.com/darroyo/nasctl/internal/web.Version=$(VERSION) -X github.com/darroyo/nasctl/internal/web.Commit=$(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
|
LDFLAGS=-s -w -X github.com/darroyo/nasctl/internal/web.Version=$(VERSION) -X github.com/darroyo/nasctl/internal/web.Commit=$(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
|
||||||
BUILD_FLAGS=CGO_ENABLED=0
|
BUILD_FLAGS=CGO_ENABLED=0
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package storage
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
"github.com/darroyo/nasctl/internal/db"
|
"github.com/darroyo/nasctl/internal/db"
|
||||||
"github.com/darroyo/nasctl/internal/system"
|
"github.com/darroyo/nasctl/internal/system"
|
||||||
@@ -15,17 +16,17 @@ type Capabilities struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Probe(ctx context.Context) (Capabilities, error) {
|
func Probe(ctx context.Context) (Capabilities, error) {
|
||||||
rsyncOut, _, _ := system.Run(ctx, "command", "-v", "rsync")
|
_, rsyncErr := exec.LookPath("rsync")
|
||||||
snapraidOut, _, _ := system.Run(ctx, "command", "-v", "snapraid")
|
_, snapraidErr := exec.LookPath("snapraid")
|
||||||
mergerfsOut, _, _ := system.Run(ctx, "command", "-v", "mergerfs")
|
_, mergerfsErr := exec.LookPath("mergerfs")
|
||||||
|
|
||||||
_, _, mergerfsMountedErr := system.Run(ctx, "grep", "-q", "fuse.mergerfs", "/proc/mounts")
|
_, _, mergerfsMountedErr := system.Run(ctx, "grep", "-q", "fuse.mergerfs", "/proc/mounts")
|
||||||
|
|
||||||
return Capabilities{
|
return Capabilities{
|
||||||
Rsync: len(rsyncOut) > 0,
|
Rsync: rsyncErr == nil,
|
||||||
MergerfsBin: len(mergerfsOut) > 0,
|
MergerfsBin: mergerfsErr == nil,
|
||||||
MergerfsMounted: mergerfsMountedErr == nil,
|
MergerfsMounted: mergerfsMountedErr == nil,
|
||||||
SnapraidBin: len(snapraidOut) > 0,
|
SnapraidBin: snapraidErr == nil,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os/exec"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProbeDetectsRsync(t *testing.T) {
|
||||||
|
caps, err := Probe(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Probe: %v", err)
|
||||||
|
}
|
||||||
|
_, rsyncErr := exec.LookPath("rsync")
|
||||||
|
if rsyncErr != nil && caps.Rsync {
|
||||||
|
t.Error("Probe claims rsync is available but exec.LookPath says it is not")
|
||||||
|
}
|
||||||
|
if rsyncErr == nil && !caps.Rsync {
|
||||||
|
t.Error("exec.LookPath found rsync but Probe did not detect it")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProbeDetectsSnapraid(t *testing.T) {
|
||||||
|
caps, err := Probe(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Probe: %v", err)
|
||||||
|
}
|
||||||
|
_, snapraidErr := exec.LookPath("snapraid")
|
||||||
|
if snapraidErr != nil && caps.SnapraidBin {
|
||||||
|
t.Error("Probe claims snapraid is available but exec.LookPath says it is not")
|
||||||
|
}
|
||||||
|
if snapraidErr == nil && !caps.SnapraidBin {
|
||||||
|
t.Error("exec.LookPath found snapraid but Probe did not detect it")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProbeDetectsMergerfsBin(t *testing.T) {
|
||||||
|
caps, err := Probe(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Probe: %v", err)
|
||||||
|
}
|
||||||
|
_, mergerfsErr := exec.LookPath("mergerfs")
|
||||||
|
if mergerfsErr != nil && caps.MergerfsBin {
|
||||||
|
t.Error("Probe claims mergerfs is available but exec.LookPath says it is not")
|
||||||
|
}
|
||||||
|
if mergerfsErr == nil && !caps.MergerfsBin {
|
||||||
|
t.Error("exec.LookPath found mergerfs but Probe did not detect it")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user