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
This commit is contained in:
Harry Míchal 2020-05-06 14:48:40 +02:00 committed by Debarshi Ray
parent b3fca25764
commit 46c21a5c14
3 changed files with 18 additions and 0 deletions

View File

@ -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

View File

@ -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=

View File

@ -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