Skip to content

🧮 FormatVersionConstraint

Formats a base version string into a Composer-recognizable version constraint based on the constraint type. This is a pure function and does not execute any Composer commands.

When to use

Use it to generate a standard constraint string for front-end display, logging, or dynamically assembling require parameters. It can also be used to preview the final constraint written by UpdatePackageVersion before calling it.

Signature

go
func FormatVersionConstraint(version string, constraintType VersionConstraint) string

Parameters

ParameterTypeDescription
versionstringBase version, for example 1.2 or 1.2.3
constraintTypeVersionConstraintConstraint type enum

Constraint type values:

ConstantExample output
ExactVersion1.2.3
CaretVersion^1.2.3
TildeVersion~1.2.3
WildcardVersion1.2.*
RangeVersion>=1.2.0 <2.0.0

Return value

Return valueTypeDescription
Single return valuestringThe formatted version constraint string

Example

go
package main

import (
	"fmt"

	"github.com/scagogogo/composer-skills/pkg/composer"
)

func main() {
	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
}

Advanced

  • This function is called internally by UpdatePackageVersion.
  • To lock to an exact version, use the convenience method LockPackageVersion.
  • RangeVersion automatically increments the major version as the upper bound; for complex scenarios, assemble the constraint yourself and pass it to RequirePackage.

Released under the MIT License