Skip to content

Commit 74599c8

Browse files
Merge pull request #484 from feichashao/build_version
Feat: Get version from build info
2 parents 03a4452 + a568acb commit 74599c8

File tree

9 files changed

+215
-4
lines changed

9 files changed

+215
-4
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ mock-gen:
118118
mockgen -destination=./pkg/jira/mocks/jiraMock.go -package=mocks github.com/openshift/backplane-cli/pkg/jira IssueServiceInterface
119119
mockgen -destination=./pkg/healthcheck/mocks/networkMock.go -package=mocks github.com/openshift/backplane-cli/pkg/healthcheck NetworkInterface
120120
mockgen -destination=./pkg/healthcheck/mocks/httpClientMock.go -package=mocks github.com/openshift/backplane-cli/pkg/healthcheck HTTPClient
121+
mockgen -destination=./pkg/info/mocks/infoMock.go -package=mocks github.com/openshift/backplane-cli/pkg/info InfoService
122+
mockgen -destination=./pkg/info/mocks/buildInfoMock.go -package=mocks github.com/openshift/backplane-cli/pkg/info BuildInfoService
121123

122124
.PHONY: build-image
123125
build-image:

cmd/ocm-backplane/version/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var VersionCmd = &cobra.Command{
1919

2020
func runVersion(cmd *cobra.Command, argv []string) error {
2121
// Print the version
22-
_, _ = fmt.Fprintf(os.Stdout, "%s\n", info.Version)
22+
_, _ = fmt.Fprintf(os.Stdout, "%s\n", info.DefaultInfoService.GetVersion())
2323

2424
return nil
2525
}

pkg/info/buildInfo.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package info
2+
3+
import "runtime/debug"
4+
5+
type BuildInfoService interface {
6+
// return the BuildInfo from Go build
7+
GetBuildInfo() (info *debug.BuildInfo, ok bool)
8+
}
9+
10+
type DefaultBuildInfoServiceImpl struct {
11+
}
12+
13+
func (b *DefaultBuildInfoServiceImpl) GetBuildInfo() (info *debug.BuildInfo, ok bool) {
14+
return debug.ReadBuildInfo()
15+
}
16+
17+
var DefaultBuildInfoService BuildInfoService = &DefaultBuildInfoServiceImpl{}

pkg/info/info.go

+26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package info
44

55
import (
66
"fmt"
7+
"strings"
78
)
89

910
const (
@@ -57,3 +58,28 @@ var (
5758

5859
UpstreamREADMETagged = fmt.Sprintf(UpstreamREADMETemplate, Version)
5960
)
61+
62+
type InfoService interface {
63+
// get the current binary version from available sources
64+
GetVersion() string
65+
}
66+
67+
type DefaultInfoServiceImpl struct {
68+
}
69+
70+
func (i *DefaultInfoServiceImpl) GetVersion() string {
71+
// If the Version is set by Goreleaser, return it directly.
72+
if Version != "" {
73+
return Version
74+
}
75+
76+
// otherwise, return the build info from Go build if available.
77+
buildInfo, available := DefaultBuildInfoService.GetBuildInfo()
78+
if available {
79+
return strings.TrimLeft(buildInfo.Main.Version, "v")
80+
}
81+
82+
return "unknown"
83+
}
84+
85+
var DefaultInfoService InfoService = &DefaultInfoServiceImpl{}

pkg/info/info_suite_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package info_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestInfo(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Info Service Suite")
13+
}

pkg/info/info_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package info_test
2+
3+
import (
4+
"runtime/debug"
5+
6+
"github.com/golang/mock/gomock"
7+
. "github.com/onsi/ginkgo"
8+
. "github.com/onsi/gomega"
9+
"github.com/openshift/backplane-cli/pkg/info"
10+
infoMock "github.com/openshift/backplane-cli/pkg/info/mocks"
11+
)
12+
13+
var _ = Describe("Info", func() {
14+
var (
15+
mockCtrl *gomock.Controller
16+
mockBuildInfoService *infoMock.MockBuildInfoService
17+
)
18+
19+
BeforeEach(func() {
20+
mockCtrl = gomock.NewController(GinkgoT())
21+
mockBuildInfoService = infoMock.NewMockBuildInfoService(mockCtrl)
22+
info.DefaultBuildInfoService = mockBuildInfoService
23+
})
24+
25+
AfterEach(func() {
26+
mockCtrl.Finish()
27+
})
28+
29+
Context("When getting build version", func() {
30+
It("Should return the pre-set Version is available", func() {
31+
info.Version = "whatever"
32+
33+
version := info.DefaultInfoService.GetVersion()
34+
Expect(version).To(Equal("whatever"))
35+
})
36+
It("Should return a version when go bulid info is available and there is no pre-set Version", func() {
37+
info.Version = ""
38+
mockBuildInfoService.EXPECT().GetBuildInfo().Return(&debug.BuildInfo{
39+
Main: debug.Module{
40+
Version: "v1.2.3",
41+
},
42+
}, true).Times(1)
43+
44+
version := info.DefaultInfoService.GetVersion()
45+
Expect(version).To(Equal("1.2.3"))
46+
})
47+
It("Should return an unknown when no way to determine version", func() {
48+
info.Version = ""
49+
mockBuildInfoService.EXPECT().GetBuildInfo().Return(nil, false).Times(1)
50+
version := info.DefaultInfoService.GetVersion()
51+
Expect(version).To(Equal("unknown"))
52+
})
53+
})
54+
})

pkg/info/mocks/buildInfoMock.go

+50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/info/mocks/infoMock.go

+48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/utils/util.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,14 @@ func CheckBackplaneVersion(cmd *cobra.Command) {
249249
// GitHub API keeps the v prefix in front which causes mismatch with info.Version
250250
latestVersion := strings.TrimLeft(latestVersionTag.TagName, "v")
251251

252+
currentVersion := info.DefaultInfoService.GetVersion()
252253
// Check if the local version is already up-to-date
253-
if latestVersion == info.Version {
254-
logger.WithField("Current version", info.Version).Info("Already up-to-date")
254+
if latestVersion == currentVersion {
255+
logger.WithField("Current version", currentVersion).Info("Already up-to-date")
255256
return
256257
}
257258

258-
logger.WithField("Current version", info.Version).WithField("Latest version", latestVersion).Warn("Your Backplane CLI is not up to date. Please run the command 'ocm backplane upgrade' to upgrade to the latest version")
259+
logger.WithField("Current version", currentVersion).WithField("Latest version", latestVersion).Warn("Your Backplane CLI is not up to date. Please run the command 'ocm backplane upgrade' to upgrade to the latest version")
259260
}
260261

261262
// CheckValidPrompt checks that the stdin and stderr are valid for prompt

0 commit comments

Comments
 (0)