Skip to content

Commit ae1c30b

Browse files
authored
Merge pull request #111 from alexandrevicenzi/version
Change the version logic
2 parents 2476f63 + 5aa2deb commit ae1c30b

File tree

4 files changed

+105
-4
lines changed

4 files changed

+105
-4
lines changed

cmd/container-suseconnect/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func init() {
4040
}
4141

4242
flag.BoolFunc("version", "print version and exit", func(string) error {
43-
fmt.Println(cs.Version)
43+
fmt.Println(cs.GetVersion())
4444
os.Exit(0)
4545
return nil
4646
})

internal/logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func getLogWritter(path string) (io.WriteCloser, error) {
5959
}
6060
}
6161

62-
_, err = fmt.Fprintf(lf, "container-suseconnect %s\n", Version)
62+
_, err = fmt.Fprintf(lf, "container-suseconnect %s\n", GetVersion())
6363
if err != nil {
6464
lf.Close()
6565
return nil, err

internal/version.go

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,58 @@
1414

1515
package containersuseconnect
1616

17-
// Version holds the current version of the application.
18-
const Version = "2.5.2"
17+
import (
18+
"fmt"
19+
"runtime/debug"
20+
"strconv"
21+
)
22+
23+
var (
24+
version string
25+
revision string
26+
)
27+
28+
// GetVersion returns the version and revision if available.
29+
// If both version and revision are found, it returns "%version (%revision)".
30+
// If only version is found, it returns "%version".
31+
// If no version is found but a revision is, it returns "devel (%revision)".
32+
// If no version and no revision are found, it returns "devel".
33+
// The revision can taken from the binary if it was stamped with version
34+
// control information.
35+
func GetVersion() string {
36+
if version == "" {
37+
version = "devel"
38+
}
39+
40+
if revision == "" {
41+
bi, ok := debug.ReadBuildInfo()
42+
43+
if !ok {
44+
return version
45+
}
46+
47+
var vcsRevision string
48+
var vcsModified bool
49+
50+
for _, s := range bi.Settings {
51+
switch s.Key {
52+
case "vcs.revision":
53+
vcsRevision = s.Value
54+
case "vcs.modified":
55+
vcsModified, _ = strconv.ParseBool(s.Value)
56+
}
57+
}
58+
59+
if vcsRevision == "" {
60+
return version
61+
} else {
62+
revision = vcsRevision[:7]
63+
64+
if vcsModified {
65+
revision = revision + "+modified"
66+
}
67+
}
68+
}
69+
70+
return fmt.Sprintf("%s (%s)", version, revision)
71+
}

internal/version_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2025 SUSE LLC. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package containersuseconnect
15+
16+
import (
17+
"testing"
18+
19+
"github.com/stretchr/testify/assert"
20+
)
21+
22+
func TestGetVersionWithVersionRevisionUnset(t *testing.T) {
23+
version = ""
24+
revision = ""
25+
26+
assert.Equal(t, "devel", GetVersion())
27+
}
28+
29+
func TestGetVersionWithVersionSet(t *testing.T) {
30+
version = "1.2.3"
31+
revision = ""
32+
33+
assert.Equal(t, "1.2.3", GetVersion())
34+
}
35+
36+
func TestGetVersionWithRevisionSet(t *testing.T) {
37+
version = ""
38+
revision = "gh12345"
39+
40+
assert.Equal(t, "devel (gh12345)", GetVersion())
41+
}
42+
43+
func TestGetVersionWithVersionRevisionSet(t *testing.T) {
44+
version = "1.2.3"
45+
revision = "gh12345"
46+
47+
assert.Equal(t, "1.2.3 (gh12345)", GetVersion())
48+
}

0 commit comments

Comments
 (0)