⚙️ LocalExec
Executes a binary installed in the local package (runs composer exec).
When to use
Use this when you need to invoke a tool under vendor/bin (such as PHPUnit, PHPStan, etc.) from a Go program and reuse the working directory and environment resolved by Composer.
Signature
go
func (c *Composer) LocalExec(command string, args ...string) (string, error)Parameters
| Parameter | Type | Description |
|---|---|---|
command | string | The name of the local binary to execute, e.g. phpunit |
args | ...string | Arguments passed through to the binary |
Return value
string: the standard output text of the commanderror: 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.Fatal(err)
}
// Run phpunit under vendor/bin with --version
output, err := comp.LocalExec("phpunit", "--version")
if err != nil {
log.Fatalf("execution failed: %v", err)
}
fmt.Println(output)
}Advanced
- This method constructs the
composer exec <command> <args...>command. - If you need to attach options (such as a
--separator or working directory), useLocalExecWithOptions(see diagnosis.go). - To execute an arbitrary binary rather than a local package tool, refer to the
Execfamily (see exec.go).