2017-10-31 08:02:01 +01:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/coreos/go-semver/semver"
|
|
|
|
)
|
|
|
|
|
2017-10-31 16:49:56 +01:00
|
|
|
// DefaultTagSuffix returns a set of default suggested tags
|
|
|
|
// based on the commit ref with an attached suffix.
|
2020-05-19 19:26:13 +02:00
|
|
|
func DefaultTagSuffix(ref, suffix string) ([]string, error) {
|
|
|
|
tags, err := DefaultTags(ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-31 16:49:56 +01:00
|
|
|
if len(suffix) == 0 {
|
2020-05-19 19:26:13 +02:00
|
|
|
return tags, nil
|
2017-10-31 16:49:56 +01:00
|
|
|
}
|
|
|
|
for i, tag := range tags {
|
|
|
|
if tag == "latest" {
|
|
|
|
tags[i] = suffix
|
|
|
|
} else {
|
|
|
|
tags[i] = fmt.Sprintf("%s-%s", tag, suffix)
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 19:26:13 +02:00
|
|
|
return tags, nil
|
2017-10-31 16:49:56 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 12:49:22 +01:00
|
|
|
func splitOff(input string, delim string) string {
|
|
|
|
parts := strings.SplitN(input, delim, 2)
|
|
|
|
|
|
|
|
if len(parts) == 2 {
|
|
|
|
return parts[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
2017-10-31 16:49:56 +01:00
|
|
|
// DefaultTags returns a set of default suggested tags based on
|
2017-10-31 08:02:01 +01:00
|
|
|
// the commit ref.
|
2020-05-19 19:26:13 +02:00
|
|
|
func DefaultTags(ref string) ([]string, error) {
|
2017-10-31 08:02:01 +01:00
|
|
|
if !strings.HasPrefix(ref, "refs/tags/") {
|
2020-05-19 19:26:13 +02:00
|
|
|
return []string{"latest"}, nil
|
2017-10-31 08:02:01 +01:00
|
|
|
}
|
|
|
|
v := stripTagPrefix(ref)
|
|
|
|
version, err := semver.NewVersion(v)
|
|
|
|
if err != nil {
|
2020-05-19 19:26:13 +02:00
|
|
|
return []string{"latest"}, err
|
2017-10-31 08:02:01 +01:00
|
|
|
}
|
|
|
|
if version.PreRelease != "" || version.Metadata != "" {
|
|
|
|
return []string{
|
|
|
|
version.String(),
|
2020-05-19 19:26:13 +02:00
|
|
|
}, nil
|
2017-10-31 08:02:01 +01:00
|
|
|
}
|
2019-02-12 12:49:22 +01:00
|
|
|
|
|
|
|
v = stripTagPrefix(ref)
|
|
|
|
v = splitOff(splitOff(v, "+"), "-")
|
|
|
|
dotParts := strings.SplitN(v, ".", 3)
|
|
|
|
|
2017-10-31 08:02:01 +01:00
|
|
|
if version.Major == 0 {
|
|
|
|
return []string{
|
2019-02-12 12:49:22 +01:00
|
|
|
fmt.Sprintf("%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor),
|
|
|
|
fmt.Sprintf("%0*d.%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor, len(dotParts[2]), version.Patch),
|
2020-05-19 19:26:13 +02:00
|
|
|
}, nil
|
2017-10-31 08:02:01 +01:00
|
|
|
}
|
|
|
|
return []string{
|
2019-02-12 12:49:22 +01:00
|
|
|
fmt.Sprintf("%0*d", len(dotParts[0]), version.Major),
|
|
|
|
fmt.Sprintf("%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor),
|
|
|
|
fmt.Sprintf("%0*d.%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor, len(dotParts[2]), version.Patch),
|
2020-05-19 19:26:13 +02:00
|
|
|
}, nil
|
2017-10-31 08:02:01 +01:00
|
|
|
}
|
|
|
|
|
2017-11-09 22:28:19 +01:00
|
|
|
// UseDefaultTag for keep only default branch for latest tag
|
|
|
|
func UseDefaultTag(ref, defaultBranch string) bool {
|
|
|
|
if strings.HasPrefix(ref, "refs/tags/") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if stripHeadPrefix(ref) == defaultBranch {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func stripHeadPrefix(ref string) string {
|
|
|
|
return strings.TrimPrefix(ref, "refs/heads/")
|
|
|
|
}
|
|
|
|
|
2017-10-31 08:02:01 +01:00
|
|
|
func stripTagPrefix(ref string) string {
|
|
|
|
ref = strings.TrimPrefix(ref, "refs/tags/")
|
|
|
|
ref = strings.TrimPrefix(ref, "v")
|
|
|
|
return ref
|
|
|
|
}
|