github.com/cloudfoundry-incubator/stembuild@v0.0.0-20211223202937-5b61d62226c6/package_stemcell/ovftool/ovftool.go (about)

     1  package ovftool
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  )
    10  
    11  var stopWalk = errors.New("stop walk")
    12  
    13  func findExecutable(root, name string) (string, error) {
    14  	var file string
    15  	walkFn := func(path string, fi os.FileInfo, err error) error {
    16  		if err != nil {
    17  			return err
    18  		}
    19  		if !fi.IsDir() && fi.Name() == name {
    20  			if s, err := exec.LookPath(path); err == nil {
    21  				file = s
    22  				return stopWalk
    23  			}
    24  		}
    25  		return nil
    26  	}
    27  	err := filepath.Walk(root, walkFn)
    28  	if file == "" {
    29  		if err == nil || err == stopWalk {
    30  			err = fmt.Errorf("executable file not found in: %s", root)
    31  		}
    32  		// CEV: this should never happen
    33  		if err == stopWalk {
    34  			err = fmt.Errorf("executable file not found in: %s - exec.LookPath error.", root)
    35  		}
    36  		return "", &exec.Error{Name: name, Err: err}
    37  	}
    38  	return file, nil
    39  }