📊 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 value | Type | Description |
|---|---|---|
| First return value | *VersionInfo | Structured version information |
| Second return value | error | Returned when execution or parsing fails |
Main fields of VersionInfo:
| Field | Type | Description |
|---|---|---|
Version | string | Full version number, e.g. 2.1.6 |
FullOutput | string | Raw command output |
Major | int | Major version number |
Minor | int | Minor version number |
Patch | int | Patch version number |
ReleaseDate | time.Time | Release 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 anycomposer --versionoutput. - Call again after upgrading Composer to confirm the new version; see SelfUpdate.