⚙️ Exec
Runs a vendor binary under the project's vendor/bin, equivalent to running composer exec <binary> -- <args...>.
When to use
Use it when you need to invoke an installed CLI tool (such as phpunit, phpstan, psql, etc. from vendor bin) from Go code with the project context, without manually constructing the vendor/bin path.
Signature
go
func (c *Composer) Exec(binary string, args ...string) (string, error)Parameters
| Parameter | Type | Description |
|---|---|---|
binary | string | Vendor binary file name, for example phpunit |
args | ...string | Argument list forwarded to the binary |
Return value
| Return value | Type | Description |
|---|---|---|
| First return value | string | The stdout output of the binary execution |
| Second return value | error | Returned when execution fails |
Example
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatalf("initialization failed: %v", err)
}
output, err := comp.Exec("phpunit", "--version")
if err != nil {
log.Fatalf("execution failed: %v", err)
}
fmt.Println(output)
}Advanced
- To list all available vendor binaries, use ExecWithList.
- To run with a specific PHP interpreter, use ExecPHP.
- To execute in a specific working directory, use
ExecWithWorkingDir; to run all binaries at once, use ExecAll.