utils.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2021 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 t8ntool
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "os"
  21. "github.com/urfave/cli/v2"
  22. )
  23. // readFile reads the json-data in the provided path and marshals into dest.
  24. func readFile(path, desc string, dest interface{}) error {
  25. inFile, err := os.Open(path)
  26. if err != nil {
  27. return NewError(ErrorIO, fmt.Errorf("failed reading %s file: %v", desc, err))
  28. }
  29. defer inFile.Close()
  30. decoder := json.NewDecoder(inFile)
  31. if err := decoder.Decode(dest); err != nil {
  32. return NewError(ErrorJson, fmt.Errorf("failed unmarshaling %s file: %v", desc, err))
  33. }
  34. return nil
  35. }
  36. // createBasedir makes sure the basedir exists, if user specified one.
  37. func createBasedir(ctx *cli.Context) (string, error) {
  38. baseDir := ""
  39. if ctx.IsSet(OutputBasedir.Name) {
  40. if base := ctx.String(OutputBasedir.Name); len(base) > 0 {
  41. err := os.MkdirAll(base, 0755) // //rw-r--r--
  42. if err != nil {
  43. return "", err
  44. }
  45. baseDir = base
  46. }
  47. }
  48. return baseDir, nil
  49. }