Skip to content

⚙️ 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

ParameterTypeDescription
binarystringVendor binary file name, for example phpunit
args...stringArgument list forwarded to the binary

Return value

Return valueTypeDescription
First return valuestringThe stdout output of the binary execution
Second return valueerrorReturned 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.

Released under the MIT License