0
0
mirror of https://github.com/thegeeklab/wp-docker-buildx.git synced 2024-11-15 05:20:43 +00:00
wp-docker-buildx/tags.go

69 lines
1.6 KiB
Go
Raw Normal View History

2017-10-31 07:02:01 +00:00
package docker
import (
"fmt"
"strings"
"github.com/coreos/go-semver/semver"
)
2017-10-31 15:49:56 +00:00
// DefaultTagSuffix returns a set of default suggested tags
// based on the commit ref with an attached suffix.
func DefaultTagSuffix(ref, suffix, commitBranch, defaultBranch string) []string {
tags := DefaultTags(ref, commitBranch, defaultBranch)
2017-10-31 15:49:56 +00:00
if len(suffix) == 0 {
return tags
}
for i, tag := range tags {
if tag == "latest" {
tags[i] = suffix
} else {
tags[i] = fmt.Sprintf("%s-%s", tag, suffix)
}
}
return tags
}
// DefaultTags returns a set of default suggested tags based on
2017-10-31 07:02:01 +00:00
// the commit ref.
func DefaultTags(ref, commitBranch, defaultBranch string) []string {
if defaultBranch != "" &&
commitBranch != defaultBranch &&
!strings.HasPrefix(ref, "refs/tags/") {
return []string{}
}
2017-10-31 07:02:01 +00:00
if !strings.HasPrefix(ref, "refs/tags/") {
return []string{"latest"}
}
2017-10-31 07:02:01 +00:00
v := stripTagPrefix(ref)
version, err := semver.NewVersion(v)
if err != nil {
return []string{"latest"}
}
if version.PreRelease != "" || version.Metadata != "" {
return []string{
version.String(),
}
}
if version.Major == 0 {
return []string{
fmt.Sprintf("%d.%d", version.Major, version.Minor),
fmt.Sprintf("%d.%d.%d", version.Major, version.Minor, version.Patch),
}
}
return []string{
fmt.Sprint(version.Major),
fmt.Sprintf("%d.%d", version.Major, version.Minor),
fmt.Sprintf("%d.%d.%d", version.Major, version.Minor, version.Patch),
}
}
func stripTagPrefix(ref string) string {
ref = strings.TrimPrefix(ref, "refs/tags/")
ref = strings.TrimPrefix(ref, "v")
return ref
}