github.com/cosmos/cosmos-sdk@v0.50.10/client/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/cosmos/cosmos-sdk/client"
     9  )
    10  
    11  func DefaultConfig() *ClientConfig {
    12  	return &ClientConfig{
    13  		ChainID:        "",
    14  		KeyringBackend: "os",
    15  		Output:         "text",
    16  		Node:           "tcp://localhost:26657",
    17  		BroadcastMode:  "sync",
    18  	}
    19  }
    20  
    21  type ClientConfig struct {
    22  	ChainID        string `mapstructure:"chain-id" json:"chain-id"`
    23  	KeyringBackend string `mapstructure:"keyring-backend" json:"keyring-backend"`
    24  	Output         string `mapstructure:"output" json:"output"`
    25  	Node           string `mapstructure:"node" json:"node"`
    26  	BroadcastMode  string `mapstructure:"broadcast-mode" json:"broadcast-mode"`
    27  }
    28  
    29  func (c *ClientConfig) SetChainID(chainID string) {
    30  	c.ChainID = chainID
    31  }
    32  
    33  func (c *ClientConfig) SetKeyringBackend(keyringBackend string) {
    34  	c.KeyringBackend = keyringBackend
    35  }
    36  
    37  func (c *ClientConfig) SetOutput(output string) {
    38  	c.Output = output
    39  }
    40  
    41  func (c *ClientConfig) SetNode(node string) {
    42  	c.Node = node
    43  }
    44  
    45  func (c *ClientConfig) SetBroadcastMode(broadcastMode string) {
    46  	c.BroadcastMode = broadcastMode
    47  }
    48  
    49  // ReadDefaultValuesFromDefaultClientConfig reads default values from default client.toml file and updates them in client.Context
    50  // The client.toml is then discarded.
    51  func ReadDefaultValuesFromDefaultClientConfig(ctx client.Context) (client.Context, error) {
    52  	prevHomeDir := ctx.HomeDir
    53  	dir, err := os.MkdirTemp("", "simapp")
    54  	if err != nil {
    55  		return ctx, fmt.Errorf("couldn't create temp dir: %w", err)
    56  	}
    57  	defer os.RemoveAll(dir)
    58  
    59  	ctx.HomeDir = dir
    60  	ctx, err = ReadFromClientConfig(ctx)
    61  	if err != nil {
    62  		return ctx, fmt.Errorf("couldn't create client config: %w", err)
    63  	}
    64  
    65  	ctx.HomeDir = prevHomeDir
    66  	return ctx, nil
    67  }
    68  
    69  // ReadFromClientConfig reads values from client.toml file and updates them in client Context
    70  func ReadFromClientConfig(ctx client.Context) (client.Context, error) {
    71  	configPath := filepath.Join(ctx.HomeDir, "config")
    72  	configFilePath := filepath.Join(configPath, "client.toml")
    73  	conf := DefaultConfig()
    74  
    75  	// when client.toml does not exist create and init with default values
    76  	if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
    77  		if err := os.MkdirAll(configPath, os.ModePerm); err != nil {
    78  			return ctx, fmt.Errorf("couldn't make client config: %v", err)
    79  		}
    80  
    81  		if ctx.ChainID != "" {
    82  			conf.ChainID = ctx.ChainID // chain-id will be written to the client.toml while initiating the chain.
    83  		}
    84  
    85  		if err := writeConfigToFile(configFilePath, conf); err != nil {
    86  			return ctx, fmt.Errorf("could not write client config to the file: %v", err)
    87  		}
    88  	}
    89  
    90  	conf, err := getClientConfig(configPath, ctx.Viper)
    91  	if err != nil {
    92  		return ctx, fmt.Errorf("couldn't get client config: %v", err)
    93  	}
    94  	// we need to update KeyringDir field on Client Context first cause it is used in NewKeyringFromBackend
    95  	ctx = ctx.WithOutputFormat(conf.Output).
    96  		WithChainID(conf.ChainID).
    97  		WithKeyringDir(ctx.HomeDir)
    98  
    99  	keyring, err := client.NewKeyringFromBackend(ctx, conf.KeyringBackend)
   100  	if err != nil {
   101  		return ctx, fmt.Errorf("couldn't get keyring: %w", err)
   102  	}
   103  
   104  	ctx = ctx.WithKeyring(keyring)
   105  
   106  	// https://github.com/cosmos/cosmos-sdk/issues/8986
   107  	client, err := client.NewClientFromNode(conf.Node)
   108  	if err != nil {
   109  		return ctx, fmt.Errorf("couldn't get client from nodeURI: %v", err)
   110  	}
   111  
   112  	ctx = ctx.WithNodeURI(conf.Node).
   113  		WithClient(client).
   114  		WithBroadcastMode(conf.BroadcastMode)
   115  
   116  	return ctx, nil
   117  }