Skip to content

Commit e776adc

Browse files
committed
[FAB-4351] Add version cmd to cryptogen
We need to print out the version info for cryptogen. This adds a version command: cryptogen version Note that the metadata package is self-contained in case we decide to move cryptogen to its own repo in the future. Change-Id: Icce83bb125cd9b8c7b8b6ae534fe330cf151e115 Signed-off-by: Gari Singh <[email protected]>
1 parent 670be92 commit e776adc

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ release: $(patsubst %,release/%, $(shell go env GOOS)-$(shell go env GOARCH))
289289
# builds release packages for all target platforms
290290
release-all: $(patsubst %,release/%, $(RELEASE_PLATFORMS))
291291

292+
release/%: GO_LDFLAGS=-X $(pkgmap.$(@F))/metadata.Version=$(PROJECT_VERSION)
293+
292294
release/windows-amd64: GOOS=windows
293295
release/windows-amd64: GO_TAGS+= nopkcs11
294296
release/windows-amd64: $(patsubst %,release/windows-amd64/bin/%, $(RELEASE_PKGS)) release/windows-amd64/install

common/tools/cryptogen/main.go

+11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"io/ioutil"
3131

3232
"github.com/hyperledger/fabric/common/tools/cryptogen/ca"
33+
"github.com/hyperledger/fabric/common/tools/cryptogen/metadata"
3334
"github.com/hyperledger/fabric/common/tools/cryptogen/msp"
3435
)
3536

@@ -198,6 +199,8 @@ var (
198199
configFile = gen.Flag("config", "The configuration template to use").File()
199200

200201
showtemplate = app.Command("showtemplate", "Show the default configuration template")
202+
203+
version = app.Command("version", "Show version information")
201204
)
202205

203206
func main() {
@@ -212,6 +215,10 @@ func main() {
212215
case showtemplate.FullCommand():
213216
fmt.Print(defaultConfig)
214217
os.Exit(0)
218+
219+
// "version" command
220+
case version.FullCommand():
221+
printVersion()
215222
}
216223

217224
}
@@ -537,3 +544,7 @@ func copyFile(src, dst string) error {
537544
}
538545
return cerr
539546
}
547+
548+
func printVersion() {
549+
fmt.Println(metadata.GetVersionInfo())
550+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package metadata
8+
9+
import (
10+
"fmt"
11+
"runtime"
12+
)
13+
14+
// package-scoped variables
15+
16+
// Package version
17+
var Version string
18+
19+
// package-scoped constants
20+
21+
// Program name
22+
const ProgramName = "cryptogen"
23+
24+
func GetVersionInfo() string {
25+
if Version == "" {
26+
Version = "development build"
27+
}
28+
29+
return fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s",
30+
ProgramName, Version, runtime.Version(),
31+
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package metadata_test
8+
9+
import (
10+
"fmt"
11+
"runtime"
12+
"testing"
13+
14+
"github.com/hyperledger/fabric/common/tools/cryptogen/metadata"
15+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
func TestGetVersionInfo(t *testing.T) {
19+
testVersion := "TestVersion"
20+
metadata.Version = testVersion
21+
22+
expected := fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s",
23+
metadata.ProgramName, testVersion, runtime.Version(),
24+
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
25+
assert.Equal(t, expected, metadata.GetVersionInfo())
26+
}

0 commit comments

Comments
 (0)