Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify regex and use semver pkg #14

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
matrix:
include:
- go: 1.21.x
- go: 1.12.x
- go: 1.15.x

name: Build Go ${{ matrix.go }}
runs-on: ubuntu-latest
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module github.com/restic/calens

go 1.12
go 1.15

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/Masterminds/sprig/v3 v3.0.1
github.com/go-test/deep v1.0.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
)
13 changes: 11 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.0.2 h1:tRi7ENs+AaOUCH+j6qwNQgPYfV26dX3JNonq+V4mhqc=
github.com/Masterminds/semver/v3 v3.0.2/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Masterminds/sprig/v3 v3.0.1 h1:RuaOafp+8qOLUPX1lInLfUrLc1MEVbnz7a40RLoixKY=
github.com/Masterminds/sprig/v3 v3.0.1/go.mod h1:Cp7HwZjmqKrC+Y7XqSJOU2yRvAJRGLiohfgz5ZJj8+4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -26,9 +27,14 @@ github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkU
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7 h1:0hQKqeLdqlt5iIwVOBErRisrHJAN57yOiPRQItI20fU=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand All @@ -40,3 +46,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"text/template"

"github.com/Masterminds/semver/v3"
"github.com/Masterminds/sprig/v3"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -116,7 +117,7 @@ func (s ReleaseSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

var versionRegex = regexp.MustCompile(`^(\d+\.\d+\.\d+)(_(\d{4}-\d{2}-\d{2}))?$`)
var versionRegex = regexp.MustCompile(`^([^_]+)(?:_(\d{4}-\d{2}-\d{2}))?$`)

// readReleases lists the directory and parses all releases from the subdir
// names there. A valid release subdir has the format "x.y.z_YYYY-MM-DD", the
Expand Down Expand Up @@ -159,12 +160,15 @@ func readReleases(dir string) (result []Release) {
continue
}

ver := data[1]
date := data[3]
ver, err := semver.NewVersion(data[1])
if err != nil {
die("invalid subdir name %v. Parsing semver returned error: %v", filepath.Join(dir, entry.Name()), err)
}
date := data[2]

rel := Release{
path: filepath.Join(dir, entry.Name()),
Version: ver,
Version: ver.String(),
}

if date != "" {
Expand Down
37 changes: 37 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"io/ioutil"
"net/url"
"os"
"path/filepath"
"testing"
"time"

"github.com/go-test/deep"
"github.com/stretchr/testify/assert"
)

func parseURL(t testing.TB, s string) *url.URL {
Expand All @@ -17,6 +20,10 @@ func parseURL(t testing.TB, s string) *url.URL {
return url
}

func ptrTime(v time.Time) *time.Time {
return &v
}

func TestReadFile(t *testing.T) {
var tests = []struct {
Data string
Expand Down Expand Up @@ -165,6 +172,36 @@ https://forum.restic.net/t/getting-last-successful-backup-time/531
})
}
}
func TestReadReleases(t *testing.T) {
type testData struct {
Date *time.Time
FolderName string
Version string
}
dir := t.TempDir()
releases := []testData{
{Date: nil, FolderName: "unreleased", Version: "unreleased"},
{Date: ptrTime(time.Date(2023, time.November, 12, 0, 0, 0, 0, time.UTC)), FolderName: "2.0.0-rc.1+build.12345_2023-11-12", Version: "2.0.0-rc.1+build.12345"},
{Date: ptrTime(time.Date(2023, time.November, 10, 0, 0, 0, 0, time.UTC)), FolderName: "0.0.1-rc.1_2023-11-10", Version: "0.0.1-rc.1"},
{Date: ptrTime(time.Date(2023, time.November, 10, 0, 0, 0, 0, time.UTC)), FolderName: "1.0.1_2023-11-10", Version: "1.0.1"},
{Date: ptrTime(time.Date(2023, time.November, 9, 0, 0, 0, 0, time.UTC)), FolderName: "4.0.0_2023-11-09", Version: "4.0.0"},
{Date: ptrTime(time.Date(2023, time.November, 8, 0, 0, 0, 0, time.UTC)), FolderName: "1.0.2-alpha.10_2023-11-08", Version: "1.0.2-alpha.10"},
{Date: ptrTime(time.Date(2023, time.September, 7, 0, 0, 0, 0, time.UTC)), FolderName: "1.0.0_2023-09-07", Version: "1.0.0"},
{Date: ptrTime(time.Date(2023, time.May, 1, 0, 0, 0, 0, time.UTC)), FolderName: "12.10.21_2023-05-01", Version: "12.10.21"},
}
for _, release := range releases {
err := os.Mkdir(filepath.Join(dir, release.FolderName), 0750)
if err != nil {
t.Fatal(err)
}
}
parsedReleases := readReleases(dir)
// test the sorting and the parsing of the folder names
for i, parsedRelease := range parsedReleases {
assert.Equal(t, releases[i].Date, parsedRelease.Date)
assert.Equal(t, releases[i].Version, parsedRelease.Version)
}
}

func TestWrapIndent(t *testing.T) {
var tests = []struct {
Expand Down
Loading