github.com/jimmyx0x/go-ethereum@v1.10.28/cmd/evm/internal/t8ntool/utils.go (about)

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