github.com/iotexproject/iotex-core@v1.14.1-rc1/tools/bot/config/config.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package config
     7  
     8  import (
     9  	"flag"
    10  	"os"
    11  
    12  	"github.com/pkg/errors"
    13  	uconfig "go.uber.org/config"
    14  
    15  	"github.com/iotexproject/iotex-core/pkg/log"
    16  )
    17  
    18  func init() {
    19  	flag.StringVar(&_overwritePath, "config-path", "config.yaml", "Config path")
    20  }
    21  
    22  var (
    23  	// overwritePath is the path to the config file which overwrite default values
    24  	_overwritePath string
    25  )
    26  
    27  var (
    28  	// Default is the default config
    29  	Default = Config{
    30  		API: API{
    31  			URL: "api.testnet.iotex.one:80",
    32  		},
    33  	}
    34  )
    35  
    36  type (
    37  	// API is the api service config
    38  	API struct {
    39  		URL string `yaml:"url"`
    40  	}
    41  	// Config is the root config struct, each package's config should be put as its sub struct
    42  	Config struct {
    43  		API            API                         `yaml:"api"`
    44  		Log            log.GlobalConfig            `yaml:"log"`
    45  		SubLogs        map[string]log.GlobalConfig `yaml:"subLogs"`
    46  		RunInterval    uint64                      `yaml:"runInterval"`
    47  		GasLimit       uint64                      `yaml:"gaslimit"`
    48  		GasPrice       uint64                      `yaml:"gasprice"`
    49  		AlertThreshold uint64                      `yaml:"alertThreshold"`
    50  		Transfer       transfer                    `yaml:"transfer"`
    51  		Xrc20          xrc20                       `yaml:"xrc20"`
    52  		Execution      execution                   `yaml:"execution"`
    53  	}
    54  	transfer struct {
    55  		Signer      string `yaml:"signer"`
    56  		AmountInRau string `yaml:"amountInRau"`
    57  	}
    58  	xrc20 struct {
    59  		Contract string `yaml:"contract"`
    60  		Signer   string `yaml:"signer"`
    61  		Amount   string `yaml:"amount"` // amount in smallest unit
    62  	}
    63  	execution struct {
    64  		Contract string      `yaml:"contract"`
    65  		Signer   string      `yaml:"signer"`
    66  		Amount   string      `yaml:"amount"` // amount in smallest unit
    67  		To       multiSendTo `yaml:"to"`
    68  	}
    69  	multiSendTo struct {
    70  		Address []string `yaml:"address"`
    71  		Amount  []string `yaml:"amount"`
    72  	}
    73  )
    74  
    75  // New create config
    76  func New() (Config, error) {
    77  	opts := make([]uconfig.YAMLOption, 0)
    78  	opts = append(opts, uconfig.Static(Default))
    79  	opts = append(opts, uconfig.Expand(os.LookupEnv))
    80  	if _overwritePath != "" {
    81  		opts = append(opts, uconfig.File(_overwritePath))
    82  	}
    83  
    84  	yaml, err := uconfig.NewYAML(opts...)
    85  	if err != nil {
    86  		return Config{}, errors.Wrap(err, "failed to init config")
    87  	}
    88  
    89  	var cfg Config
    90  	if err := yaml.Get(uconfig.Root).Populate(&cfg); err != nil {
    91  		return Config{}, errors.Wrap(err, "failed to unmarshal YAML config to struct")
    92  	}
    93  
    94  	return cfg, nil
    95  }