From d4adf7ab29b9e9454d3573f4f6f813689354b73f Mon Sep 17 00:00:00 2001 From: Gari Singh Date: Sat, 1 Jul 2017 05:49:57 -0400 Subject: [PATCH] [FAB-5141] configtxgen --version should not panic If configtxgen cannot find a configtx.yaml file, it panics. While this in and of itself is ok, it should not panic when simply trying to get the version. Two changes were made: - move response to --version above all other flags - added a defer / recover function to better handle config not found and make things more clear Change-Id: I512d1fed464c734db7178eb576440c45767f6994 Signed-off-by: Gari Singh --- common/configtx/tool/configtxgen/main.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/common/configtx/tool/configtxgen/main.go b/common/configtx/tool/configtxgen/main.go index d47110281e4..92985ff9229 100644 --- a/common/configtx/tool/configtxgen/main.go +++ b/common/configtx/tool/configtxgen/main.go @@ -23,6 +23,7 @@ import ( "fmt" "io/ioutil" "os" + "strings" "github.com/hyperledger/fabric/bccsp/factory" "github.com/hyperledger/fabric/common/config" @@ -347,17 +348,30 @@ func main() { flag.Parse() + // show version + if *version { + printVersion() + os.Exit(exitCode) + } + logging.SetLevel(logging.INFO, "") + // don't need to panic when running via command line + defer func() { + if err := recover(); err != nil { + if strings.Contains(fmt.Sprint(err), "Error reading configuration: Unsupported Config Type") { + logger.Error("Could not find configtx.yaml. " + + "Please make sure that FABRIC_CFG_PATH is set to a path " + + "which contains configtx.yaml") + } + os.Exit(1) + } + }() + logger.Info("Loading configuration") factory.InitFactories(nil) config := genesisconfig.Load(profile) - if *version { - printVersion() - os.Exit(exitCode) - } - if outputBlock != "" { if err := doOutputBlock(config, channelID, outputBlock); err != nil { logger.Fatalf("Error on outputBlock: %s", err)