cosmos-sdk/cosmovisor/errors/multi.go

87 lines
2.0 KiB
Go
Raw Normal View History

feat: Add cosmovisor --help (#10229) * [10126]: Create the help text and a func for checking if help is requested. * [10126]: Check if help is requested and print it if so. * [10126]: If help is requested, and it's possible, also run the binary with the --help flag. * [10126]: Add a Config method for creating a detailed string. * [10126]: Include the config detail string if the config is okay. * [10126]: Create a MultiError error. * [10126]: Get all configuration errors rather than just one at a time. * [10126]: Create unit tests on the MultiError. * [10126]: Remove an extra space from an output string. * [10126]: Add unit tests on more of the args stuff. * [10126]: Export the environment variable name strings. * [10126]: Move the help command stuff into the new cmd area. * [10126]: Move the unit tests on the help stuff that just got moved. * [10126]: Lint fixes. * [10126]: Export the EnvPreupgradeMaxRetries const and handle its error the same way as the others. * [10126]: Update the args test with the new config entry. * [10126]: Add EnvPreupgradeMaxRetries to the help text. * [10126]: Output the full path in the error when the root isn't a directory. * [10126]: Add some newlines that were missing from some help output. * [10126]: Add a link to the documentation to the help text. * [10126]: Don't allow MultiErrors to be in MultiErrors by extracting sub-errors while flattening. * [10126]: Add some missing function comments. * [10126]: Add changelog entry. * [10126]: Fix changelog pull link. * [10126]: Move multierror into its own errors package, then rename it to just multi. * [10126]: Change the Config DetailString to use the environment variables instead of Config field names. Also add the missing PreupgradeMaxRetries entry to it. * [10126]: Remove the environment variables from the help text and just defer to the cosmovisor README. * [10126]: Update the help text as suggested. * [10126]: Update ShouldGiveHelp. Give help if either name or home env vars aren't set. Give help if the first arg is help or any args are -h or --help. This reflects cobra defaults. * [10126]: Pass all args when running a help command rather than just using --help. * [10126]: Undo the changes to process.go. Instead, if an app is configured, just call it with the provided args. * [10126]: Output help if any arg is help. * [10126]: Reorg imports. * [10126]: Change 'Monitored Upgrade Info File' to just 'Monitored File'. The rest of the filename gives the rest of the context. * [10126]: Move the config error handling and output out of DoHelp and put it into the main Run function. That way, it's not being done twice in two different ways, and the setup is always logged. * [10126]: Make checking for a help request case-insensitive (to match what's done in version). * [10126]: Fix unit test that broke when I changed the Monitored File title. * [10126]: Change some unit test env var stuff to use a struct instead of string slices.
2021-09-28 16:08:31 -07:00
package errors
import (
"fmt"
"strings"
fix(cosmovisor): udpate the help and version command output (#10458) ## Description Closes: #10450 * `cosmovisor help` should not print any error. * `cosmovisor version` should print more clear message if it can't run `app version`. ## Before ``` ~/tmp ❯ cosmovisor version Cosmosvisor - A process manager for Cosmos SDK application binaries. Cosmovisor is a wrapper for a Cosmos SDK based App (set using the required DAEMON_NAME env variable). It starts the App by passing all provided arguments and monitors the DAEMON_HOME/data/upgrade-info.json file to perform an update. The upgrade-info.json file is created by the App x/upgrade module when the blockchain height reaches an approved upgrade proposal. The file includes data from the proposal. Cosmovisor interprets that data to perform an update: switch a current binary and restart the App. Configuration of Cosmovisor is done through environment variables, which are documented in: https://github.com/cosmos/cosmos-sdk/tree/master/cosmovisor/README.md 8:03PM ERR multiple configuration errors found: module=cosmovisor 8:03PM ERR 1: error="DAEMON_NAME is not set" module=cosmovisor 8:03PM ERR 2: error="DAEMON_HOME is not set" module=cosmovisor 8:03PM ERR error="2 errors: 1: DAEMON_NAME is not set, 2: DAEMON_HOME is not set" module=cosmovisor ❯ cosmovisor help Cosmosvisor - A process manager for Cosmos SDK application binaries. Cosmovisor is a wrapper for a Cosmos SDK based App (set using the required DAEMON_NAME env variable). It starts the App by passing all provided arguments and monitors the DAEMON_HOME/data/upgrade-info.json file to perform an update. The upgrade-info.json file is created by the App x/upgrade module when the blockchain height reaches an approved upgrade proposal. The file includes data from the proposal. Cosmovisor interprets that data to perform an update: switch a current binary and restart the App. Configuration of Cosmovisor is done through environment variables, which are documented in: https://github.com/cosmos/cosmos-sdk/tree/master/cosmovisor/README.md 8:03PM ERR multiple configuration errors found: module=cosmovisor 8:03PM ERR 1: error="DAEMON_NAME is not set" module=cosmovisor 8:03PM ERR 2: error="DAEMON_HOME is not set" module=cosmovisor 8:03PM ERR error="2 errors: 1: DAEMON_NAME is not set, 2: DAEMON_HOME is not set" module=cosmovisor ``` ## After ``` ❯ ./cosmovisor version Cosmovisor Version: v1.0.0-76-g66bddfb04 8:05PM ERR Can't run APP version 8:05PM ERR 1: error="DAEMON_NAME is not set" 8:05PM ERR 2: error="DAEMON_HOME is not set" ❯ ./cosmovisor help Cosmosvisor - A process manager for Cosmos SDK application binaries. Cosmovisor is a wrapper for a Cosmos SDK based App (set using the required DAEMON_NAME env variable). It starts the App by passing all provided arguments and monitors the DAEMON_HOME/data/upgrade-info.json file to perform an update. The upgrade-info.json file is created by the App x/upgrade module when the blockchain height reaches an approved upgrade proposal. The file includes data from the proposal. Cosmovisor interprets that data to perform an update: switch a current binary and restart the App. Configuration of Cosmovisor is done through environment variables, which are documented in: https://github.com/cosmos/cosmos-sdk/tree/master/cosmovisor/README.md To get help for the configured binary: cosmovisor run help ``` --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-11-19 09:35:56 -08:00
"github.com/rs/zerolog"
feat: Add cosmovisor --help (#10229) * [10126]: Create the help text and a func for checking if help is requested. * [10126]: Check if help is requested and print it if so. * [10126]: If help is requested, and it's possible, also run the binary with the --help flag. * [10126]: Add a Config method for creating a detailed string. * [10126]: Include the config detail string if the config is okay. * [10126]: Create a MultiError error. * [10126]: Get all configuration errors rather than just one at a time. * [10126]: Create unit tests on the MultiError. * [10126]: Remove an extra space from an output string. * [10126]: Add unit tests on more of the args stuff. * [10126]: Export the environment variable name strings. * [10126]: Move the help command stuff into the new cmd area. * [10126]: Move the unit tests on the help stuff that just got moved. * [10126]: Lint fixes. * [10126]: Export the EnvPreupgradeMaxRetries const and handle its error the same way as the others. * [10126]: Update the args test with the new config entry. * [10126]: Add EnvPreupgradeMaxRetries to the help text. * [10126]: Output the full path in the error when the root isn't a directory. * [10126]: Add some newlines that were missing from some help output. * [10126]: Add a link to the documentation to the help text. * [10126]: Don't allow MultiErrors to be in MultiErrors by extracting sub-errors while flattening. * [10126]: Add some missing function comments. * [10126]: Add changelog entry. * [10126]: Fix changelog pull link. * [10126]: Move multierror into its own errors package, then rename it to just multi. * [10126]: Change the Config DetailString to use the environment variables instead of Config field names. Also add the missing PreupgradeMaxRetries entry to it. * [10126]: Remove the environment variables from the help text and just defer to the cosmovisor README. * [10126]: Update the help text as suggested. * [10126]: Update ShouldGiveHelp. Give help if either name or home env vars aren't set. Give help if the first arg is help or any args are -h or --help. This reflects cobra defaults. * [10126]: Pass all args when running a help command rather than just using --help. * [10126]: Undo the changes to process.go. Instead, if an app is configured, just call it with the provided args. * [10126]: Output help if any arg is help. * [10126]: Reorg imports. * [10126]: Change 'Monitored Upgrade Info File' to just 'Monitored File'. The rest of the filename gives the rest of the context. * [10126]: Move the config error handling and output out of DoHelp and put it into the main Run function. That way, it's not being done twice in two different ways, and the setup is always logged. * [10126]: Make checking for a help request case-insensitive (to match what's done in version). * [10126]: Fix unit test that broke when I changed the Monitored File title. * [10126]: Change some unit test env var stuff to use a struct instead of string slices.
2021-09-28 16:08:31 -07:00
)
// MultiError is an error combining multiple other errors.
// It will never have 0 or 1 errors. It will always have two or more.
type MultiError struct {
errs []error
}
// FlattenErrors possibly creates a MultiError.
// Nil entries are ignored.
// If all provided errors are nil (or nothing is provided), nil is returned.
// If only one non-nil error is provided, it is returned unchanged.
// If two or more non-nil errors are provided, the returned error will be of type *MultiError
// and it will contain each non-nil error.
func FlattenErrors(errs ...error) error {
rv := MultiError{}
for _, err := range errs {
if err != nil {
if merr, isMerr := err.(*MultiError); isMerr {
rv.errs = append(rv.errs, merr.errs...)
} else {
rv.errs = append(rv.errs, err)
}
}
}
switch rv.Len() {
case 0:
return nil
case 1:
return rv.errs[0]
}
return &rv
}
// GetErrors gets all the errors that make up this MultiError.
func (e MultiError) GetErrors() []error {
// Return a copy of the errs slice to prevent alteration of the original slice.
rv := make([]error, e.Len())
copy(rv, e.errs)
return rv
}
// Len gets the number of errors in this MultiError.
func (e MultiError) Len() int {
return len(e.errs)
}
// Error implements the error interface for a MultiError.
func (e *MultiError) Error() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%d errors: ", len(e.errs)))
for i, err := range e.errs {
if i != 0 {
sb.WriteString(", ")
}
sb.WriteString(fmt.Sprintf("%d: %v", i+1, err))
}
return sb.String()
}
// String implements the string interface for a MultiError.
func (e MultiError) String() string {
return e.Error()
}
fix(cosmovisor): udpate the help and version command output (#10458) ## Description Closes: #10450 * `cosmovisor help` should not print any error. * `cosmovisor version` should print more clear message if it can't run `app version`. ## Before ``` ~/tmp ❯ cosmovisor version Cosmosvisor - A process manager for Cosmos SDK application binaries. Cosmovisor is a wrapper for a Cosmos SDK based App (set using the required DAEMON_NAME env variable). It starts the App by passing all provided arguments and monitors the DAEMON_HOME/data/upgrade-info.json file to perform an update. The upgrade-info.json file is created by the App x/upgrade module when the blockchain height reaches an approved upgrade proposal. The file includes data from the proposal. Cosmovisor interprets that data to perform an update: switch a current binary and restart the App. Configuration of Cosmovisor is done through environment variables, which are documented in: https://github.com/cosmos/cosmos-sdk/tree/master/cosmovisor/README.md 8:03PM ERR multiple configuration errors found: module=cosmovisor 8:03PM ERR 1: error="DAEMON_NAME is not set" module=cosmovisor 8:03PM ERR 2: error="DAEMON_HOME is not set" module=cosmovisor 8:03PM ERR error="2 errors: 1: DAEMON_NAME is not set, 2: DAEMON_HOME is not set" module=cosmovisor ❯ cosmovisor help Cosmosvisor - A process manager for Cosmos SDK application binaries. Cosmovisor is a wrapper for a Cosmos SDK based App (set using the required DAEMON_NAME env variable). It starts the App by passing all provided arguments and monitors the DAEMON_HOME/data/upgrade-info.json file to perform an update. The upgrade-info.json file is created by the App x/upgrade module when the blockchain height reaches an approved upgrade proposal. The file includes data from the proposal. Cosmovisor interprets that data to perform an update: switch a current binary and restart the App. Configuration of Cosmovisor is done through environment variables, which are documented in: https://github.com/cosmos/cosmos-sdk/tree/master/cosmovisor/README.md 8:03PM ERR multiple configuration errors found: module=cosmovisor 8:03PM ERR 1: error="DAEMON_NAME is not set" module=cosmovisor 8:03PM ERR 2: error="DAEMON_HOME is not set" module=cosmovisor 8:03PM ERR error="2 errors: 1: DAEMON_NAME is not set, 2: DAEMON_HOME is not set" module=cosmovisor ``` ## After ``` ❯ ./cosmovisor version Cosmovisor Version: v1.0.0-76-g66bddfb04 8:05PM ERR Can't run APP version 8:05PM ERR 1: error="DAEMON_NAME is not set" 8:05PM ERR 2: error="DAEMON_HOME is not set" ❯ ./cosmovisor help Cosmosvisor - A process manager for Cosmos SDK application binaries. Cosmovisor is a wrapper for a Cosmos SDK based App (set using the required DAEMON_NAME env variable). It starts the App by passing all provided arguments and monitors the DAEMON_HOME/data/upgrade-info.json file to perform an update. The upgrade-info.json file is created by the App x/upgrade module when the blockchain height reaches an approved upgrade proposal. The file includes data from the proposal. Cosmovisor interprets that data to perform an update: switch a current binary and restart the App. Configuration of Cosmovisor is done through environment variables, which are documented in: https://github.com/cosmos/cosmos-sdk/tree/master/cosmovisor/README.md To get help for the configured binary: cosmovisor run help ``` --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-11-19 09:35:56 -08:00
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)
}
}