🧮 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) stringParameters
| Parameter | Type | Description |
|---|---|---|
version | string | Base version, for example 1.2 or 1.2.3 |
constraintType | VersionConstraint | Constraint type enum |
Constraint type values:
| Constant | Example output |
|---|---|
ExactVersion | 1.2.3 |
CaretVersion | ^1.2.3 |
TildeVersion | ~1.2.3 |
WildcardVersion | 1.2.* |
RangeVersion | >=1.2.0 <2.0.0 |
Return value
| Return value | Type | Description |
|---|---|---|
| Single return value | string | The 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.
RangeVersionautomatically increments the major version as the upper bound; for complex scenarios, assemble the constraint yourself and pass it to RequirePackage.