Core API
The core API provides the fundamental functionality for creating and managing Composer instances.
Package: composer
import "github.com/scagogogo/go-composer-sdk/pkg/composer"
Types
Composer
The main struct that provides all Composer functionality.
type Composer struct {
// Contains filtered or unexported fields
}
Options
Configuration options for creating a Composer instance.
type Options struct {
ExecutablePath string // Path to composer executable
WorkingDir string // Working directory for operations
AutoInstall bool // Auto-install Composer if not found
DefaultTimeout time.Duration // Default timeout for operations
Detector *detector.Detector // Custom detector instance
Installer *installer.Installer // Custom installer instance
}
Functions
New
Creates a new Composer instance with the specified options.
func New(options Options) (*Composer, error)
Parameters:
options
- Configuration options for the Composer instance
Returns:
*Composer
- A new Composer instanceerror
- Error if creation fails
Example:
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatalf("Failed to create Composer instance: %v", err)
}
DefaultOptions
Returns default configuration options.
func DefaultOptions() Options
Returns:
Options
- Default configuration with sensible defaults
Example:
options := composer.DefaultOptions()
options.WorkingDir = "/path/to/project"
comp, err := composer.New(options)
Core Methods
IsInstalled
Checks if Composer is installed and accessible.
func (c *Composer) IsInstalled() bool
Returns:
bool
- True if Composer is installed and accessible
Example:
if !comp.IsInstalled() {
log.Fatal("Composer is not installed")
}
GetVersion
Gets the installed Composer version.
func (c *Composer) GetVersion() (string, error)
Returns:
string
- Composer version stringerror
- Error if version cannot be retrieved
Example:
version, err := comp.GetVersion()
if err != nil {
log.Printf("Failed to get version: %v", err)
return
}
fmt.Printf("Composer version: %s\n", version)
Run
Executes a raw Composer command with the given arguments.
func (c *Composer) Run(args ...string) (string, error)
Parameters:
args
- Command arguments to pass to Composer
Returns:
string
- Command outputerror
- Error if command fails
Example:
output, err := comp.Run("--version")
if err != nil {
log.Printf("Command failed: %v", err)
return
}
fmt.Println(output)
RunWithContext
Executes a Composer command with context support for cancellation and timeouts.
func (c *Composer) RunWithContext(ctx context.Context, args ...string) (string, error)
Parameters:
ctx
- Context for cancellation and timeoutargs
- Command arguments to pass to Composer
Returns:
string
- Command outputerror
- Error if command fails or context is cancelled
Example:
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
output, err := comp.RunWithContext(ctx, "install")
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
log.Println("Command timed out")
} else {
log.Printf("Command failed: %v", err)
}
return
}
Configuration Methods
SetWorkingDir
Sets the working directory for Composer operations.
func (c *Composer) SetWorkingDir(dir string)
Parameters:
dir
- Path to the working directory
Example:
comp.SetWorkingDir("/path/to/php/project")
GetWorkingDir
Gets the current working directory.
func (c *Composer) GetWorkingDir() string
Returns:
string
- Current working directory path
Example:
workDir := comp.GetWorkingDir()
fmt.Printf("Working directory: %s\n", workDir)
SetEnv
Sets environment variables for Composer operations.
func (c *Composer) SetEnv(env []string)
Parameters:
env
- Array of environment variables in "KEY=VALUE" format
Example:
comp.SetEnv([]string{
"COMPOSER_MEMORY_LIMIT=-1",
"COMPOSER_PROCESS_TIMEOUT=600",
"COMPOSER_CACHE_DIR=/tmp/composer-cache",
})
SetTimeout
Sets the default timeout for operations.
func (c *Composer) SetTimeout(timeout time.Duration)
Parameters:
timeout
- Default timeout duration
Example:
comp.SetTimeout(5 * time.Minute)
Utility Methods
SelfUpdate
Updates Composer to the latest version.
func (c *Composer) SelfUpdate() error
Returns:
error
- Error if update fails
Example:
err := comp.SelfUpdate()
if err != nil {
log.Printf("Failed to update Composer: %v", err)
}
ClearCache
Clears the Composer cache.
func (c *Composer) ClearCache() error
Returns:
error
- Error if cache clearing fails
Example:
err := comp.ClearCache()
if err != nil {
log.Printf("Failed to clear cache: %v", err)
}
Diagnose
Runs Composer's diagnostic checks.
func (c *Composer) Diagnose() (string, error)
Returns:
string
- Diagnostic outputerror
- Error if diagnostics fail
Example:
output, err := comp.Diagnose()
if err != nil {
log.Printf("Diagnostics failed: %v", err)
return
}
fmt.Println("Diagnostic results:")
fmt.Println(output)
Error Handling
All methods that can fail return an error as the last return value. Always check and handle errors appropriately:
// Good error handling
version, err := comp.GetVersion()
if err != nil {
log.Printf("Failed to get version: %v", err)
return
}
// Use the version
fmt.Printf("Composer version: %s\n", version)
Context Usage
For long-running operations, use context-aware methods to enable cancellation and timeouts:
// Create a context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
// Use context-aware method
output, err := comp.RunWithContext(ctx, "install", "--no-dev")
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
log.Println("Operation timed out")
} else {
log.Printf("Operation failed: %v", err)
}
}
Best Practices
- Always check if Composer is installed before performing operations
- Set the working directory to your PHP project root
- Use context-aware methods for long-running operations
- Handle errors appropriately - don't ignore them
- Configure environment variables as needed for your use case
- Set reasonable timeouts to prevent hanging operations