executil.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package executil provides small helpers for constructing subprocess commands.
  14. package executil
  15. import (
  16. "fmt"
  17. "os/exec"
  18. "golang.org/x/sys/execabs"
  19. )
  20. // Command resolves an executable to an absolute path before constructing the command.
  21. func Command(name string, args ...string) (*exec.Cmd, error) {
  22. path, err := execabs.LookPath(name)
  23. if err != nil {
  24. return nil, fmt.Errorf("find executable %q: %w", name, err)
  25. }
  26. //nolint:gosec // Callers intentionally choose the executable and arguments; LookPath resolves the binary first.
  27. return execabs.Command(path, args...), nil
  28. }