diff --git a/app/setup/genesis-hash.go b/app/setup/genesis-hash.go index a50fb9fa2..dc1b04cb0 100644 --- a/app/setup/genesis-hash.go +++ b/app/setup/genesis-hash.go @@ -97,7 +97,15 @@ func GenesisHashCmd() *cobra.Command { appHash = genCfg.StateHash } - return writeAndReturnGenesisHash(cmd, genesisFile, appHash) + if genesisFile != "" { + err := writeAndReturnGenesisHash(genesisFile, appHash) + if err != nil { + return display.PrintErr(cmd, err) + } + } + return display.PrintCmd(cmd, &genesisHashRes{ + Hash: base64.StdEncoding.EncodeToString(appHash), + }) }, } @@ -141,29 +149,20 @@ func appHashFromSnapshotFile(filePath string) ([]byte, error) { return hash.Sum(nil), nil } -func writeAndReturnGenesisHash(cmd *cobra.Command, genesisFile string, appHash []byte) error { - if genesisFile != "" { - genesisFile, err := node.ExpandPath(genesisFile) - if err != nil { - return display.PrintErr(cmd, err) - } - - cfg, err := config.LoadGenesisConfig(genesisFile) - if err != nil { - return display.PrintErr(cmd, err) - } - - cfg.StateHash = appHash +func writeAndReturnGenesisHash(genesisFile string, appHash []byte) error { + genesisFile, err := node.ExpandPath(genesisFile) + if err != nil { + return err + } - err = cfg.SaveAs(genesisFile) - if err != nil { - return display.PrintErr(cmd, err) - } + cfg, err := config.LoadGenesisConfig(genesisFile) + if err != nil { + return err } - return display.PrintCmd(cmd, &genesisHashRes{ - Hash: base64.StdEncoding.EncodeToString(appHash), - }) + cfg.StateHash = appHash + + return cfg.SaveAs(genesisFile) } type genesisHashRes struct { diff --git a/app/shared/display/format.go b/app/shared/display/format.go index c38703b5c..6c094d99e 100644 --- a/app/shared/display/format.go +++ b/app/shared/display/format.go @@ -183,7 +183,11 @@ func PrintCmd(cmd *cobra.Command, msg MsgFormatter) error { return prettyPrint(wrappedMsg, OutputFormat(format), cmd.OutOrStdout(), cmd.OutOrStderr()) } -// PrintErr prints the error according to the commands output format flag. +// PrintErr prints the error according to the commands output format flag. The +// returned error is nil if the message it was printed successfully. Thus, this +// function must ONLY be called from within a cobra.Command's RunE function or +// or returned directly by the RunE function, NOT used to direct application +// logic since the returned error no longer pertains to the initial error. func PrintErr(cmd *cobra.Command, err error) error { outputFormat, err2 := getOutputFormat(cmd) if err2 != nil {