From 46c21a5c1403d7411a1aa97a76a1a2615e372786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harry=20M=C3=ADchal?= Date: Wed, 6 May 2020 14:48:40 +0200 Subject: [PATCH] podman: Add function to check if Podman is newer than a given version This is in the same style as GLib's GLIB_CHECK_VERSION [1]. Note that the github.com/HarryMichal/go-version package is a fork of github.com/mcuadros/go-version. The latter is now marked as read-only and archived by its owner. The fork was necessary to fix the documentation of the CompareSimple function. The function is supposed to conform to the strcmp convention. ie., return 0 if both versions are equal, 1 if the left side is bigger, and -1 if the right side is bigger. However, the documentation had it the other way round. [1] https://developer.gnome.org/glib/stable/glib-Version-Information.html#GLIB-CHECK-VERSION:CAPS https://github.com/containers/toolbox/pull/318 --- src/go.mod | 1 + src/go.sum | 2 ++ src/pkg/podman/podman.go | 15 +++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/src/go.mod b/src/go.mod index 41fe7e7..63c5c53 100644 --- a/src/go.mod +++ b/src/go.mod @@ -3,6 +3,7 @@ module github.com/containers/toolbox go 1.13 require ( + github.com/HarryMichal/go-version v1.0.0 github.com/sirupsen/logrus v1.5.0 github.com/spf13/cobra v0.0.6 golang.org/x/sys v0.0.0-20190422165155-953cdadca894 diff --git a/src/go.sum b/src/go.sum index ea9697b..ab8c133 100644 --- a/src/go.sum +++ b/src/go.sum @@ -1,5 +1,7 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/HarryMichal/go-version v1.0.0 h1:fXYa5vT46C3pULfSIgnfeNfSxJ9bCGZ2ERn/wKPlD6c= +github.com/HarryMichal/go-version v1.0.0/go.mod h1:w3uLQ2NlFmZ01qBywppIbDplbPEjeBW8xywlluMcMsc= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= diff --git a/src/pkg/podman/podman.go b/src/pkg/podman/podman.go index 2f4ce5b..d6154d2 100644 --- a/src/pkg/podman/podman.go +++ b/src/pkg/podman/podman.go @@ -20,6 +20,7 @@ import ( "bytes" "encoding/json" + "github.com/HarryMichal/go-version" "github.com/containers/toolbox/pkg/shell" "github.com/sirupsen/logrus" ) @@ -28,6 +29,20 @@ var ( LogLevel = logrus.ErrorLevel ) +// CheckVersion compares provided version with the version of Podman. +// +// Takes in one string parameter that should be in the format that is used for versioning (eg. 1.0.0, 2.5.1-dev). +// +// Returns true if the Podman version is equal to or higher than the required version. +func CheckVersion(requiredVersion string) bool { + podmanVersion, _ := GetVersion() + + podmanVersion = version.Normalize(podmanVersion) + requiredVersion = version.Normalize(requiredVersion) + + return version.CompareSimple(podmanVersion, requiredVersion) >= 0 +} + // GetVersion returns version of Podman in a string func GetVersion() (string, error) { var stdout bytes.Buffer