Skip to content

Commit

Permalink
fix more misuse of display.PrintErr
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed Jan 24, 2025
1 parent 24bfbb3 commit cfcc680
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
41 changes: 20 additions & 21 deletions app/setup/genesis-hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
},
}

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion app/shared/display/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cfcc680

Please sign in to comment.