github.com/amazechain/amc@v0.1.3/conf/config.go (about)

     1  // Copyright 2022 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The AmazeChain library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package conf
    18  
    19  import (
    20  	"bufio"
    21  	"fmt"
    22  	"github.com/amazechain/amc/params"
    23  	"os"
    24  
    25  	"gopkg.in/yaml.v2"
    26  )
    27  
    28  type Config struct {
    29  	NodeCfg     NodeConfig          `json:"node" yaml:"node"`
    30  	NetworkCfg  NetWorkConfig       `json:"network" yaml:"network"`
    31  	LoggerCfg   LoggerConfig        `json:"logger" yaml:"logger"`
    32  	DatabaseCfg DatabaseConfig      `json:"database" yaml:"database"`
    33  	PprofCfg    PprofConfig         `json:"pprof" yaml:"pprof"`
    34  	ChainCfg    *params.ChainConfig `json:"chain" yaml:"chain"`
    35  	AccountCfg  AccountConfig       `json:"account" yaml:"account"`
    36  	MetricsCfg  MetricsConfig       `json:"metrics" yaml:"metrics"`
    37  	P2PCfg      *P2PConfig          `json:"p2p" yaml:"p2p"`
    38  	// Gas Price Oracle options
    39  	GPO   GpoConfig   `json:"gpo" yaml:"gpo"`
    40  	Miner MinerConfig `json:"miner"`
    41  }
    42  
    43  func SaveConfigToFile(file string, config Config) error {
    44  	if len(file) == 0 {
    45  		file = "./config2.yaml"
    46  	}
    47  
    48  	fd, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
    49  	if err != nil {
    50  		//log.Errorf("filed open file %v, err %v", file, err)
    51  		return err
    52  	}
    53  	defer fd.Close()
    54  	return yaml.NewEncoder(fd).Encode(config)
    55  	//return toml.NewEncoder(fd).Encode(blockchain)
    56  }
    57  
    58  func LoadConfigFromFile(file string, config *Config) error {
    59  	if len(file) <= 0 {
    60  		return fmt.Errorf("failed to load blockchain from file, file is nil")
    61  	}
    62  	//_, err := toml.DecodeFile(file, blockchain)
    63  	fd, err := os.Open(file)
    64  	if err != nil {
    65  		return err
    66  	}
    67  	defer fd.Close()
    68  	reader := bufio.NewReader(fd)
    69  	//return toml.NewDecoder(reader).Decode(blockchain)
    70  	return yaml.NewDecoder(reader).Decode(config)
    71  }