utils.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "os"
  21. "strings"
  22. "github.com/ethereum/go-ethereum/cmd/utils"
  23. "github.com/urfave/cli/v2"
  24. )
  25. // getPassphrase obtains a passphrase given by the user. It first checks the
  26. // --passfile command line flag and ultimately prompts the user for a
  27. // passphrase.
  28. func getPassphrase(ctx *cli.Context, confirmation bool) string {
  29. // Look for the --passwordfile flag.
  30. passphraseFile := ctx.String(passphraseFlag.Name)
  31. if passphraseFile != "" {
  32. content, err := os.ReadFile(passphraseFile)
  33. if err != nil {
  34. utils.Fatalf("Failed to read password file '%s': %v",
  35. passphraseFile, err)
  36. }
  37. return strings.TrimRight(string(content), "\r\n")
  38. }
  39. // Otherwise prompt the user for the passphrase.
  40. return utils.GetPassPhrase("", confirmation)
  41. }
  42. // mustPrintJSON prints the JSON encoding of the given object and
  43. // exits the program with an error message when the marshaling fails.
  44. func mustPrintJSON(jsonObject interface{}) {
  45. str, err := json.MarshalIndent(jsonObject, "", " ")
  46. if err != nil {
  47. utils.Fatalf("Failed to marshal JSON object: %v", err)
  48. }
  49. fmt.Println(string(str))
  50. }