added function to generate calver variables
parent
ff4507545b
commit
f391c0dc72
@ -0,0 +1,115 @@
|
||||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Polyform License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package environ
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func calversions(s string) map[string]string {
|
||||
env := map[string]string{}
|
||||
|
||||
version := parseCalver(s)
|
||||
if version == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// we try to determine if the major and minor
|
||||
// versions are valid years.
|
||||
if !isYear(version.Major) {
|
||||
return env
|
||||
}
|
||||
|
||||
env["DRONE_CALVER"] = version.String()
|
||||
env["DRONE_CALVER_MAJOR"] = version.Major
|
||||
env["DRONE_CALVER_MINOR"] = version.Minor
|
||||
env["DRONE_CALVER_MICRO"] = version.Micro
|
||||
if version.Modifier != "" {
|
||||
env["DRONE_CALVER_MODIFIER"] = version.Modifier
|
||||
}
|
||||
|
||||
version.Modifier = ""
|
||||
env["DRONE_CALVER_SHORT"] = version.String()
|
||||
return env
|
||||
}
|
||||
|
||||
type calver struct {
|
||||
Major string
|
||||
Minor string
|
||||
Micro string
|
||||
Modifier string
|
||||
}
|
||||
|
||||
// helper function that parses tags in the calendar version
|
||||
// format. note this is not a robust parser implementation
|
||||
// and mat fail to properly parse all strings.
|
||||
func parseCalver(s string) *calver {
|
||||
s = strings.TrimPrefix(s, "v")
|
||||
p := strings.SplitN(s, ".", 3)
|
||||
if len(p) < 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
c := new(calver)
|
||||
c.Major = p[0]
|
||||
c.Minor = p[1]
|
||||
if len(p) > 2 {
|
||||
c.Micro = p[2]
|
||||
}
|
||||
|
||||
switch {
|
||||
case strings.Contains(c.Micro, "-"):
|
||||
p := strings.SplitN(c.Micro, "-", 2)
|
||||
c.Micro = p[0]
|
||||
c.Modifier = p[1]
|
||||
}
|
||||
|
||||
// the major and minor segments must be numbers to
|
||||
// conform to the calendar version spec.
|
||||
if !isNumber(c.Major) ||
|
||||
!isNumber(c.Minor) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// String returns the calendar version string.
|
||||
func (c *calver) String() string {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString(c.Major)
|
||||
buf.WriteString(".")
|
||||
buf.WriteString(c.Minor)
|
||||
if c.Micro != "" {
|
||||
buf.WriteString(".")
|
||||
buf.WriteString(c.Micro)
|
||||
}
|
||||
if c.Modifier != "" {
|
||||
buf.WriteString("-")
|
||||
buf.WriteString(c.Modifier)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// helper function returns true if the string is a
|
||||
// valid number.
|
||||
func isNumber(s string) bool {
|
||||
_, err := strconv.Atoi(s)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// helper function returns true if the string is a
|
||||
// valid year. This assumes a minimum year of 2019
|
||||
// for YYYY format and a minimum year of 19 for YY
|
||||
// format.
|
||||
//
|
||||
// TODO(bradrydzewski) if people are still using this
|
||||
// code in 2099 we need to adjust the minimum YY value.
|
||||
func isYear(s string) bool {
|
||||
i, _ := strconv.Atoi(s)
|
||||
return (i > 18 && i < 100) || (i > 2018 && i < 9999)
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Polyform License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package environ
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
func TestCalver(t *testing.T) {
|
||||
a := calversions("v19.1.0-beta.20190318")
|
||||
b := map[string]string{
|
||||
"DRONE_CALVER": "19.1.0-beta.20190318",
|
||||
"DRONE_CALVER_MAJOR": "19",
|
||||
"DRONE_CALVER_MINOR": "1",
|
||||
"DRONE_CALVER_MICRO": "0",
|
||||
"DRONE_CALVER_SHORT": "19.1.0",
|
||||
"DRONE_CALVER_MODIFIER": "beta.20190318",
|
||||
}
|
||||
if diff := cmp.Diff(a, b); diff != "" {
|
||||
t.Errorf("Unexpected calver variables")
|
||||
t.Log(diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalverAlternate(t *testing.T) {
|
||||
a := calversions("2019.01.0002")
|
||||
b := map[string]string{
|
||||
"DRONE_CALVER": "2019.01.0002",
|
||||
"DRONE_CALVER_MAJOR": "2019",
|
||||
"DRONE_CALVER_MINOR": "01",
|
||||
"DRONE_CALVER_MICRO": "0002",
|
||||
"DRONE_CALVER_SHORT": "2019.01.0002",
|
||||
}
|
||||
if diff := cmp.Diff(a, b); diff != "" {
|
||||
t.Errorf("Unexpected calver variables")
|
||||
t.Log(diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalver_Invalid(t *testing.T) {
|
||||
tests := []string{
|
||||
"1.2.3",
|
||||
"1.2",
|
||||
"1",
|
||||
"0.12",
|
||||
"0.12.1",
|
||||
}
|
||||
for _, s := range tests {
|
||||
envs := calversions(s)
|
||||
if len(envs) != 0 {
|
||||
t.Errorf("Expect invalid calversion: %s", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalverParser(t *testing.T) {
|
||||
tests := []struct {
|
||||
s string
|
||||
v *calver
|
||||
}{
|
||||
{"09.01.02", &calver{"09", "01", "02", ""}},
|
||||
{"2009.01.02", &calver{"2009", "01", "02", ""}},
|
||||
{"2009.1.2", &calver{"2009", "1", "2", ""}},
|
||||
{"09.1.2", &calver{"09", "1", "2", ""}},
|
||||
{"9.1.2", &calver{"9", "1", "2", ""}},
|
||||
{"v19.1.0-beta.20190318", &calver{"19", "1", "0", "beta.20190318"}},
|
||||
|
||||
// invalid values
|
||||
{"foo.bar.baz", nil},
|
||||
{"foo.bar", nil},
|
||||
{"foo.1", nil},
|
||||
{"foo", nil},
|
||||
{"1", nil},
|
||||
{"1.foo", nil},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
got, want := parseCalver(test.s), test.v
|
||||
if diff := cmp.Diff(got, want); diff != "" {
|
||||
t.Errorf("Unexpected calver %s", test.s)
|
||||
t.Log(diff)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue