From e4be28c2d3fb3618d63b35055397245c8eb2d63d Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 24 Feb 2025 09:57:55 +0100 Subject: [PATCH] fix(services): allow tilde and env in config validator home path (backport #4532) (#4539) * fix(services): allow tilde and env in config validator home path (#4532) * fix(services): allow tilde and env in config validator home path * updates * up (cherry picked from commit ff663c97bac88cdac5194682327a005e695e7099) # Conflicts: # ignite/config/chain/v1/validator.go # ignite/services/chain/chain.go # ignite/templates/app/files/go.mod.plush # ignite/templates/typed/simulation.go * conflicts --------- Co-authored-by: Julien Robert --- changelog.md | 4 ++++ ignite/config/chain/v1/validator.go | 3 --- ignite/services/chain/chain.go | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index bc6f95c8a5..6d757cc15b 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,10 @@ ## Unreleased +### Bug Fixe + +- [#4532](https://github.com/ignite/cli/pull/4532) Fix non working _shortcuts_ in validator home config + ## [`v28.8.0`](https://github.com/ignite/cli/releases/tag/v28.8.0) ### Features diff --git a/ignite/config/chain/v1/validator.go b/ignite/config/chain/v1/validator.go index 7af0b615c5..2594c9e092 100644 --- a/ignite/config/chain/v1/validator.go +++ b/ignite/config/chain/v1/validator.go @@ -36,9 +36,6 @@ type Gentx struct { // Moniker is the validator's (optional) moniker. Moniker string `yaml:"moniker"` - // Home is directory for config and data. - Home string `yaml:"home"` - // KeyringBackend is keyring's backend. KeyringBackend string `yaml:"keyring-backend"` diff --git a/ignite/services/chain/chain.go b/ignite/services/chain/chain.go index 54288d92a8..099d2b152d 100644 --- a/ignite/services/chain/chain.go +++ b/ignite/services/chain/chain.go @@ -4,6 +4,7 @@ import ( "context" "os" "path/filepath" + "strings" "github.com/go-git/go-git/v5" "github.com/spf13/cobra" @@ -358,6 +359,11 @@ func (c *Chain) DefaultHome() (string, error) { } validator, _ := chainconfig.FirstValidator(cfg) if validator.Home != "" { + expandedHome, err := expandHome(validator.Home) + if err != nil { + return "", err + } + validator.Home = expandedHome return validator.Home, nil } @@ -531,3 +537,16 @@ func (c *Chain) Commands(ctx context.Context) (chaincmdrunner.Runner, error) { return chaincmdrunner.New(ctx, cc, ccrOptions...) } + +// expandHome expands a path that may start with "~" and may contain environment variables. +func expandHome(path string) (string, error) { + if strings.HasPrefix(path, "~") { + home, err := os.UserHomeDir() + if err != nil { + return "", err + } + // Only replace the first occurrence at the start. + path = home + strings.TrimPrefix(path, "~") + } + return os.ExpandEnv(path), nil +}