Skip to content

Commit 5aaff21

Browse files
authored
feat: parse home flag earlier (#20771)
1 parent 524a84c commit 5aaff21

22 files changed

Lines changed: 87 additions & 99 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
102102
* (proto) [#20098](https://github.com/cosmos/cosmos-sdk/pull/20098) Use cosmos_proto added_in annotation instead of // Since comments.
103103
* (baseapp) [#20208](https://github.com/cosmos/cosmos-sdk/pull/20208) Skip running validateBasic for rechecking txs.
104104
* (baseapp) [#20380](https://github.com/cosmos/cosmos-sdk/pull/20380) Enhanced OfferSnapshot documentation.
105+
* (client) [#20771](https://github.com/cosmos/cosmos-sdk/pull/20771) Remove `ReadDefaultValuesFromDefaultClientConfig` from `client` package. (It was introduced in `v0.50.6` as a quick fix).
105106

106107
### Bug Fixes
107108

client/config/config.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,6 @@ func ReadFromClientConfig(ctx client.Context) (client.Context, error) {
5353
return CreateClientConfig(ctx, "", nil)
5454
}
5555

56-
// ReadDefaultValuesFromDefaultClientConfig reads default values from default client.toml file and updates them in client.Context
57-
// The client.toml is then discarded.
58-
func ReadDefaultValuesFromDefaultClientConfig(ctx client.Context, customClientTemplate string, customConfig interface{}) (client.Context, error) {
59-
prevHomeDir := ctx.HomeDir
60-
dir, err := os.MkdirTemp("", "simapp")
61-
if err != nil {
62-
return ctx, fmt.Errorf("couldn't create temp dir: %w", err)
63-
}
64-
defer os.RemoveAll(dir)
65-
66-
ctx.HomeDir = dir
67-
ctx, err = CreateClientConfig(ctx, customClientTemplate, customConfig)
68-
if err != nil {
69-
return ctx, fmt.Errorf("couldn't create client config: %w", err)
70-
}
71-
72-
ctx.HomeDir = prevHomeDir
73-
return ctx, nil
74-
}
75-
7656
// CreateClientConfig reads the client.toml file and returns a new populated client.Context
7757
// If the client.toml file does not exist, it creates one with default values.
7858
// It takes a customClientTemplate and customConfig as input that can be used to overwrite the default config and enhance the client.toml file.

client/v2/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4242

4343
* [#18626](https://github.com/cosmos/cosmos-sdk/pull/18626) Support for off-chain signing and verification of a file.
4444
* [#18461](https://github.com/cosmos/cosmos-sdk/pull/18461) Support governance proposals.
45+
* [#20771](https://github.com/cosmos/cosmos-sdk/pull/20771) Add `GetNodeHomeDirectory` helper.
4546

4647
### API Breaking Changes
4748

client/v2/helpers/home.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package helpers
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
)
8+
9+
// GetNodeHomeDirectory gets the home directory of the node (where the config is located).
10+
// It parses the home flag if set if the `NODE_HOME` environment variable if set (and ignores name).
11+
// Otherwise, it returns the default home directory given its name.
12+
func GetNodeHomeDirectory(name string) (string, error) {
13+
// get the home directory from the flag
14+
args := os.Args
15+
for i := 0; i < len(args); i++ {
16+
if args[i] == "--home" && i+1 < len(args) {
17+
return filepath.Clean(args[i+1]), nil
18+
} else if strings.HasPrefix(args[i], "--home=") {
19+
return filepath.Clean(args[i][7:]), nil
20+
}
21+
}
22+
23+
// get the home directory from the environment variable
24+
homeDir := os.Getenv("NODE_HOME")
25+
if homeDir != "" {
26+
return filepath.Clean(homeDir), nil
27+
}
28+
29+
// return the default home directory
30+
userHomeDir, err := os.UserHomeDir()
31+
if err != nil {
32+
return "", err
33+
}
34+
35+
return filepath.Join(userHomeDir, name), nil
36+
}

docs/learn/advanced/07-cli.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Flags are added to commands directly (generally in the [module's CLI file](../..
172172

173173
## Environment variables
174174

175-
Each flag is bound to its respective named environment variable. Then name of the environment variable consist of two parts - capital case `basename` followed by flag name of the flag. `-` must be substituted with `_`. For example flag `--home` for application with basename `GAIA` is bound to `GAIA_HOME`. It allows reducing the amount of flags typed for routine operations. For example instead of:
175+
Each flag is bound to its respective named environment variable. Then name of the environment variable consist of two parts - capital case `basename` followed by flag name of the flag. `-` must be substituted with `_`. For example flag `--node` for application with basename `GAIA` is bound to `GAIA_NODE`. It allows reducing the amount of flags typed for routine operations. For example instead of:
176176

177177
```shell
178178
gaia --home=./ --node=<node address> --chain-id="testchain-1" --keyring-backend=test tx ... --from=<key name>
@@ -182,7 +182,7 @@ this will be more convenient:
182182

183183
```shell
184184
# define env variables in .env, .envrc etc
185-
GAIA_HOME=<path to home>
185+
NODE_HOME=<path to home>
186186
GAIA_NODE=<node address>
187187
GAIA_CHAIN_ID="testchain-1"
188188
GAIA_KEYRING_BACKEND="test"

scripts/init-simapp.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ SIMD_BIN=${SIMD_BIN:=$(which simd 2>/dev/null)}
44

55
if [ -z "$SIMD_BIN" ]; then echo "SIMD_BIN is not set. Make sure to run make install before"; exit 1; fi
66
echo "using $SIMD_BIN"
7-
if [ -d "$($SIMD_BIN config home)" ]; then rm -r $($SIMD_BIN config home); fi
7+
if [ -d "$($SIMD_BIN config home)" ]; then rm -rv $($SIMD_BIN config home); fi
88
$SIMD_BIN config set client chain-id demo
99
$SIMD_BIN config set client keyring-backend test
1010
$SIMD_BIN config set client keyring-default-keyname alice

scripts/simapp-v2-init.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ CONFIG="${CONFIG:-$HOME/.simappv2/config}"
1111

1212
COSMOS_BUILD_OPTIONS=v2 make build
1313

14-
if [ -d "$($SIMD config home)" ]; then rm -r $($SIMD config home); fi
14+
if [ -d "$($SIMD config home)" ]; then rm -rv $($SIMD config home); fi
1515

1616
$SIMD init simapp-v2-node --chain-id simapp-v2-chain
1717

@@ -26,6 +26,7 @@ jq '.app_state.mint.minter.inflation = "0.300000000000000000"' genesis.json > te
2626
# change the initial height to 2 to work around store/v2 and iavl limitations with a genesis block
2727
jq '.initial_height = 2' genesis.json > temp.json && mv temp.json genesis.json
2828

29+
$SIMD config set client chain-id simapp-v2-chain
2930
$SIMD keys add test_validator --indiscreet
3031
VALIDATOR_ADDRESS=$($SIMD keys show test_validator -a --keyring-backend test)
3132

server/v2/cometbft/flags/flags.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ const (
99
)
1010

1111
const (
12-
FlagHome = "home"
13-
FlagKeyringDir = "keyring-dir"
14-
FlagUseLedger = "ledger"
1512
FlagChainID = "chain-id"
1613
FlagNode = "node"
1714
FlagGRPC = "grpc-addr"

server/v2/commands.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ func AddCommands(rootCmd *cobra.Command, newApp AppCreator[transaction.Tx], logg
109109

110110
// configHandle writes the default config to the home directory if it does not exist and sets the server context
111111
func configHandle(s *Server, home string, cmd *cobra.Command) error {
112-
if _, err := os.Stat(filepath.Join(home, "config")); os.IsNotExist(err) {
112+
// we need to check app.toml as the config folder can already exist for the client.toml
113+
if _, err := os.Stat(filepath.Join(home, "config", "app.toml")); os.IsNotExist(err) {
113114
if err = s.WriteConfig(filepath.Join(home, "config")); err != nil {
114115
return err
115116
}

simapp/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Always refer to the [UPGRADING.md](https://github.com/cosmos/cosmos-sdk/blob/mai
3535
* [#20409](https://github.com/cosmos/cosmos-sdk/pull/20409) Add `tx` as `SkipStoreKeys` in `app_config.go`.
3636
* [#20485](https://github.com/cosmos/cosmos-sdk/pull/20485) The signature of `x/upgrade/types.UpgradeHandler` has changed to accept `appmodule.VersionMap` from `module.VersionMap`. These types are interchangeable, but usages of `UpradeKeeper.SetUpgradeHandler` may need to adjust their usages to match the new signature.
3737
* [#20740](https://github.com/cosmos/cosmos-sdk/pull/20740) Update `genutilcli.Commands` to use the genutil modules from the module manager.
38+
* [#20771](https://github.com/cosmos/cosmos-sdk/pull/20771) Use client/v2 `GetNodeHomeDirectory` helper in `app.go` and use the `DefaultNodeHome` constant everywhere in the app.
3839

3940
<!-- TODO: move changelog.md elements to here -->
4041

0 commit comments

Comments
 (0)