github.com/Finschia/finschia-sdk@v0.48.1/client/config/config.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/Finschia/finschia-sdk/client" 9 ) 10 11 // Default constants 12 const ( 13 chainID = "" 14 keyringBackend = "os" 15 output = "" 16 node = "tcp://localhost:26657" 17 broadcastMode = "sync" 18 ) 19 20 type ClientConfig struct { 21 ChainID string `mapstructure:"chain-id" json:"chain-id"` 22 KeyringBackend string `mapstructure:"keyring-backend" json:"keyring-backend"` 23 Output string `mapstructure:"output" json:"output"` 24 Node string `mapstructure:"node" json:"node"` 25 BroadcastMode string `mapstructure:"broadcast-mode" json:"broadcast-mode"` 26 } 27 28 // defaultClientConfig returns the reference to ClientConfig with default values. 29 func defaultClientConfig() *ClientConfig { 30 return &ClientConfig{chainID, keyringBackend, output, node, broadcastMode} 31 } 32 33 func (c *ClientConfig) SetChainID(chainID string) { 34 c.ChainID = chainID 35 } 36 37 func (c *ClientConfig) SetKeyringBackend(keyringBackend string) { 38 c.KeyringBackend = keyringBackend 39 } 40 41 func (c *ClientConfig) SetOutput(output string) { 42 c.Output = output 43 } 44 45 func (c *ClientConfig) SetNode(node string) { 46 c.Node = node 47 } 48 49 func (c *ClientConfig) SetBroadcastMode(broadcastMode string) { 50 c.BroadcastMode = broadcastMode 51 } 52 53 // ReadFromClientConfig reads values from client.toml file and updates them in client Context 54 func ReadFromClientConfig(ctx client.Context) (client.Context, error) { 55 configPath := filepath.Join(ctx.HomeDir, "config") 56 configFilePath := filepath.Join(configPath, "client.toml") 57 conf := defaultClientConfig() 58 59 // if config.toml file does not exist we create it and write default ClientConfig values into it. 60 if _, err := os.Stat(configFilePath); os.IsNotExist(err) { 61 if err := ensureConfigPath(configPath); err != nil { 62 return ctx, fmt.Errorf("couldn't make client config: %v", err) 63 } 64 65 if err := writeConfigToFile(configFilePath, conf); err != nil { 66 return ctx, fmt.Errorf("could not write client config to the file: %v", err) 67 } 68 } 69 70 conf, err := getClientConfig(configPath, ctx.Viper) 71 if err != nil { 72 return ctx, fmt.Errorf("couldn't get client config: %v", err) 73 } 74 // we need to update KeyringDir field on Client Context first cause it is used in NewKeyringFromBackend 75 ctx = ctx.WithOutputFormat(conf.Output). 76 WithChainID(conf.ChainID). 77 WithKeyringDir(ctx.HomeDir) 78 79 keyring, err := client.NewKeyringFromBackend(ctx, conf.KeyringBackend) 80 if err != nil { 81 return ctx, fmt.Errorf("couldn't get key ring: %v", err) 82 } 83 84 ctx = ctx.WithKeyring(keyring) 85 86 // https://github.com/cosmos/cosmos-sdk/issues/8986 87 client, err := client.NewClientFromNode(conf.Node) 88 if err != nil { 89 return ctx, fmt.Errorf("couldn't get client from nodeURI: %v", err) 90 } 91 92 ctx = ctx.WithNodeURI(conf.Node). 93 WithClient(client). 94 WithBroadcastMode(conf.BroadcastMode) 95 96 return ctx, nil 97 }