// Package component resolves companion executables within one installation. package component import ( "errors" "fmt" "os" "os/exec" "path/filepath" "syscall" ) type launchError struct { cause error path string code int } func (e *launchError) Error() string { if e.code == 127 { return fmt.Sprintf("missing component %s (expected %s)", filepath.Base(e.path), e.path) } return fmt.Sprintf("cannot launch %s: %v", e.path, e.cause) } func (e *launchError) Unwrap() error { return e.cause } func ExitCode(err error) int { if e, ok := errors.AsType[*launchError](err); ok { return e.code } return 1 } func Path(role string) (string, error) { self, err := os.Executable() if err != nil { return "", err } self, err = filepath.EvalSymlinks(self) if err != nil { return "", err } path := filepath.Join(filepath.Dir(self), "albedo-"+role) if _, err = exec.LookPath(path); err != nil { code := 126 if _, statErr := os.Stat(path); os.IsNotExist(statErr) { code = 127 err = statErr } return "", &launchError{path: path, cause: err, code: code} } return path, nil } func Exec(role string, args []string) error { path, err := Path(role) if err != nil { return err } // Keep the public invocation path for service definitions across upgrades. err = syscall.Exec(path, append([]string{os.Args[0]}, args...), os.Environ()) return &launchError{path: path, cause: err, code: 126} }