🚀 New
Creates a new Composer instance; the entry point for using the SDK. If the executable path is not specified, it auto-detects Composer on the system; if not found and AutoInstall is enabled, it installs it automatically.
When to use
In almost all scenarios, you first call New to obtain a *Composer instance, then call methods on it. It is typically used together with DefaultOptions.
Signature
go
func New(options Options) (*Composer, error)Parameters
| Parameter | Type | Description |
|---|---|---|
options | Options | Configuration, including executable path, working directory, auto-install flag, timeout, etc. |
Return value
| Return value | Type | Description |
|---|---|---|
| First return value | *Composer | The created Composer instance |
| Second return value | error | Returned when detection and auto-install both fail, or when the specified path does not exist |
Example
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
options := composer.DefaultOptions()
options.WorkingDir = "/path/to/project"
comp, err := composer.New(options)
if err != nil {
log.Fatalf("init Composer failed: %v", err)
}
fmt.Printf("Composer ready: %s\n", comp.GetExecutablePath())
}Advanced
- To detect, install, and initialize in one line, use the more convenient QuickSetup.
- To skip auto-install and only use Composer when already installed, set
Options.AutoInstalltofalse. - After the instance is created, you can switch the working directory at any time with SetWorkingDir.