Skip to content

Commit

Permalink
version: don't duplicate version tag if already part of the string, v…
Browse files Browse the repository at this point in the history
…ersion.DetectOS() (#18)

* version: don't duplicate version tag if already part of the string

* version/DetectOS()
  • Loading branch information
mmetc authored Jul 15, 2024
1 parent 7bba11e commit 5ee10dd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/crowdsecurity/go-cs-lib
go 1.20

require (
github.com/blackfireio/osinfo v1.0.5
github.com/coreos/go-systemd/v22 v22.5.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/blackfireio/osinfo v1.0.5 h1:6hlaWzfcpb87gRmznVf7wSdhysGqLRz9V/xuSdCEXrA=
github.com/blackfireio/osinfo v1.0.5/go.mod h1:Pd987poVNmd5Wsx6PRPw4+w7kLlf9iJxoRKPtPAjOrA=
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
18 changes: 18 additions & 0 deletions version/os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package version

import (
"github.com/blackfireio/osinfo"
)

func DetectOS() (string, string) {
osInfo, err := osinfo.GetOSInfo()
if err != nil {
return System, "???"
}

if osInfo.Name != "" && System == "docker" {
return osInfo.Name + " (docker)", osInfo.Version
}

return osInfo.Name, osInfo.Version
}
12 changes: 10 additions & 2 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package version
import (
"fmt"
"runtime"
"strings"
)

var (
Expand All @@ -14,7 +15,7 @@ var (
)

func FullString() string {
ret := fmt.Sprintf("version: %s-%s\n", Version, Tag)
ret := fmt.Sprintf("version: %s\n", String())
ret += fmt.Sprintf("BuildDate: %s\n", BuildDate)
ret += fmt.Sprintf("GoVersion: %s\n", GoVersion)
ret += fmt.Sprintf("Platform: %s\n", System)
Expand All @@ -23,5 +24,12 @@ func FullString() string {
}

func String() string {
return fmt.Sprintf("%s-%s", Version, Tag)
// if the version number already contains the tag, don't duplicate it
ret := Version

if !strings.HasSuffix(ret, Tag) && !strings.HasSuffix(ret, "g"+Tag+"-dirty") {
ret += "-" + Tag
}

return ret
}

0 comments on commit 5ee10dd

Please sign in to comment.