feat: parse home flag earlier (#20771)

This commit is contained in:
Julien Robert
2024-06-25 18:53:57 +00:00
committed by GitHub
parent 524a84c3ba
commit 5aaff21096
22 changed files with 87 additions and 99 deletions
-20
View File
@@ -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.
+1
View 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
+36
View File
@@ -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
}