Skip to content

📦 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:

LayerEntrySuitable Scenarios
🧱 Basic InstallerInstaller / NewInstaller / DefaultInstallerJust want to install/uninstall/check version, no progress concern
⚙️ Config-drivenConfig / DefaultConfig / SmartConfigCustom path, sudo, PHP auto-install, target version
🚀 Smart InstallerSmartInstaller / EnsureComposerInstalledNeed 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.

go
type Installer struct {
    config Config
}

Constructor and Config

go
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 replacement

Install and Uninstall

go
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 output

Install() internal flow:

  1. Check PHP: When HasPHP() is false, if AutoInstallPHP=true try InstallPHP, otherwise directly return ErrPHPNotFound.
  2. Linux Package Manager Priority: On Linux if PreferPackageManager=true, first try InstallComposerViaPackageManager, success and pass composer --version verification then return.
  3. Platform Dispatch: Call GetPlatformInstaller(i.config) to get platform-specific PlatformInstaller, execute its Install().

Common Errors

Error VariableMeaning
ErrInstallationFailedInstallation process failed (script execution error etc.)
ErrInsufficientRightsInsufficient permissions, need sudo / admin
ErrUnsupportedPlatformUnsupported OS platform
ErrDownloadFailedDownload failed
ErrPHPNotFoundPHP not found and auto-install failed
ErrComposerAlreadyInstalledComposer already installed
ErrComposerNotFoundComposer binary not found when uninstalling/checking version

Quick Examples

Most Carefree: Smart Ensure Installed

go
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

go
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

go
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

DocumentContent
⚙️ ConfigConfig fields, DefaultConfig vs SmartConfig comparison, install path/sudo/PHP auto-install
💻 Platform InstallerPlatformInstaller interface, GetPlatformInstaller, each platform Install strategy
🚀 Smart InstallerSmartInstaller, progress callback, retry, cancel, EnsureComposerInstalled
🐧 Distro & PHPDetectLinuxDistro, DistroInfo, PHP detection/installation, Composer version management
🔧 Utility FunctionsGetPlatformName / GetArchName / CanUseSudo / ValidateInstallPath

Advanced

  • Working with Detector: Before installing, use pkg/detector to 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 ProgressCallback to progress bar, see Smart Installer.
  • CI Environment: CI containers are usually already root, UseSudo=false works; SmartConfig defaults UseSudo=true on Linux, can override as needed in containers.

Released under the MIT License