github.com/octohelm/cuemod@v0.9.4/pkg/version/version.go (about)

     1  package version
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"golang.org/x/mod/module"
     8  	"golang.org/x/mod/semver"
     9  )
    10  
    11  func Convert(version string, t time.Time, rev string, dirty bool) string {
    12  	exact := true
    13  	base, err := module.PseudoVersionBase(version)
    14  	if err == nil {
    15  		version = base
    16  		exact = false
    17  	}
    18  	if version == "" {
    19  		version = "v0.0.0"
    20  		exact = true
    21  	}
    22  	if dirty {
    23  		version += "-dirty"
    24  		exact = false
    25  	}
    26  	return pseudoVersion(version, t, rev, exact)
    27  }
    28  
    29  func pseudoVersion(version string, t time.Time, rev string, exact bool) string {
    30  	major := semver.Major(version)
    31  	if major == "" {
    32  		major = "v0"
    33  	}
    34  
    35  	if exact {
    36  		build := semver.Build(version)
    37  		segment := fmt.Sprintf("%s-%s", t.UTC().Format(module.PseudoVersionTimestampFormat), rev)
    38  		version = semver.Canonical(version)
    39  		if version == "" {
    40  			version = major + ".0.0"
    41  		}
    42  		return version + "-" + segment + build
    43  	}
    44  
    45  	return module.PseudoVersion(
    46  		major,
    47  		version,
    48  		t,
    49  		rev,
    50  	)
    51  }