diff --git a/Makefile b/Makefile index 6667b4591ba..876e32665fa 100755 --- a/Makefile +++ b/Makefile @@ -129,10 +129,13 @@ orderer-docker: build/image/orderer/$(DUMMY) .PHONY: configtxgen configtxgen: GO_TAGS+= nopkcs11 +configtxgen: GO_LDFLAGS=-X $(pkgmap.$(@F))/metadata.Version=$(PROJECT_VERSION) configtxgen: build/bin/configtxgen +configtxlator: GO_LDFLAGS=-X $(pkgmap.$(@F))/metadata.Version=$(PROJECT_VERSION) configtxlator: build/bin/configtxlator +cryptogen: GO_LDFLAGS=-X $(pkgmap.$(@F))/metadata.Version=$(PROJECT_VERSION) cryptogen: build/bin/cryptogen tools-docker: build/image/tools/$(DUMMY) diff --git a/common/tools/configtxlator/main.go b/common/tools/configtxlator/main.go index 7f80c5b09be..12a5fc41d5a 100644 --- a/common/tools/configtxlator/main.go +++ b/common/tools/configtxlator/main.go @@ -17,25 +17,50 @@ limitations under the License. package main import ( - "flag" "fmt" "net/http" + "os" + "github.com/hyperledger/fabric/common/tools/configtxlator/metadata" "github.com/hyperledger/fabric/common/tools/configtxlator/rest" - "github.com/op/go-logging" + "gopkg.in/alecthomas/kingpin.v2" ) var logger = logging.MustGetLogger("configtxlator") +// command line flags +var ( + app = kingpin.New("configtxlator", "Utility for generating Hyperledger Fabric channel configurations") + + start = app.Command("start", "Start the configtxlator REST server") + hostname = start.Flag("hostname", "The hostname or IP on which the REST server will listen").Default("0.0.0.0").String() + port = start.Flag("port", "The port on which the REST server will listen").Default("7059").Int() + + version = app.Command("version", "Show version information") +) + func main() { - var serverPort int + kingpin.Version("0.0.1") + switch kingpin.MustParse(app.Parse(os.Args[1:])) { + // "start" command + case start.FullCommand(): + startServer(fmt.Sprintf("%s:%d", *hostname, *port)) - flag.IntVar(&serverPort, "serverPort", 7059, "Specify the port for the REST server to listen on.") - flag.Parse() + // "version" command + case version.FullCommand(): + printVersion() + } - logger.Infof("Serving HTTP requests on port: %d", serverPort) - err := http.ListenAndServe(fmt.Sprintf(":%d", serverPort), rest.NewRouter()) +} + +func startServer(address string) { + logger.Infof("Serving HTTP requests on %s", address) + err := http.ListenAndServe(address, rest.NewRouter()) + + app.Fatalf("Error starting server:[%s]\n", err) +} - logger.Fatal("Error runing http server:", err) +func printVersion() { + fmt.Println(metadata.GetVersionInfo()) } diff --git a/common/tools/configtxlator/metadata/metadata.go b/common/tools/configtxlator/metadata/metadata.go new file mode 100644 index 00000000000..4586d1ef1f2 --- /dev/null +++ b/common/tools/configtxlator/metadata/metadata.go @@ -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 = "configtxlator" + +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)) +} diff --git a/common/tools/configtxlator/metadata/metadata_test.go b/common/tools/configtxlator/metadata/metadata_test.go new file mode 100644 index 00000000000..337a19a8d3d --- /dev/null +++ b/common/tools/configtxlator/metadata/metadata_test.go @@ -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()) +}