github.com/Finschia/finschia-sdk@v0.48.1/client/config/toml.go (about)

     1  package config
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"text/template"
     7  
     8  	"github.com/spf13/viper"
     9  )
    10  
    11  const defaultConfigTemplate = `# This is a TOML config file.
    12  # For more information, see https://github.com/toml-lang/toml
    13  
    14  ###############################################################################
    15  ###                           Client Configuration                            ###
    16  ###############################################################################
    17  
    18  # The network chain ID
    19  chain-id = "{{ .ChainID }}"
    20  # The keyring's backend, where the keys are stored (os|file|kwallet|pass|test|memory)
    21  keyring-backend = "{{ .KeyringBackend }}"
    22  # CLI output format (text|json), it will override the default values of both query and tx
    23  output = "{{ .Output }}"
    24  # <host>:<port> to Tendermint RPC interface for this chain
    25  node = "{{ .Node }}"
    26  # Transaction broadcasting mode (sync|async|block)
    27  broadcast-mode = "{{ .BroadcastMode }}"
    28  `
    29  
    30  // writeConfigToFile parses defaultConfigTemplate, renders config using the template and writes it to
    31  // configFilePath.
    32  func writeConfigToFile(configFilePath string, config *ClientConfig) error {
    33  	var buffer bytes.Buffer
    34  
    35  	tmpl := template.New("clientConfigFileTemplate")
    36  	configTemplate, err := tmpl.Parse(defaultConfigTemplate)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	if err := configTemplate.Execute(&buffer, config); err != nil {
    42  		return err
    43  	}
    44  
    45  	return os.WriteFile(configFilePath, buffer.Bytes(), 0o600)
    46  }
    47  
    48  // ensureConfigPath creates a directory configPath if it does not exist
    49  func ensureConfigPath(configPath string) error {
    50  	return os.MkdirAll(configPath, os.ModePerm)
    51  }
    52  
    53  // getClientConfig reads values from client.toml file and unmarshalls them into ClientConfig
    54  func getClientConfig(configPath string, v *viper.Viper) (*ClientConfig, error) {
    55  	v.AddConfigPath(configPath)
    56  	v.SetConfigName("client")
    57  	v.SetConfigType("toml")
    58  
    59  	if err := v.ReadInConfig(); err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	conf := new(ClientConfig)
    64  	if err := v.Unmarshal(conf); err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	return conf, nil
    69  }