feat: parse home flag earlier (#20771)
This commit is contained in:
@@ -53,26 +53,6 @@ func ReadFromClientConfig(ctx client.Context) (client.Context, error) {
|
||||
return CreateClientConfig(ctx, "", nil)
|
||||
}
|
||||
|
||||
// ReadDefaultValuesFromDefaultClientConfig reads default values from default client.toml file and updates them in client.Context
|
||||
// The client.toml is then discarded.
|
||||
func ReadDefaultValuesFromDefaultClientConfig(ctx client.Context, customClientTemplate string, customConfig interface{}) (client.Context, error) {
|
||||
prevHomeDir := ctx.HomeDir
|
||||
dir, err := os.MkdirTemp("", "simapp")
|
||||
if err != nil {
|
||||
return ctx, fmt.Errorf("couldn't create temp dir: %w", err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
ctx.HomeDir = dir
|
||||
ctx, err = CreateClientConfig(ctx, customClientTemplate, customConfig)
|
||||
if err != nil {
|
||||
return ctx, fmt.Errorf("couldn't create client config: %w", err)
|
||||
}
|
||||
|
||||
ctx.HomeDir = prevHomeDir
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
// CreateClientConfig reads the client.toml file and returns a new populated client.Context
|
||||
// If the client.toml file does not exist, it creates one with default values.
|
||||
// It takes a customClientTemplate and customConfig as input that can be used to overwrite the default config and enhance the client.toml file.
|
||||
|
||||
@@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
|
||||
|
||||
* [#18626](https://github.com/cosmos/cosmos-sdk/pull/18626) Support for off-chain signing and verification of a file.
|
||||
* [#18461](https://github.com/cosmos/cosmos-sdk/pull/18461) Support governance proposals.
|
||||
* [#20771](https://github.com/cosmos/cosmos-sdk/pull/20771) Add `GetNodeHomeDirectory` helper.
|
||||
|
||||
### API Breaking Changes
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetNodeHomeDirectory gets the home directory of the node (where the config is located).
|
||||
// It parses the home flag if set if the `NODE_HOME` environment variable if set (and ignores name).
|
||||
// Otherwise, it returns the default home directory given its name.
|
||||
func GetNodeHomeDirectory(name string) (string, error) {
|
||||
// get the home directory from the flag
|
||||
args := os.Args
|
||||
for i := 0; i < len(args); i++ {
|
||||
if args[i] == "--home" && i+1 < len(args) {
|
||||
return filepath.Clean(args[i+1]), nil
|
||||
} else if strings.HasPrefix(args[i], "--home=") {
|
||||
return filepath.Clean(args[i][7:]), nil
|
||||
}
|
||||
}
|
||||
|
||||
// get the home directory from the environment variable
|
||||
homeDir := os.Getenv("NODE_HOME")
|
||||
if homeDir != "" {
|
||||
return filepath.Clean(homeDir), nil
|
||||
}
|
||||
|
||||
// return the default home directory
|
||||
userHomeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filepath.Join(userHomeDir, name), nil
|
||||
}
|
||||
Reference in New Issue
Block a user