Skip to content

🔢 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 SelfUpdate to bring Composer up to the latest.
  • 📌 Lock critical dependencies to exact versions before deployment with LockPackageVersion for reproducibility.
  • 🎚️ Programmatically generate version-constraint strings (^1.2, ~1.2.3, >=1.2 <2.0) to write back into composer.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.

go
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"`
}
FieldTypeDescription
VersionstringFull version, e.g. 2.6.6
FullOutputstringRaw command output
Major / Minor / PatchintMajor / minor / patch numbers
ReleaseDatetime.TimeRelease date (zero value if parsing fails)

VersionConstraint

Version-constraint type constants defined in version_constraints.go, used by FormatVersionConstraint / UpdatePackageVersion.

go
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

MethodSignatureDescription
🔢 GetVersionfunc (c *Composer) GetVersion() (string, error)Parses out the Composer version string
📊 GetVersionInfofunc (c *Composer) GetVersionInfo() (*VersionInfo, error)Structured version info
⬆️ SelfUpdatefunc (c *Composer) SelfUpdate() errorcomposer self-update
🧩 FormatVersionConstraintfunc FormatVersionConstraint(version string, constraintType VersionConstraint) stringGenerates a constraint string by type
🎚️ UpdatePackageVersionfunc (c *Composer) UpdatePackageVersion(packageName string, version string, constraintType VersionConstraint) errorUpdates a package version with the given constraint
🔒 LockPackageVersionfunc (c *Composer) LockPackageVersion(packageName string, version string) errorLocks to an exact version
📋 GetPackageVersionsfunc (c *Composer) GetPackageVersions(packageName string) (string, error)composer show --all <pkg> text output
🔧 ParseVersionOutputfunc ParseVersionOutput(output string) (*VersionInfo, error)Parses --version output

Parameters

FormatVersionConstraint / UpdatePackageVersion

ParameterTypeDescription
versionstringBase version, e.g. 1.2 or 1.2.3
constraintTypeVersionConstraintConstraint-type constant

Examples

Self-check the Composer version

go
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

go
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.0

Update a dependency with a caret constraint

go
if err := comp.UpdatePackageVersion("symfony/console", "5.4", composer.CaretVersion); err != nil {
	log.Fatal(err)
}
// equivalent to: composer require symfony/console ^5.4

Lock a critical dependency to an exact version

go
// 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.20

List all available versions of a package

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

Released under the MIT License