Skip to content

Commit

Permalink
[FAB-4351] Add version cmd to cryptogen
Browse files Browse the repository at this point in the history
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 <gari.r.singh@gmail.com>
  • Loading branch information
mastersingh24 committed Jun 5, 2017
1 parent 670be92 commit e776adc
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ release: $(patsubst %,release/%, $(shell go env GOOS)-$(shell go env GOARCH))
# builds release packages for all target platforms
release-all: $(patsubst %,release/%, $(RELEASE_PLATFORMS))

release/%: GO_LDFLAGS=-X $(pkgmap.$(@F))/metadata.Version=$(PROJECT_VERSION)

release/windows-amd64: GOOS=windows
release/windows-amd64: GO_TAGS+= nopkcs11
release/windows-amd64: $(patsubst %,release/windows-amd64/bin/%, $(RELEASE_PKGS)) release/windows-amd64/install
Expand Down
11 changes: 11 additions & 0 deletions common/tools/cryptogen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"io/ioutil"

"github.com/hyperledger/fabric/common/tools/cryptogen/ca"
"github.com/hyperledger/fabric/common/tools/cryptogen/metadata"
"github.com/hyperledger/fabric/common/tools/cryptogen/msp"
)

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

showtemplate = app.Command("showtemplate", "Show the default configuration template")

version = app.Command("version", "Show version information")
)

func main() {
Expand All @@ -212,6 +215,10 @@ func main() {
case showtemplate.FullCommand():
fmt.Print(defaultConfig)
os.Exit(0)

// "version" command
case version.FullCommand():
printVersion()
}

}
Expand Down Expand Up @@ -537,3 +544,7 @@ func copyFile(src, dst string) error {
}
return cerr
}

func printVersion() {
fmt.Println(metadata.GetVersionInfo())
}
32 changes: 32 additions & 0 deletions common/tools/cryptogen/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package metadata

import (
"fmt"
"runtime"
)

// package-scoped variables

// Package version
var Version string

// package-scoped constants

// Program name
const ProgramName = "cryptogen"

func GetVersionInfo() string {
if Version == "" {
Version = "development build"
}

return fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s",
ProgramName, Version, runtime.Version(),
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
}
26 changes: 26 additions & 0 deletions common/tools/cryptogen/metadata/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package metadata_test

import (
"fmt"
"runtime"
"testing"

"github.com/hyperledger/fabric/common/tools/cryptogen/metadata"
"github.com/stretchr/testify/assert"
)

func TestGetVersionInfo(t *testing.T) {
testVersion := "TestVersion"
metadata.Version = testVersion

expected := fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s",
metadata.ProgramName, testVersion, runtime.Version(),
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
assert.Equal(t, expected, metadata.GetVersionInfo())
}

0 comments on commit e776adc

Please sign in to comment.