Skip to content

Commit

Permalink
use closure for file.close
Browse files Browse the repository at this point in the history
  • Loading branch information
leejw51crypto committed Oct 22, 2020
1 parent 51de149 commit 19b0c33
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
15 changes: 8 additions & 7 deletions cmd/chain-maind/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,28 +152,29 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
return err
}

defer func() {
cerr := file.Close()
if err == nil {
err = cerr
}
}()

var genesis map[string]interface{}
if err := json.NewDecoder(file).Decode(&genesis); err != nil {
file.Close()
return err
}

if err := mergo.Merge(&genesis, genesisPatch, mergo.WithOverride); err != nil {
file.Close()
return err
}
if err := file.Truncate(0); err != nil {
file.Close()
return err
}
if _, err := file.Seek(0, 0); err != nil {
file.Close()
return err
}

ret := json.NewEncoder(file).Encode(&genesis)
file.Close()
return ret
return json.NewEncoder(file).Encode(&genesis)
}

rootCmd.AddCommand(
Expand Down
9 changes: 7 additions & 2 deletions x/genutil/client/cli/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,18 @@ func writeSignedGenTx(clientCtx client.Context, outputDocument string, tx sdk.Tx
return err
}

defer func() {
cerr := outputFile.Close()
if err == nil {
err = cerr
}
}()

json, err := clientCtx.TxConfig.TxJSONEncoder()(tx)
if err != nil {
outputFile.Close()
return err
}

_, err = fmt.Fprintf(outputFile, "%s\n", json)
outputFile.Close()
return err
}

0 comments on commit 19b0c33

Please sign in to comment.