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

Fix Varnish version reporting #38

Merged
merged 3 commits into from
Oct 21, 2018
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
41 changes: 27 additions & 14 deletions varnish.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,18 @@ func (v *varnishVersion) queryVersion() error {
}

func (v *varnishVersion) parseVersion(version string) error {
r := regexp.MustCompile(`(\d)\.?(\d)?\.?(\d)?(?:.*revision\s(.*)\))?`)
parts := r.FindStringSubmatch(version)
r := regexp.MustCompile(`(?P<major>\d+)(\.(?P<minor>\d+))?(\.(?P<patch>\d+))?(.*revision\s(?P<revision>[0-9a-f]*)\))?`)
match := r.FindStringSubmatch(version)

parts := make(map[string]string)
for i, name := range r.SubexpNames() {
if i != 0 && name != "" {
parts[name] = match[i]
}
}

if len(parts) > 1 {
if err := v.set(parts[1:]); err != nil {
if err := v.set(parts); err != nil {
return err
}
}
Expand Down Expand Up @@ -255,25 +263,30 @@ func (v *varnishVersion) Labels() map[string]string {
return labels
}

func (v *varnishVersion) set(parts []string) error {
for i, part := range parts {
if len(part) == 0 {
func (v *varnishVersion) set(parts map[string]string) error {
for name, value := range parts {
// skip empty value
if len(value) == 0 {
continue
}
if i == 3 {
v.Revision = part
break

// save revision as-is (string)
if name == "revision" {
v.Revision = value
continue
}
num, err := strconv.Atoi(part)

// convert semver parts to integer and save it
num, err := strconv.Atoi(value)
if err != nil {
return err
}
switch i {
case 0:
switch name {
case "major":
v.Major = num
case 1:
case "minor":
v.Minor = num
case 2:
case "patch":
v.Patch = num
}
}
Expand Down
3 changes: 3 additions & 0 deletions varnish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func Test_VarnishVersion(t *testing.T) {
"varnishstat (varnish-5.2.0 revision 4c4875cbf)": &varnishVersion{
Major: 5, Minor: 2, Patch: 0, Revision: "4c4875cbf",
},
"varnishstat (varnish-4.1.10 revision 1d090c5a08f41c36562644bafcce9d3cb85d824f)": &varnishVersion{
Major: 4, Minor: 1, Patch: 10, Revision: "1d090c5a08f41c36562644bafcce9d3cb85d824f",
},
"varnishstat (varnish-4.1.0 revision 3041728)": &varnishVersion{
Major: 4, Minor: 1, Patch: 0, Revision: "3041728",
},
Expand Down