github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/cmd/burrow/commands/helpers.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/signal"
     7  	"strconv"
     8  	"strings"
     9  	"syscall"
    10  
    11  	"github.com/hyperledger/burrow/config"
    12  	"github.com/hyperledger/burrow/config/source"
    13  	"github.com/hyperledger/burrow/genesis"
    14  	logging_config "github.com/hyperledger/burrow/logging/logconfig"
    15  )
    16  
    17  type Output interface {
    18  	Printf(format string, args ...interface{})
    19  	Logf(format string, args ...interface{})
    20  	Fatalf(format string, args ...interface{})
    21  }
    22  
    23  func obtainDefaultConfig(configFile, genesisDocFile string) (*config.BurrowConfig, error) {
    24  	// We need to reflect on whether this obscures where values are coming from
    25  	conf := config.DefaultBurrowConfig()
    26  	// We treat logging a little differently in that if anything is set for logging we will not
    27  	// set default outputs
    28  	conf.Logging = nil
    29  	err := source.EachOf(
    30  		burrowConfigProvider(configFile),
    31  		source.FirstOf(
    32  			genesisDocProvider(genesisDocFile, false),
    33  			// Try working directory
    34  			genesisDocProvider(config.DefaultGenesisDocJSONFileName, true)),
    35  	).Apply(conf)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	// If no logging config was provided use the default
    40  	if conf.Logging == nil {
    41  		conf.Logging = logging_config.DefaultNodeLoggingConfig()
    42  	}
    43  	return conf, nil
    44  }
    45  
    46  func burrowConfigProvider(configFile string) source.ConfigProvider {
    47  	return source.FirstOf(
    48  		// Will fail if file doesn't exist, but still skipped it configFile == ""
    49  		source.File(configFile, false),
    50  		source.Environment(config.DefaultBurrowConfigEnvironmentVariable),
    51  		// Try working directory
    52  		source.File(config.DefaultBurrowConfigTOMLFileName, true),
    53  		source.Default(config.DefaultBurrowConfig()))
    54  }
    55  
    56  func genesisDocProvider(genesisFile string, skipNonExistent bool) source.ConfigProvider {
    57  	return source.NewConfigProvider(fmt.Sprintf("genesis file at %s", genesisFile),
    58  		source.ShouldSkipFile(genesisFile, skipNonExistent),
    59  		func(baseConfig interface{}) error {
    60  			conf, ok := baseConfig.(*config.BurrowConfig)
    61  			if !ok {
    62  				return fmt.Errorf("config passed was not BurrowConfig")
    63  			}
    64  			if conf.GenesisDoc != nil {
    65  				return nil
    66  			}
    67  			genesisDoc := new(genesis.GenesisDoc)
    68  			err := source.FromFile(genesisFile, genesisDoc)
    69  			if err != nil {
    70  				return err
    71  			}
    72  			conf.GenesisDoc = genesisDoc
    73  			return nil
    74  		})
    75  }
    76  
    77  func parseRange(rangeString string) (start int64, end int64, err error) {
    78  	start = 0
    79  	end = -1
    80  
    81  	if rangeString == "" {
    82  		return
    83  	}
    84  
    85  	bounds := strings.Split(rangeString, ":")
    86  	if len(bounds) == 1 {
    87  		startString := bounds[0]
    88  		start, err = strconv.ParseInt(startString, 10, 64)
    89  		return
    90  	}
    91  	if len(bounds) == 2 {
    92  		if bounds[0] != "" {
    93  			start, err = strconv.ParseInt(bounds[0], 10, 64)
    94  			if err != nil {
    95  				return
    96  			}
    97  		}
    98  		if bounds[1] != "" {
    99  			end, err = strconv.ParseInt(bounds[1], 10, 64)
   100  		}
   101  		return
   102  	}
   103  	return 0, 0, fmt.Errorf("could not parse range from %s", rangeString)
   104  }
   105  
   106  func handleTerm() {
   107  	c := make(chan os.Signal)
   108  	signal.Notify(c, os.Interrupt, syscall.SIGTERM)
   109  	go func() {
   110  		<-c
   111  		os.Exit(1)
   112  	}()
   113  }