Skip to content

📊 GetVersionInfo

Gets structured information about the Composer version, including the version number, major/minor/patch version numbers, and release date. Equivalent to running composer --version and parsing the output into a *VersionInfo.

When to use

Use this when you need to branch on the major version number in code, record the release date, or generate a version report. Compared to GetVersion, which only returns a string, this method provides directly accessible fields.

Signature

go
func (c *Composer) GetVersionInfo() (*VersionInfo, error)

Parameters

This method takes no parameters.

Return value

Return valueTypeDescription
First return value*VersionInfoStructured version information
Second return valueerrorReturned when execution or parsing fails

Main fields of VersionInfo:

FieldTypeDescription
VersionstringFull version number, e.g. 2.1.6
FullOutputstringRaw command output
MajorintMajor version number
MinorintMinor version number
PatchintPatch version number
ReleaseDatetime.TimeRelease date (may be zero)

Example

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("Initialization failed: %v", err)
	}

	info, err := comp.GetVersionInfo()
	if err != nil {
		log.Fatalf("Failed to get version info: %v", err)
	}

	fmt.Printf("Composer %d.%d.%d\n", info.Major, info.Minor, info.Patch)
	if !info.ReleaseDate.IsZero() {
		fmt.Printf("Release date: %s\n", info.ReleaseDate.Format("2006-01-02"))
	}
}

Advanced

  • When you only need the version number string, use the lighter GetVersion.
  • The parsing logic is encapsulated in ParseVersionOutput(output) and can be reused for any composer --version output.
  • Call again after upgrading Composer to confirm the new version; see SelfUpdate.

Released under the MIT License