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

     1  //go:build darwin
     2  // +build darwin
     3  
     4  package ovftool
     5  
     6  import (
     7  	"bytes"
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  )
    12  
    13  // homeDirectory returns the home directory of the current user,
    14  // errors are ignored.
    15  func homeDirectory() string {
    16  	if s := os.Getenv("HOME"); s != "" {
    17  		return s
    18  	}
    19  
    20  	out, err := exec.Command("sh", "-c", "cd ~ && pwd").Output()
    21  	if err != nil {
    22  		return ""
    23  	}
    24  
    25  	s := string(bytes.TrimSpace(out))
    26  	if s == "" {
    27  		return ""
    28  	}
    29  	return s
    30  }
    31  
    32  func SearchPaths() ([]string, error) {
    33  	// search paths
    34  	var vmwareDirs = []string{
    35  		"/Applications/VMware Fusion.app",
    36  	}
    37  	if home := homeDirectory(); home != "" {
    38  		vmwareDirs = append(vmwareDirs, filepath.Join(home, vmwareDirs[0]))
    39  	}
    40  	return vmwareDirs, nil
    41  }
    42  
    43  func Ovftool(searchPaths []string) (string, error) {
    44  	const name = "ovftool"
    45  	if path, err := exec.LookPath(name); err == nil {
    46  		return path, nil
    47  	}
    48  
    49  	for _, root := range searchPaths {
    50  		if fi, err := os.Stat(root); err != nil || !fi.IsDir() {
    51  			continue
    52  		}
    53  		if path, err := findExecutable(root, name); err == nil {
    54  			return path, nil
    55  		}
    56  	}
    57  
    58  	return "", &exec.Error{Name: name, Err: exec.ErrNotFound}
    59  }