Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
akaladarshi committed Apr 23, 2024
1 parent bc91f5a commit 9a9a729
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 20 deletions.
4 changes: 2 additions & 2 deletions tools/cosmovisor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

* [#19995](https://github.com/cosmos/cosmos-sdk/pull/19995):
* `init command` writes the configuration to the config file only at the default path `DAEMON_HOME/cosmovisor/config.toml`.
* Set `CONFIG` environment variable to provide `config.toml` path to load the configuration in `cosmovisor run`.
* Add `--config` flag to provide `config.toml` path to the configuration file in root command used by `add-upgrade` and `config` subcommands.
* Provide `--cosmovisor-config` flag with value as args to provide the path to the configuration file in the `run` command. `run --cosmovisor-config <path> (other cmds with flags) ...`.
* Add `--cosmovisor-config` flag to provide `config.toml` path to the configuration file in root command used by `add-upgrade` and `config` subcommands.
* `config command` now displays the configuration from the config file if it is provided. If the config file is not provided, it will display the configuration from the environment variables.

## v1.5.0 - 2023-07-17
Expand Down
2 changes: 0 additions & 2 deletions tools/cosmovisor/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
upgradetypes "cosmossdk.io/x/upgrade/types"
)

var ErrEmptyConfigENV = errors.New("config env variable not set or empty")

// environment variable names
const (
EnvHome = "DAEMON_HOME"
Expand Down
2 changes: 1 addition & 1 deletion tools/cosmovisor/cmd/cosmovisor/add_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewAddUpgradeCmd() *cobra.Command {

// AddUpgrade adds upgrade info to manifest
func AddUpgrade(cmd *cobra.Command, args []string) error {
configPath, err := cmd.Flags().GetString(cosmovisor.FlagConfig)
configPath, err := cmd.Flags().GetString(cosmovisor.FlagCosmovisorConfig)
if err != nil {
return fmt.Errorf("failed to get config flag: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tools/cosmovisor/cmd/cosmovisor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var configCmd = &cobra.Command{
otherwise it will display the config from the environment variables.`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := cosmovisor.GetConfigFromFile(cmd.Flag(cosmovisor.FlagConfig).Value.String())
cfg, err := cosmovisor.GetConfigFromFile(cmd.Flag(cosmovisor.FlagCosmovisorConfig).Value.String())
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions tools/cosmovisor/cmd/cosmovisor/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func NewIntCmd() *cobra.Command {
initCmd := &cobra.Command{
Use: "init <path to executable>",
Short: "Initialize a cosmovisor daemon home directory.",
Long: `Initialize a cosmovisor daemon home directory with the provided executable
Configuration file uses default path (<-home->/cosmovisor/config.toml) to export the config.`,
Long: `Initialize a cosmovisor daemon home directory with the provided executable.
Configuration file is initialized at the default path (<-home->/cosmovisor/config.toml).`,
Args: cobra.ExactArgs(1),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion tools/cosmovisor/cmd/cosmovisor/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ func NewRootCmd() *cobra.Command {
NewAddUpgradeCmd(),
)

rootCmd.PersistentFlags().String(cosmovisor.FlagConfig, "", "path to cosmovisor config file")
rootCmd.PersistentFlags().StringP(cosmovisor.FlagCosmovisorConfig, "c", "", "path to cosmovisor config file")
return rootCmd
}
41 changes: 32 additions & 9 deletions tools/cosmovisor/cmd/cosmovisor/run.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
package main

import (
"os"
"fmt"
"strings"

"github.com/spf13/cobra"

"cosmossdk.io/tools/cosmovisor"
)

const (
cfgENV = "CONFIG"
)

var runCmd = &cobra.Command{
Use: "run",
Short: "Run an APP command.",
Long: `Run an APP command. This command is intended to be used by the cosmovisor binary.
Provide config file path via CONFIG environment variable.
Provide cosmovisor config file path in command args or set env variables to load configuration.
`,
SilenceUsage: true,
DisableFlagParsing: true,
RunE: func(_ *cobra.Command, args []string) error {
return run(args)
cfgPath, args, err := parseCosmovisorConfig(args)
if err != nil {
return fmt.Errorf("failed to parse cosmovisor config: %w", err)
}

return run(cfgPath, args)
},
}

// run runs the configured program with the given args and monitors it for upgrades.
func run(args []string, options ...RunOption) error {
cfg, err := cosmovisor.GetConfigFromFile(os.Getenv(cfgENV))
func run(cfgPath string, args []string, options ...RunOption) error {
cfg, err := cosmovisor.GetConfigFromFile(cfgPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -56,3 +58,24 @@ func run(args []string, options ...RunOption) error {

return err
}

func parseCosmovisorConfig(args []string) (string, []string, error) {
var configFilePath string
for i, arg := range args {
// Check if the argument is the config flag
if strings.EqualFold(arg, fmt.Sprintf("--%s", cosmovisor.FlagCosmovisorConfig)) ||
strings.EqualFold(arg, fmt.Sprintf("-%s", cosmovisor.FlagCosmovisorConfig)) {
// Check if there is an argument after the flag which should be the config file path
if i+1 >= len(args) {
return "", nil, fmt.Errorf("--%s requires an argument", cosmovisor.FlagCosmovisorConfig)
}

configFilePath = args[i+1]
// Remove the flag and its value from the arguments
args = append(args[:i], args[i+2:]...)
break
}
}

return configFilePath, args, nil
}
3 changes: 2 additions & 1 deletion tools/cosmovisor/cmd/cosmovisor/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func printVersion(cmd *cobra.Command, args []string, noAppVersion bool) error {
return nil
}

if err := run(append([]string{"version"}, args...)); err != nil {
if err := run("", append([]string{"version"}, args...)); err != nil {
return fmt.Errorf("failed to run version command: %w", err)
}

Expand All @@ -62,6 +62,7 @@ func printVersionJSON(cmd *cobra.Command, args []string, noAppVersion bool) erro

buf := new(strings.Builder)
if err := run(
"",
[]string{"version", "--long", "--output", "json"},
StdOutRunOption(buf),
); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tools/cosmovisor/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ const (
FlagCosmovisorOnly = "cosmovisor-only"
FlagForce = "force"
FlagUpgradeHeight = "upgrade-height"
FlagConfig = "config"
FlagCosmovisorConfig = "cosmovisor-config"
)

0 comments on commit 9a9a729

Please sign in to comment.