📦 Installer
When the system doesn't have Composer (or even PHP), automatically install them — this is pkg/installer's responsibility.
Package path: github.com/scagogogo/composer-skills/pkg/installer
The installer package provides three layers of abstraction:
| Layer | Entry | Suitable Scenarios |
|---|---|---|
| 🧱 Basic Installer | Installer / NewInstaller / DefaultInstaller | Just want to install/uninstall/check version, no progress concern |
| ⚙️ Config-driven | Config / DefaultConfig / SmartConfig | Custom path, sudo, PHP auto-install, target version |
| 🚀 Smart Installer | SmartInstaller / EnsureComposerInstalled | Need progress callback, retry, cancellation, auto diagnostics |
Underneath there's platform dispatch (PlatformInstaller, Linux/macOS/Windows/Unix install strategies), Linux distro detection and PHP installation (distro.go), and a set of utility functions (utils.go).
🏗️ Three-layer Abstraction & Platform Dispatch
Core Capabilities
Auto Install Composer
Call getcomposer.org installer script or system package manager to install Composer to target directory.
Auto Install PHP
When AutoInstallPHP=true, missing PHP will be installed via apt/dnf/pacman/apk first, then Composer.
Cross-platform Dispatch
GetPlatformInstaller routes by GOOS to Linux/MacOS/Windows/Unix installer.
Smart Install
SmartInstaller provides progress callback, auto retry, context cancellation, post-install verification.
Distro Awareness
DetectLinuxDistro identifies ubuntu/centos/arch/alpine etc., matches correct package manager.
Utility Functions
GetPlatformName/GetArchName/CanUseSudo/ValidateInstallPath help with diagnostics and validation.
Installer Type
Basic installer, holding a Config.
type Installer struct {
config Config
}Constructor and Config
func NewInstaller(config Config) *Installer // Create with specified config
func DefaultInstaller() *Installer // Create with DefaultConfig()
func (i *Installer) GetConfig() Config // Read current config
func (i *Installer) SetConfig(config Config) // Runtime config replacementInstall and Uninstall
func (i *Installer) Install() error // Install per config (includes PHP check, package manager priority, platform dispatch)
func (i *Installer) InstallVersion(version string) error // Install specific version, e.g., "1"/"2"/"2.5.1"/"latest"/"preview"
func (i *Installer) Uninstall() error // Locate and uninstall Composer
func (i *Installer) IsInstalled() bool // Whether installed (PATH + common paths)
func (i *Installer) GetInstalledVersion() (string, error) // Installed version's --version outputInstall() internal flow:
- Check PHP: When
HasPHP()is false, ifAutoInstallPHP=truetryInstallPHP, otherwise directly returnErrPHPNotFound. - Linux Package Manager Priority: On Linux if
PreferPackageManager=true, first tryInstallComposerViaPackageManager, success and passcomposer --versionverification then return. - Platform Dispatch: Call
GetPlatformInstaller(i.config)to get platform-specificPlatformInstaller, execute itsInstall().
Common Errors
| Error Variable | Meaning |
|---|---|
ErrInstallationFailed | Installation process failed (script execution error etc.) |
ErrInsufficientRights | Insufficient permissions, need sudo / admin |
ErrUnsupportedPlatform | Unsupported OS platform |
ErrDownloadFailed | Download failed |
ErrPHPNotFound | PHP not found and auto-install failed |
ErrComposerAlreadyInstalled | Composer already installed |
ErrComposerNotFound | Composer binary not found when uninstalling/checking version |
Quick Examples
Most Carefree: Smart Ensure Installed
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/installer"
)
func main() {
// Already installed returns directly; not installed auto-installs via SmartConfig
result, err := installer.EnsureComposerInstalled(nil)
if err != nil {
log.Fatalf("Installation failed: %v", err)
}
fmt.Printf("Composer ready: %s (version %s)\n", result.ComposerPath, result.Version)
}Install with Specified Config
package main
import (
"log"
"github.com/scagogogo/composer-skills/pkg/installer"
)
func main() {
cfg := installer.DefaultConfig()
cfg.InstallPath = "/usr/local/bin"
cfg.UseSudo = true
cfg.AutoInstallPHP = true
inst := installer.NewInstaller(cfg)
if err := inst.Install(); err != nil {
log.Fatalf("Installation failed: %v", err)
}
}Install Specific Version and Check Version
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/installer"
)
func main() {
inst := installer.DefaultInstaller()
if err := inst.InstallVersion("2.5.1"); err != nil {
log.Fatalf("Installation failed: %v", err)
}
ver, err := inst.GetInstalledVersion()
if err != nil {
log.Fatalf("Version query failed: %v", err)
}
fmt.Println("Installed version:", ver)
}Sub-documents
| Document | Content |
|---|---|
| ⚙️ Config | Config fields, DefaultConfig vs SmartConfig comparison, install path/sudo/PHP auto-install |
| 💻 Platform Installer | PlatformInstaller interface, GetPlatformInstaller, each platform Install strategy |
| 🚀 Smart Installer | SmartInstaller, progress callback, retry, cancel, EnsureComposerInstalled |
| 🐧 Distro & PHP | DetectLinuxDistro, DistroInfo, PHP detection/installation, Composer version management |
| 🔧 Utility Functions | GetPlatformName / GetArchName / CanUseSudo / ValidateInstallPath |
Advanced
- Working with Detector: Before installing, use
pkg/detectorto probe; skip if installed; probe again after install to verify. - Working with Composer: After successful install, use the obtained path to initialize
composer.New(composer.Options{ExecutablePath: ...}). - Progress Visualization: CLI / TUI scenarios connect
ProgressCallbackto progress bar, see Smart Installer. - CI Environment: CI containers are usually already root,
UseSudo=falseworks;SmartConfigdefaultsUseSudo=trueon Linux, can override as needed in containers.