Skip to content

Commit e4c342e

Browse files
feat: parse home flag earlier (backport #20771) (#20777)
Co-authored-by: Julien Robert <julien@rbrt.fr>
1 parent 0e6db14 commit e4c342e

File tree

18 files changed

+208
-196
lines changed

18 files changed

+208
-196
lines changed

client/v2/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
3636

3737
## [Unreleased]
3838

39+
### Features
40+
41+
* [#20771](https://github.com/cosmos/cosmos-sdk/pull/20771) Add `GetNodeHomeDirectory` helper in `client/v2/helpers`.
42+
3943
## [v2.0.0-beta.2] - 2024-06-19
4044

4145
### Features
@@ -50,7 +54,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
5054
* [#20083](https://github.com/cosmos/cosmos-sdk/pull/20083) Integrate latest version of cosmos-proto and improve version filtering.
5155
* [#19618](https://github.com/cosmos/cosmos-sdk/pull/19618) Marshal enum as string in queries.
5256
* [#19060](https://github.com/cosmos/cosmos-sdk/pull/19060) Use client context from root (or enhanced) command in autocli commands.
53-
* Note, the given command must have a `client.Context` in its context.
57+
* Note, the given command must have a `client.Context` in its context.
5458
* [#19216](https://github.com/cosmos/cosmos-sdk/pull/19216) Do not overwrite TxConfig, use directly the one provided in context. TxConfig should always be set in the `client.Context` in `root.go` of an app.
5559
* [#20266](https://github.com/cosmos/cosmos-sdk/pull/20266) Add ability to override the short description in AutoCLI-generated top-level commands.
5660

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/docs/learn/advanced/07-cli.md

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

154154
## Environment variables
155155

156-
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:
156+
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:
157157

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

164164
```shell
165165
# define env variables in .env, .envrc etc
166-
GAIA_HOME=<path to home>
166+
NODE_HOME=<path to home>
167167
GAIA_NODE=<node address>
168168
GAIA_CHAIN_ID="testchain-1"
169169
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 app api.enable true

simapp/app.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"fmt"
88
"io"
99
"os"
10-
"path/filepath"
1110

1211
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
1312
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
1413
"cosmossdk.io/client/v2/autocli"
14+
clienthelpers "cosmossdk.io/client/v2/helpers"
1515
"cosmossdk.io/core/appmodule"
1616
"cosmossdk.io/log"
1717
storetypes "cosmossdk.io/store/types"
@@ -178,12 +178,11 @@ type SimApp struct {
178178
}
179179

180180
func init() {
181-
userHomeDir, err := os.UserHomeDir()
181+
var err error
182+
DefaultNodeHome, err = clienthelpers.GetNodeHomeDirectory(".simapp")
182183
if err != nil {
183184
panic(err)
184185
}
185-
186-
DefaultNodeHome = filepath.Join(userHomeDir, ".simapp")
187186
}
188187

189188
// NewSimApp returns a reference to an initialized SimApp.

simapp/app_v2.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ package simapp
44

55
import (
66
"io"
7-
"os"
8-
"path/filepath"
97

108
dbm "github.com/cosmos/cosmos-db"
119

10+
clienthelpers "cosmossdk.io/client/v2/helpers"
1211
"cosmossdk.io/depinject"
1312
"cosmossdk.io/log"
1413
storetypes "cosmossdk.io/store/types"
@@ -89,12 +88,11 @@ type SimApp struct {
8988
}
9089

9190
func init() {
92-
userHomeDir, err := os.UserHomeDir()
91+
var err error
92+
DefaultNodeHome, err = clienthelpers.GetNodeHomeDirectory(".simapp")
9393
if err != nil {
9494
panic(err)
9595
}
96-
97-
DefaultNodeHome = filepath.Join(userHomeDir, ".simapp")
9896
}
9997

10098
// NewSimApp returns a reference to an initialized SimApp.

simapp/go.mod

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.21
44

55
require (
66
cosmossdk.io/api v0.7.5
7-
cosmossdk.io/client/v2 v2.0.0-beta.1.0.20240619200301-495e6d11c307
7+
cosmossdk.io/client/v2 v2.0.0-beta.2.0.20240625194835-17173894fea9 // TODO(@julienrbrt): tag client/v2
88
cosmossdk.io/collections v0.4.0 // indirect
99
cosmossdk.io/core v0.11.0
1010
cosmossdk.io/depinject v1.0.0-alpha.4
@@ -17,7 +17,7 @@ require (
1717
cosmossdk.io/x/feegrant v0.1.1
1818
cosmossdk.io/x/nft v0.1.1
1919
cosmossdk.io/x/tx v0.13.3
20-
cosmossdk.io/x/upgrade v0.1.2
20+
cosmossdk.io/x/upgrade v0.1.4-0.20240625194835-17173894fea9 // TODO(@julienrbrt): tag upgrade
2121
github.com/cometbft/cometbft v0.38.7
2222
github.com/cosmos/cosmos-db v1.0.2
2323
// this version is not used as it is always replaced by the latest Cosmos SDK version
@@ -27,17 +27,17 @@ require (
2727
github.com/spf13/cast v1.6.0
2828
github.com/spf13/cobra v1.8.0
2929
github.com/spf13/pflag v1.0.5
30-
github.com/spf13/viper v1.18.2
30+
github.com/spf13/viper v1.19.0
3131
github.com/stretchr/testify v1.9.0
3232
google.golang.org/protobuf v1.33.0
3333
)
3434

3535
require (
36-
cloud.google.com/go v0.112.0 // indirect
36+
cloud.google.com/go v0.112.1 // indirect
3737
cloud.google.com/go/compute v1.24.0 // indirect
3838
cloud.google.com/go/compute/metadata v0.2.3 // indirect
3939
cloud.google.com/go/iam v1.1.6 // indirect
40-
cloud.google.com/go/storage v1.36.0 // indirect
40+
cloud.google.com/go/storage v1.38.0 // indirect
4141
cosmossdk.io/errors v1.0.1 // indirect
4242
filippo.io/edwards25519 v1.0.0 // indirect
4343
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
@@ -102,15 +102,15 @@ require (
102102
github.com/google/s2a-go v0.1.7 // indirect
103103
github.com/google/uuid v1.6.0 // indirect
104104
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
105-
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
105+
github.com/googleapis/gax-go/v2 v2.12.3 // indirect
106106
github.com/gorilla/handlers v1.5.1 // indirect
107107
github.com/gorilla/mux v1.8.0 // indirect
108108
github.com/gorilla/websocket v1.5.0 // indirect
109109
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
110110
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
111111
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
112112
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
113-
github.com/hashicorp/go-getter v1.7.3 // indirect
113+
github.com/hashicorp/go-getter v1.7.4 // indirect
114114
github.com/hashicorp/go-hclog v1.5.0 // indirect
115115
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
116116
github.com/hashicorp/go-metrics v0.5.3 // indirect
@@ -144,7 +144,7 @@ require (
144144
github.com/mtibben/percent v0.2.1 // indirect
145145
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a // indirect
146146
github.com/oklog/run v1.1.0 // indirect
147-
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
147+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
148148
github.com/petermattis/goid v0.0.0-20231207134359-e60b3f734c67 // indirect
149149
github.com/pkg/errors v0.9.1 // indirect
150150
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
@@ -170,11 +170,11 @@ require (
170170
github.com/zondax/ledger-go v0.14.3 // indirect
171171
go.etcd.io/bbolt v1.3.8 // indirect
172172
go.opencensus.io v0.24.0 // indirect
173-
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.47.0 // indirect
174-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.47.0 // indirect
175-
go.opentelemetry.io/otel v1.22.0 // indirect
176-
go.opentelemetry.io/otel/metric v1.22.0 // indirect
177-
go.opentelemetry.io/otel/trace v1.22.0 // indirect
173+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
174+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
175+
go.opentelemetry.io/otel v1.24.0 // indirect
176+
go.opentelemetry.io/otel/metric v1.24.0 // indirect
177+
go.opentelemetry.io/otel/trace v1.24.0 // indirect
178178
go.uber.org/multierr v1.10.0 // indirect
179179
golang.org/x/crypto v0.22.0 // indirect
180180
golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0 // indirect
@@ -185,10 +185,10 @@ require (
185185
golang.org/x/term v0.19.0 // indirect
186186
golang.org/x/text v0.14.0 // indirect
187187
golang.org/x/time v0.5.0 // indirect
188-
google.golang.org/api v0.162.0 // indirect
188+
google.golang.org/api v0.171.0 // indirect
189189
google.golang.org/appengine v1.6.8 // indirect
190190
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
191-
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
191+
google.golang.org/genproto/googleapis/api v0.0.0-20240311132316-a219d84964c2 // indirect
192192
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
193193
google.golang.org/grpc v1.63.2 // indirect
194194
gopkg.in/ini.v1 v1.67.0 // indirect

0 commit comments

Comments
 (0)