Skip to content

Commit 8aabdee

Browse files
committed
fix: use exec.LookPath function
This function us standard PATH environment variable Signed-off-by: Guilhem Lettron <[email protected]>
1 parent a744263 commit 8aabdee

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

pkg/hostpath/healthcheck.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"os"
2323
"os/exec"
24-
"path/filepath"
2524
"strings"
2625

2726
"github.com/golang/glog"
@@ -49,21 +48,6 @@ type FileSystems struct {
4948
Filsystem []ContainerFileSystem `json:"filesystems"`
5049
}
5150

52-
func locateCommandPath(commandName string) string {
53-
// default to root
54-
binary := filepath.Join("/", commandName)
55-
for _, path := range []string{"/bin", "/usr/sbin", "/usr/bin"} {
56-
binPath := filepath.Join(path, binary)
57-
if _, err := os.Stat(binPath); err != nil {
58-
continue
59-
}
60-
61-
return binPath
62-
}
63-
64-
return ""
65-
}
66-
6751
func getSourcePath(volumeHandle string) string {
6852
return fmt.Sprintf("%s/%s", dataRoot, volumeHandle)
6953
}
@@ -100,7 +84,11 @@ func parseMountInfo(originalMountInfo []byte) ([]MountPointInfo, error) {
10084
}
10185

10286
func checkMountPointExist(sourcePath string) (bool, error) {
103-
cmdPath := locateCommandPath("findmnt")
87+
cmdPath, err := exec.LookPath("findmnt")
88+
if err != nil {
89+
return false, fmt.Errorf("findmnt not found: %w", err)
90+
}
91+
10492
out, err := exec.Command(cmdPath, "--json").CombinedOutput()
10593
if err != nil {
10694
glog.V(3).Infof("failed to execute command: %+v", cmdPath)

pkg/hostpath/hostpath.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ func (h *hostPath) discoverExistingSnapshots() {
177177
}
178178

179179
func (hp *hostPath) discoveryExistingVolumes() error {
180-
cmdPath := locateCommandPath("findmnt")
180+
cmdPath, err := exec.LookPath("findmnt")
181+
if err != nil {
182+
return fmt.Errorf("findmnt not found: %w", err)
183+
}
184+
181185
out, err := exec.Command(cmdPath, "--json").CombinedOutput()
182186
if err != nil {
183187
glog.V(3).Infof("failed to execute command: %+v", cmdPath)

0 commit comments

Comments
 (0)