Skip to content

Commit 3d2cac7

Browse files
committed
Move binary version variables to a seperate package
1 parent e278e1a commit 3d2cac7

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ AWS_SERVICE=$(shell echo $(SERVICE) | tr '[:upper:]' '[:lower:]')
99
VERSION ?= "v0.0.0"
1010
GITCOMMIT=$(shell git rev-parse HEAD)
1111
BUILDDATE=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
12-
GO_LDFLAGS=-ldflags "-X main.version=$(VERSION) \
13-
-X main.buildHash=$(GITCOMMIT) \
14-
-X main.buildDate=$(BUILDDATE)"
12+
IMPORT_PATH=github.com/aws-controllers-k8s/code-generator
13+
GO_LDFLAGS=-ldflags "-X $(IMPORT_PATH)/pkg/version.Version=$(VERSION) \
14+
-X $(IMPORT_PATH)/pkg/version.BuildHash=$(GITCOMMIT) \
15+
-X $(IMPORT_PATH)/pkg/version.BuildDate=$(BUILDDATE)"
1516

1617
# We need to use the codegen tag when building and testing because the
1718
# aws-sdk-go/private/model/api package is gated behind a build tag "codegen"...

cmd/ack-generate/command/root.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ A tool to generate AWS service controller code`
3030
)
3131

3232
var (
33-
version string
34-
buildHash string
35-
buildDate string
3633
defaultCacheDir string
3734
optCacheDir string
3835
optRefreshCache bool
@@ -125,11 +122,7 @@ func init() {
125122
// Execute adds all child commands to the root command and sets flags
126123
// appropriately. This is called by main.main(). It only needs to happen once
127124
// to the rootCmd.
128-
func Execute(v string, bh string, bd string) {
129-
version = v
130-
buildHash = bh
131-
buildDate = bd
132-
125+
func Execute() {
133126
if err := rootCmd.Execute(); err != nil {
134127
os.Exit(1)
135128
}

cmd/ack-generate/command/version.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ package command
1515

1616
import (
1717
"fmt"
18-
"runtime"
1918

2019
"github.com/spf13/cobra"
20+
21+
"github.com/aws-controllers-k8s/code-generator/pkg/version"
2122
)
2223

2324
const debugHeader = `Date: %s
@@ -31,8 +32,7 @@ var versionCmd = &cobra.Command{
3132
Use: "version",
3233
Short: "Display the version of " + appName,
3334
Run: func(cmd *cobra.Command, args []string) {
34-
goVersion := fmt.Sprintf("%s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH)
35-
fmt.Printf(debugHeader, buildDate, goVersion, version, buildHash)
35+
fmt.Printf(debugHeader, version.BuildDate, version.GoVersion, version.Version, version.BuildHash)
3636
},
3737
}
3838

cmd/ack-generate/main.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@ import (
1717
"github.com/aws-controllers-k8s/code-generator/cmd/ack-generate/command"
1818
)
1919

20-
var (
21-
// version of application at compile time (-X 'main.version=$(VERSION)').
22-
version = "(Unknown Version)"
23-
// buildHash GIT hash of application at compile time (-X 'main.buildHash=$(GITCOMMIT)').
24-
buildHash = "No Git-hash Provided."
25-
// buildDate of application at compile time (-X 'main.buildDate=$(BUILDDATE)').
26-
buildDate = "No Build Date Provided."
27-
)
28-
2920
func main() {
30-
command.Execute(version, buildHash, buildDate)
21+
command.Execute()
3122
}

pkg/version/version.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package version
15+
16+
import (
17+
"fmt"
18+
"runtime"
19+
)
20+
21+
var (
22+
// BuildDate of application at compile time (-X 'main.buildDate=$(BUILDDATE)').
23+
BuildDate string = "No Build Date Provided."
24+
// Version of application at compile time (-X 'main.version=$(VERSION)').
25+
Version string = "(Unknown Version)"
26+
// BuildHash is the GIT hash of application at compile time (-X 'main.buildHash=$(GITCOMMIT)').
27+
BuildHash string = "No Git-hash Provided."
28+
// GoVersion is the Go compiler version used to compile this binary
29+
GoVersion string
30+
)
31+
32+
func init() {
33+
GoVersion = fmt.Sprintf("%s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH)
34+
}

0 commit comments

Comments
 (0)