Skip to content

Commit b593425

Browse files
committed
[FAB-4352] Add version cmd to configtxlator
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 <[email protected]>
1 parent 88d4845 commit b593425

File tree

4 files changed

+94
-8
lines changed

4 files changed

+94
-8
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,13 @@ orderer-docker: build/image/orderer/$(DUMMY)
129129

130130
.PHONY: configtxgen
131131
configtxgen: GO_TAGS+= nopkcs11
132+
configtxgen: GO_LDFLAGS=-X $(pkgmap.$(@F))/metadata.Version=$(PROJECT_VERSION)
132133
configtxgen: build/bin/configtxgen
133134

135+
configtxlator: GO_LDFLAGS=-X $(pkgmap.$(@F))/metadata.Version=$(PROJECT_VERSION)
134136
configtxlator: build/bin/configtxlator
135137

138+
cryptogen: GO_LDFLAGS=-X $(pkgmap.$(@F))/metadata.Version=$(PROJECT_VERSION)
136139
cryptogen: build/bin/cryptogen
137140

138141
tools-docker: build/image/tools/$(DUMMY)

common/tools/configtxlator/main.go

+33-8
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,50 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"flag"
2120
"fmt"
2221
"net/http"
22+
"os"
2323

24+
"github.com/hyperledger/fabric/common/tools/configtxlator/metadata"
2425
"github.com/hyperledger/fabric/common/tools/configtxlator/rest"
25-
2626
"github.com/op/go-logging"
27+
"gopkg.in/alecthomas/kingpin.v2"
2728
)
2829

2930
var logger = logging.MustGetLogger("configtxlator")
3031

32+
// command line flags
33+
var (
34+
app = kingpin.New("configtxlator", "Utility for generating Hyperledger Fabric channel configurations")
35+
36+
start = app.Command("start", "Start the configtxlator REST server")
37+
hostname = start.Flag("hostname", "The hostname or IP on which the REST server will listen").Default("0.0.0.0").String()
38+
port = start.Flag("port", "The port on which the REST server will listen").Default("7059").Int()
39+
40+
version = app.Command("version", "Show version information")
41+
)
42+
3143
func main() {
32-
var serverPort int
44+
kingpin.Version("0.0.1")
45+
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
46+
// "start" command
47+
case start.FullCommand():
48+
startServer(fmt.Sprintf("%s:%d", *hostname, *port))
3349

34-
flag.IntVar(&serverPort, "serverPort", 7059, "Specify the port for the REST server to listen on.")
35-
flag.Parse()
50+
// "version" command
51+
case version.FullCommand():
52+
printVersion()
53+
}
3654

37-
logger.Infof("Serving HTTP requests on port: %d", serverPort)
38-
err := http.ListenAndServe(fmt.Sprintf(":%d", serverPort), rest.NewRouter())
55+
}
56+
57+
func startServer(address string) {
58+
logger.Infof("Serving HTTP requests on %s", address)
59+
err := http.ListenAndServe(address, rest.NewRouter())
60+
61+
app.Fatalf("Error starting server:[%s]\n", err)
62+
}
3963

40-
logger.Fatal("Error runing http server:", err)
64+
func printVersion() {
65+
fmt.Println(metadata.GetVersionInfo())
4166
}
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 = "configtxlator"
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)