Skip to content

⚙️ Config

A struct controlling installer behavior — where to install, whether to use sudo, proxy settings, target version, and whether to auto-install PHP.

Package path: github.com/scagogogo/composer-skills/pkg/installer

Config Struct

go
type Config struct {
    DownloadURL         string // Composer installer script download URL
    InstallPath         string // Composer installation directory
    UseProxy            bool   // Whether to use proxy for download
    ProxyURL            string // Proxy server URL
    TimeoutSeconds      int    // Download/install timeout (seconds)
    UseSudo             bool   // Whether to use sudo/admin privileges (Unix)
    PreferBrewOnMac     bool   // Whether to prefer Homebrew on macOS
    PreferPackageManager bool  // Whether to prefer system package manager on Linux
    AutoInstallPHP      bool   // Whether to auto-install PHP when missing
    TargetVersion       string // Target version: "latest"/"1"/"2"/"preview"/"2.5.1"
}

Field Details

FieldTypeDefault (DefaultConfig)Description
DownloadURLstringhttps://getcomposer.org/installerURL to fetch composer-setup.php
InstallPathstringWindows: %ProgramFiles%\Composer; darwin/linux: /usr/local/binComposer landing directory
UseProxyboolfalseWhether to use proxy for download
ProxyURLstringEmptyProxy URL, paired with UseProxy
TimeoutSecondsint300Download/install timeout in seconds
UseSudoboolfalseWhether to use sudo when writing to system directories (macOS/linux/unix)
PreferBrewOnMacbooltrueTry brew install composer first on macOS
PreferPackageManagerbooltrueTry apt/dnf/pacman etc. first on Linux
AutoInstallPHPbooltrueAuto-install PHP via package manager when missing
TargetVersionstringlatestSpecify install version; empty string equals latest

Two Preset Constructors

DefaultConfig

Returns a general default config suitable for the current OS.

go
func DefaultConfig() Config

Features:

  • ✅ Enables PreferBrewOnMac / PreferPackageManager / AutoInstallPHP on both macOS and Linux.
  • ⚠️ UseSudo=false — writing to /usr/local/bin on most desktop Linux requires manually enabling sudo or setting DefaultConfig().UseSudo=true.

SmartConfig

Further optimizes based on OS on top of DefaultConfig, giving "most likely to succeed in one shot" config.

go
func SmartConfig() Config
OSSmartConfig Adjustment
🍎 darwinPreferBrewOnMac=true, AutoInstallPHP=true
🐧 linuxPreferPackageManager=true, AutoInstallPHP=true, UseSudo=true
🪟 windowsPreferPackageManager=false, AutoInstallPHP=false (direct phar download, no PHP involvement)

When to Use SmartConfig

Most "install without thinking" scenarios can directly use SmartConfig(). It enables sudo by default on Linux and disables PHP auto-install on Windows, avoiding errors on Windows due to missing package manager.

DefaultConfig vs SmartConfig Comparison

FieldDefaultConfig (linux/darwin)SmartConfig (linux)SmartConfig (darwin)SmartConfig (windows)
InstallPath/usr/local/bin/usr/local/bin/usr/local/bin%ProgramFiles%\Composer
UseSudofalsetruefalsefalse
PreferBrewOnMactruetruetruefalse
PreferPackageManagertruetruetruefalse
AutoInstallPHPtruetruetruefalse
TargetVersionlatestlatestlatestlatest
TimeoutSeconds300300300300

Core difference: SmartConfig enables sudo on Linux, and on Windows doesn't rely on package manager or auto-install PHP — two most common pitfalls are both avoided by default.

Installation Paths

OSDefault InstallPathExecutable Output
🪟 Windows%ProgramFiles%\Composercomposer.phar + composer.bat (need to manually add to PATH)
🍎 darwin/usr/local/bincomposer.phar + composer (shell wrapper script)
🐧 linux/usr/local/bincomposer.phar + composer (shell wrapper script)

The composer wrapper script content is #!/bin/sh\nphp "<phar>" "$@", Windows .bat is @php "<phar>" %*.

Whether to Use sudo

When UseSudo=true:

  • File writing uses sudo tee / echo ... | sudo tee.
  • chmod uses sudo chmod.
  • Package manager commands prefix with sudo.
  • File deletion uses sudo rm -f.

When UseSudo=false, all operations use normal file operations; unwritable target directory returns ErrInsufficientRights.

Container Environment

CI containers, Docker images are usually already root, sudo may not exist or behave unexpectedly. Explicitly set UseSudo=false and point InstallPath to a writable directory (e.g., /usr/local/bin is usually writable in root images).

Whether to Auto-install PHP

When AutoInstallPHP=true, Install() / InstallWithProgress() when HasPHP() is false will:

  1. Call DetectLinuxDistro() to get distro and package manager.
  2. Call InstallPHP(distro, useSudo) to install PHP and common extensions (mbstring/xml/curl) via corresponding package manager.
  3. Verify again with HasPHP(), if still fails return ErrPHPNotFound.

When AutoInstallPHP=false, missing PHP directly returns ErrPHPNotFound without any attempt — suitable for environments that want strict control over PHP source.

No Auto PHP Install on Windows

Windows has no unified package manager, SmartConfig defaults AutoInstallPHP=false. Windows users need to install PHP themselves and ensure php is in PATH.

Quick Examples

One-click Install with SmartConfig

go
package main

import (
    "log"

    "github.com/scagogogo/composer-skills/pkg/installer"
)

func main() {
    inst := installer.NewInstaller(installer.SmartConfig())
    if err := inst.Install(); err != nil {
        log.Fatalf("Installation failed: %v", err)
    }
}

Custom Path + Proxy

go
package main

import (
    "log"

    "github.com/scagogogo/composer-skills/pkg/installer"
)

func main() {
    cfg := installer.DefaultConfig()
    cfg.InstallPath = "/opt/composer/bin"
    cfg.UseProxy = true
    cfg.ProxyURL = "http://127.0.0.1:7890"
    cfg.TimeoutSeconds = 120
    cfg.TargetVersion = "2.5.1"

    inst := installer.NewInstaller(cfg)
    if err := inst.Install(); err != nil {
        log.Fatalf("Installation failed: %v", err)
    }
}

Runtime Config Switch

go
inst := installer.DefaultInstaller()
// First check current config
cfg := inst.GetConfig()
// Modify field and write back
cfg.UseSudo = true
inst.SetConfig(cfg)

Advanced

  • Target Version Semantics: TargetVersion empty or latest pulls stable; preview pulls preview; 1/2 pulls major version; like 2.5.1 pulls specific version phar. See Distro & PHP's InstallComposerVersion.
  • Proxy & Timeout: UseProxy/ProxyURL/TimeoutSeconds only affect downloads via composerutils.DownloadFile (e.g., macOS/unix direct download installer script); curl direct downloads (InstallComposerVersion internally) currently don't read proxy config.
  • Working with SmartInstaller: SmartInstaller receives InstallOptions, where Config field is this Config; empty auto-fallback to SmartConfig().

Released under the MIT License