Skip to content

Commit

Permalink
[FAB-4352] Add version cmd to configtxlator
Browse files Browse the repository at this point in the history
Adds a version cmd to configtxlator.
Modified configxtxlator to use kingpin
as the CLI library as that's what is
used in cryptogen as well.

Version info is added to the binary
using both
make configtxlator
make release[-all]

(Note: this fixes the issue with
make cryptogen not adding the version
as well)

Change-Id: I6bc52f12a09b4ea60dcab2693205cf151fb1b681
Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
  • Loading branch information
mastersingh24 committed Jun 6, 2017
1 parent 88d4845 commit b593425
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 33 additions & 8 deletions common/tools/configtxlator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
32 changes: 32 additions & 0 deletions common/tools/configtxlator/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 = "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))
}
26 changes: 26 additions & 0 deletions common/tools/configtxlator/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 b593425

Please sign in to comment.