Skip to content

🏷️ 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

ParameterTypeDescription
outputstringThe raw output of composer --version

Return value

  • *VersionInfo: version information, containing Version (e.g. 2.6.6), Major/Minor/Patch (integers), ReleaseDate (time.Time, optional), and FullOutput (the original string).
  • error: returns a parse error when the output is too short or the version keyword 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 the version keyword, parses major.minor.patch with fmt.Sscanf, then parses the date in 2006-01-02 15:04:05 format with time.Parse.
  • 🚀 To execute the command and parse in one step, use GetVersionInfo() (internally runs Run("--version") and then calls this parser).
  • 🔗 If you only need to extract the version string without structuring, use the lighter-weight ExtractVersionFromOutput.

Released under the MIT License