🏷️ ParseVersionOutput
Parses the output of composer --version, extracting the version number, major/minor/patch numbers, and release date, and returns *VersionInfo.
When to use
Use this when you already have the text output of composer --version and need to parse a string like Composer version 2.6.6 2024-02-22 15:37:50 into structured data. It is the parser called internally by GetVersionInfo.
Signature
go
func ParseVersionOutput(output string) (*VersionInfo, error)Parameters
| Parameter | Type | Description |
|---|---|---|
output | string | The raw output of composer --version |
Return value
*VersionInfo: version information, containingVersion(e.g.2.6.6),Major/Minor/Patch(integers),ReleaseDate(time.Time, optional), andFullOutput(the original string).error: returns a parse error when the output is too short or theversionkeyword is not found.
Example
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
output := "Composer version 2.6.6 2024-02-22 15:37:50"
info, err := composer.ParseVersionOutput(output)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Composer %d.%d.%d (released %s)\n", info.Major, info.Minor, info.Patch, info.ReleaseDate.Format("2006-01-02"))
}Advanced
- 🔍 Parsing logic: first tokenizes with
strings.Fields, locates the version string after theversionkeyword, parsesmajor.minor.patchwithfmt.Sscanf, then parses the date in2006-01-02 15:04:05format withtime.Parse. - 🚀 To execute the command and parse in one step, use
GetVersionInfo()(internally runsRun("--version")and then calls this parser). - 🔗 If you only need to extract the version string without structuring, use the lighter-weight
ExtractVersionFromOutput.