diff --git a/cosmovisor/CHANGELOG.md b/cosmovisor/CHANGELOG.md index 91d0e9ffd..0504d4be1 100644 --- a/cosmovisor/CHANGELOG.md +++ b/cosmovisor/CHANGELOG.md @@ -44,6 +44,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ + [\#10285](https://github.com/cosmos/cosmos-sdk/pull/10316) Running `cosmovisor` without the `run` argument. +### Bug Fixes + ++ [\#10458](https://github.com/cosmos/cosmos-sdk/pull/10458) Fix version when using 'go install github.com/cosmos/cosmos-sdk/cosmovisor/cmd/cosmovisor@v1.0.0' to install cosmovisor. + ## v1.0.0 2021-09-30 ### Features diff --git a/cosmovisor/args.go b/cosmovisor/args.go index fa5ef2036..b58e4f7c1 100644 --- a/cosmovisor/args.go +++ b/cosmovisor/args.go @@ -177,20 +177,16 @@ func GetConfigFromEnv() (*Config, error) { } // LogConfigOrError logs either the config details or the error. -func LogConfigOrError(logger zerolog.Logger, cfg *Config, cerr error) { +func LogConfigOrError(logger zerolog.Logger, cfg *Config, err error) { + if cfg == nil && err == nil { + return + } + logger.Info().Msg("Configuration:") switch { - case cerr != nil: - switch err := cerr.(type) { - case *cverrors.MultiError: - logger.Error().Msg("multiple configuration errors found:") - for i, e := range err.GetErrors() { - logger.Error().Err(e).Msg(fmt.Sprintf(" %d:", i+1)) - } - default: - logger.Error().Err(cerr).Msg("configuration error:") - } + case err != nil: + cverrors.LogErrors(logger, "configuration errors found", err) case cfg != nil: - logger.Info().Msg("Configuration is valid:\n" + cfg.DetailString()) + logger.Info().Msg(cfg.DetailString()) } } diff --git a/cosmovisor/args_test.go b/cosmovisor/args_test.go index 86c0d0df1..069050a1a 100644 --- a/cosmovisor/args_test.go +++ b/cosmovisor/args_test.go @@ -554,14 +554,14 @@ func (s *argsTestSuite) TestLogConfigOrError() { name: "multi error", cfg: nil, err: errMulti, - contains: []string{"multiple configuration errors found", errs[0].Error(), errs[1].Error(), errs[2].Error()}, + contains: []string{"configuration errors found", errs[0].Error(), errs[1].Error(), errs[2].Error()}, notcontains: nil, }, { name: "config", cfg: cfg, err: nil, - contains: []string{"Configuration is valid", cfg.DetailString()}, + contains: []string{"Configurable Values", cfg.DetailString()}, notcontains: nil, }, { diff --git a/cosmovisor/cmd/cosmovisor/cmd/help.go b/cosmovisor/cmd/cosmovisor/cmd/help.go index abb757dd9..e25df4ee3 100644 --- a/cosmovisor/cmd/cosmovisor/cmd/help.go +++ b/cosmovisor/cmd/cosmovisor/cmd/help.go @@ -2,10 +2,6 @@ package cmd import ( "fmt" - "os" - "time" - - "github.com/rs/zerolog" "github.com/cosmos/cosmos-sdk/cosmovisor" ) @@ -17,20 +13,13 @@ var HelpArgs = []string{"help", "--help", "-h"} // Help is needed if either cosmovisor.EnvName and/or cosmovisor.EnvHome env vars aren't set. // Help is requested if the first arg is "help", "--help", or "-h". func ShouldGiveHelp(arg string) bool { - return isOneOf(arg, HelpArgs) || len(os.Getenv(cosmovisor.EnvName)) == 0 || len(os.Getenv(cosmovisor.EnvHome)) == 0 + return isOneOf(arg, HelpArgs) } // DoHelp outputs help text func DoHelp() { // Not using the logger for this output because the header and footer look weird for help text. fmt.Println(GetHelpText()) - // Check the config and output details or any errors. - // Not using the cosmovisor.Logger in order to ignore any level it might have set, - // and also to not have any of the extra parameters in the output. - output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.Kitchen} - logger := zerolog.New(output).With().Timestamp().Logger() - cfg, err := cosmovisor.GetConfigFromEnv() - cosmovisor.LogConfigOrError(logger, cfg, err) } // GetHelpText creates the help text multi-line string. @@ -49,5 +38,10 @@ documented in: https://github.com/cosmos/cosmos-sdk/tree/master/cosmovisor/READM To get help for the configured binary: cosmovisor run help + +Available Commands: + help This help message + run Runs app passing all subsequent parameters + version Prints version of cosmovisor and the associated app. `, cosmovisor.EnvName, cosmovisor.EnvHome) } diff --git a/cosmovisor/cmd/cosmovisor/cmd/help_test.go b/cosmovisor/cmd/cosmovisor/cmd/help_test.go index 636695701..ebcc252d5 100644 --- a/cosmovisor/cmd/cosmovisor/cmd/help_test.go +++ b/cosmovisor/cmd/cosmovisor/cmd/help_test.go @@ -88,94 +88,6 @@ func (s *HelpTestSuite) setEnv(t *testing.T, env *cosmovisorHelpEnv) { } } -func (s *HelpTestSuite) TestShouldGiveHelpEnvVars() { - initialEnv := s.clearEnv() - defer s.setEnv(nil, initialEnv) - - emptyVal := "" - homeVal := "/somehome" - nameVal := "somename" - - tests := []struct { - name string - envHome *string - envName *string - expected bool - }{ - { - name: "home set name set", - envHome: &homeVal, - envName: &nameVal, - expected: false, - }, - { - name: "home not set name not set", - envHome: nil, - envName: nil, - expected: true, - }, - { - name: "home empty name not set", - envHome: &emptyVal, - envName: nil, - expected: true, - }, - { - name: "home set name not set", - envHome: &homeVal, - envName: nil, - expected: true, - }, - { - name: "home not set name empty", - envHome: nil, - envName: &emptyVal, - expected: true, - }, - { - name: "home empty name empty", - envHome: &emptyVal, - envName: &emptyVal, - expected: true, - }, - { - name: "home set name empty", - envHome: &homeVal, - envName: &emptyVal, - expected: true, - }, - { - name: "home not set name set", - envHome: nil, - envName: &nameVal, - expected: true, - }, - { - name: "home empty name set", - envHome: &emptyVal, - envName: &nameVal, - expected: true, - }, - } - - prepEnv := func(t *testing.T, envVar string, envVal *string) { - if envVal == nil { - require.NoError(t, os.Unsetenv(cosmovisor.EnvHome)) - } else { - require.NoError(t, os.Setenv(envVar, *envVal)) - } - } - - for _, tc := range tests { - s.T().Run(tc.name, func(t *testing.T) { - prepEnv(t, cosmovisor.EnvHome, tc.envHome) - prepEnv(t, cosmovisor.EnvName, tc.envName) - actual := ShouldGiveHelp("not-a-help-arg") - assert.Equal(t, tc.expected, actual) - }) - } -} - func (s HelpTestSuite) TestShouldGiveHelpArg() { initialEnv := s.clearEnv() defer s.setEnv(nil, initialEnv) diff --git a/cosmovisor/cmd/cosmovisor/cmd/root.go b/cosmovisor/cmd/cosmovisor/cmd/root.go index a4d3f2b2f..2b1921ca5 100644 --- a/cosmovisor/cmd/cosmovisor/cmd/root.go +++ b/cosmovisor/cmd/cosmovisor/cmd/root.go @@ -13,12 +13,11 @@ func RunCosmovisorCommand(args []string) error { arg0 = strings.TrimSpace(args[0]) } switch { + case IsVersionCommand(arg0): + return PrintVersion() case ShouldGiveHelp(arg0): DoHelp() return nil - case IsVersionCommand(arg0): - PrintVersion() - return Run([]string{"version"}) case IsRunCommand(arg0): return Run(args[1:]) } diff --git a/cosmovisor/cmd/cosmovisor/cmd/run.go b/cosmovisor/cmd/cosmovisor/cmd/run.go index 428aa5391..8912e1a97 100644 --- a/cosmovisor/cmd/cosmovisor/cmd/run.go +++ b/cosmovisor/cmd/cosmovisor/cmd/run.go @@ -16,10 +16,9 @@ func IsRunCommand(arg string) bool { // Run runs the configured program with the given args and monitors it for upgrades. func Run(args []string) error { - cfg, cerr := cosmovisor.GetConfigFromEnv() - cosmovisor.LogConfigOrError(cosmovisor.Logger, cfg, cerr) - if cerr != nil { - return cerr + cfg, err := cosmovisor.GetConfigFromEnv() + if err != nil { + return err } launcher, err := cosmovisor.NewLauncher(cfg) if err != nil { diff --git a/cosmovisor/cmd/cosmovisor/cmd/version.go b/cosmovisor/cmd/cosmovisor/cmd/version.go index 11c6ee75e..774c64b9b 100644 --- a/cosmovisor/cmd/cosmovisor/cmd/version.go +++ b/cosmovisor/cmd/cosmovisor/cmd/version.go @@ -2,6 +2,11 @@ package cmd import ( "fmt" + "os" + "time" + + cverrors "github.com/cosmos/cosmos-sdk/cosmovisor/errors" + "github.com/rs/zerolog" ) // Version represents Cosmovisor version value. Set during build @@ -16,6 +21,16 @@ func IsVersionCommand(arg string) bool { } // PrintVersion prints the cosmovisor version. -func PrintVersion() { +func PrintVersion() error { fmt.Println("Cosmovisor Version: ", Version) + + if err := Run([]string{"version"}); err != nil { + // Check the config and output details or any errors. + // Not using the cosmovisor.Logger in order to ignore any level it might have set, + // and also to not have any of the extra parameters in the output. + output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.Kitchen} + logger := zerolog.New(output).With().Timestamp().Logger() + cverrors.LogErrors(logger, "Can't run APP version", err) + } + return nil } diff --git a/cosmovisor/cmd/cosmovisor/main.go b/cosmovisor/cmd/cosmovisor/main.go index 4d82805be..8cd8623c7 100644 --- a/cosmovisor/cmd/cosmovisor/main.go +++ b/cosmovisor/cmd/cosmovisor/main.go @@ -5,12 +5,13 @@ import ( "github.com/cosmos/cosmos-sdk/cosmovisor" "github.com/cosmos/cosmos-sdk/cosmovisor/cmd/cosmovisor/cmd" + cverrors "github.com/cosmos/cosmos-sdk/cosmovisor/errors" ) func main() { cosmovisor.SetupLogging() if err := cmd.RunCosmovisorCommand(os.Args[1:]); err != nil { - cosmovisor.Logger.Error().Err(err).Msg("") + cverrors.LogErrors(cosmovisor.Logger, "", err) os.Exit(1) } } diff --git a/cosmovisor/errors/multi.go b/cosmovisor/errors/multi.go index 3b1d419ea..bb7885a9b 100644 --- a/cosmovisor/errors/multi.go +++ b/cosmovisor/errors/multi.go @@ -3,6 +3,8 @@ package errors import ( "fmt" "strings" + + "github.com/rs/zerolog" ) // MultiError is an error combining multiple other errors. @@ -67,3 +69,18 @@ func (e *MultiError) Error() string { func (e MultiError) String() string { return e.Error() } + +func LogErrors(logger zerolog.Logger, msg string, err error) { + switch err := err.(type) { + case *MultiError: + if msg != "" { + logger.Error().Msg(msg) + } + for i, e := range err.GetErrors() { + logger.Error().Err(e).Msg(fmt.Sprintf(" %d:", i+1)) + } + default: + logger.Error().Err(err).Msg(msg) + } + +}