🔢 Version
Get Composer's own version, self-update, and manage version constraints (caret / tilde / locked / range) for packages in a project.
Version-related capabilities live in three files: version.go (GetVersion / SelfUpdate), result_types.go (GetVersionInfo + VersionInfo + ParseVersionOutput), and version_constraints.go (VersionConstraint constants, FormatVersionConstraint, UpdatePackageVersion, LockPackageVersion, GetPackageVersions). This document consolidates them.
When to use
- 🩺 Startup self-check: confirm the Composer version meets a minimum; prompt to upgrade if too old.
- ⬆️ Ops scripts call
SelfUpdateto bring Composer up to the latest. - 📌 Lock critical dependencies to exact versions before deployment with
LockPackageVersionfor reproducibility. - 🎚️ Programmatically generate version-constraint strings (
^1.2,~1.2.3,>=1.2 <2.0) to write back intocomposer.json. - 🔍 List all available versions of a package for compatibility-matrix analysis.
Structured types
VersionInfo
Returned by GetVersionInfo / ParseVersionOutput; parses Composer version 2.6.6 2024-02-22 15:37:50 into structured fields.
type VersionInfo struct {
Version string `json:"version"`
FullOutput string `json:"full_output"`
Major int `json:"major"`
Minor int `json:"minor"`
Patch int `json:"patch"`
ReleaseDate time.Time `json:"release_date,omitempty"`
}| Field | Type | Description |
|---|---|---|
Version | string | Full version, e.g. 2.6.6 |
FullOutput | string | Raw command output |
Major / Minor / Patch | int | Major / minor / patch numbers |
ReleaseDate | time.Time | Release date (zero value if parsing fails) |
VersionConstraint
Version-constraint type constants defined in version_constraints.go, used by FormatVersionConstraint / UpdatePackageVersion.
type VersionConstraint string
const (
ExactVersion VersionConstraint = "exact" // 1.2.3
CaretVersion VersionConstraint = "caret" // ^1.2.3
TildeVersion VersionConstraint = "tilde" // ~1.2.3
RangeVersion VersionConstraint = "range" // >=1.2.0 <2.0.0
WildcardVersion VersionConstraint = "wildcard" // 1.2.*
)Method signatures
| Method | Signature | Description |
|---|---|---|
| 🔢 GetVersion | func (c *Composer) GetVersion() (string, error) | Parses out the Composer version string |
| 📊 GetVersionInfo | func (c *Composer) GetVersionInfo() (*VersionInfo, error) | Structured version info |
| ⬆️ SelfUpdate | func (c *Composer) SelfUpdate() error | composer self-update |
| 🧩 FormatVersionConstraint | func FormatVersionConstraint(version string, constraintType VersionConstraint) string | Generates a constraint string by type |
| 🎚️ UpdatePackageVersion | func (c *Composer) UpdatePackageVersion(packageName string, version string, constraintType VersionConstraint) error | Updates a package version with the given constraint |
| 🔒 LockPackageVersion | func (c *Composer) LockPackageVersion(packageName string, version string) error | Locks to an exact version |
| 📋 GetPackageVersions | func (c *Composer) GetPackageVersions(packageName string) (string, error) | composer show --all <pkg> text output |
| 🔧 ParseVersionOutput | func ParseVersionOutput(output string) (*VersionInfo, error) | Parses --version output |
Parameters
FormatVersionConstraint / UpdatePackageVersion
| Parameter | Type | Description |
|---|---|---|
version | string | Base version, e.g. 1.2 or 1.2.3 |
constraintType | VersionConstraint | Constraint-type constant |
Examples
Self-check the Composer version
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatalf("init failed: %v", err)
}
info, err := comp.GetVersionInfo()
if err != nil {
log.Fatalf("get version failed: %v", err)
}
fmt.Printf("Composer %d.%d.%d (released %s)\n",
info.Major, info.Minor, info.Patch, info.ReleaseDate.Format("2006-01-02"))
if info.Major < 2 {
fmt.Println("⬆️ Composer version too old, upgrading...")
if err := comp.SelfUpdate(); err != nil {
log.Fatal(err)
}
}
}Programmatically generate version constraints
fmt.Println(composer.FormatVersionConstraint("1.2.3", composer.ExactVersion)) // 1.2.3
fmt.Println(composer.FormatVersionConstraint("1.2.3", composer.CaretVersion)) // ^1.2.3
fmt.Println(composer.FormatVersionConstraint("1.2.3", composer.TildeVersion)) // ~1.2.3
fmt.Println(composer.FormatVersionConstraint("1.2", composer.WildcardVersion)) // 1.2.*
fmt.Println(composer.FormatVersionConstraint("1.2", composer.RangeVersion)) // >=1.2.0 <2.0.0Update a dependency with a caret constraint
if err := comp.UpdatePackageVersion("symfony/console", "5.4", composer.CaretVersion); err != nil {
log.Fatal(err)
}
// equivalent to: composer require symfony/console ^5.4Lock a critical dependency to an exact version
// Freeze the version before deployment for reproducibility
if err := comp.LockPackageVersion("symfony/console", "5.4.20"); err != nil {
log.Fatal(err)
}
// equivalent to: composer require symfony/console 5.4.20List all available versions of a package
out, err := comp.GetPackageVersions("symfony/console")
if err != nil {
log.Fatal(err)
}
fmt.Println(out)Want a structured version list
GetPackageVersions returns raw text. For a []string list, use the convenience method GetPackageVersionsList (see Convenience methods).
Advanced
Simplified handling of RangeVersion
RangeVersion assumes input like 1.2 and outputs >=1.2.0 <2.0.0 (major version +1). It only takes the first numeric segment and increments it; for complex versions (e.g. 1.2.3) it produces a less precise result like >=1.2.3.0 <2.0.0 — for complex cases, hand-write the constraint string.
UpdatePackageVersion reuses Require
UpdatePackageVersion calls RequirePackage internally, i.e. it re-runs composer require on an existing dependency to rewrite the version constraint. To add a new dependency, use RequirePackage directly.