pulumi/sdk/go/common/util/executable/executable.go

75 lines
2.1 KiB
Go
Raw Normal View History

package executable
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
)
const unableToFindProgramTemplate = "unable to find program: %s"
// FindExecutable attempts to find the needed executable in various locations on the
// filesystem, eventually resorting to searching in $PATH.
func FindExecutable(program string) (string, error) {
if runtime.GOOS == "windows" && !strings.HasSuffix(program, ".exe") {
Enable perfsprint linter (#14813) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Prompted by a comment in another review: https://github.com/pulumi/pulumi/pull/14654#discussion_r1419995945 This lints that we don't use `fmt.Errorf` when `errors.New` will suffice, it also covers a load of other cases where `Sprintf` is sub-optimal. Most of these edits were made by running `perfsprint --fix`. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-12 12:19:42 +00:00
program = program + ".exe"
}
// look in the same directory
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("unable to get current working directory: %w", err)
}
cwdProgram := filepath.Join(cwd, program)
if fileInfo, err := os.Stat(cwdProgram); !os.IsNotExist(err) && !fileInfo.Mode().IsDir() {
logging.V(5).Infof("program %s found in CWD", program)
return cwdProgram, nil
}
// look in potentials $GOPATH/bin
if goPath := os.Getenv("GOPATH"); len(goPath) > 0 {
// splitGoPath will return a list of paths in which to look for the binary.
// Because the GOPATH can take the form of multiple paths (https://golang.org/cmd/go/#hdr-GOPATH_environment_variable)
// we need to split the GOPATH, and look into each of the paths.
// If the GOPATH hold only one path, there will only be one element in the slice.
goPathParts := splitGoPath(goPath, runtime.GOOS)
for _, pp := range goPathParts {
goPathProgram := filepath.Join(pp, "bin", program)
fileInfo, err := os.Stat(goPathProgram)
if err != nil && !os.IsNotExist(err) {
return "", err
}
if fileInfo != nil && !fileInfo.Mode().IsDir() {
logging.V(5).Infof("program %s found in %s/bin", program, pp)
return goPathProgram, nil
}
}
}
// look in the $PATH somewhere
if fullPath, err := exec.LookPath(program); err == nil {
logging.V(5).Infof("program %s found in $PATH", program)
return fullPath, nil
}
return "", fmt.Errorf(unableToFindProgramTemplate, program)
}
func splitGoPath(goPath string, os string) []string {
var sep string
switch os {
case "windows":
sep = ";"
case "linux", "darwin":
sep = ":"
}
return strings.Split(goPath, sep)
}