-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cosmovisor): let
cosmovisor version
return a valid json (#11731)
- Loading branch information
1 parent
1d8a878
commit 019444a
Showing
17 changed files
with
208 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cmd | ||
|
||
import ( | ||
"io" | ||
"os" | ||
) | ||
|
||
// DefaultRunConfig defintes a default RunConfig that writes to os.Stdout and os.Stderr | ||
var DefaultRunConfig = RunConfig{ | ||
StdOut: os.Stdout, | ||
StdErr: os.Stderr, | ||
} | ||
|
||
// RunConfig defines the configuration for running a command | ||
type RunConfig struct { | ||
StdOut io.Writer | ||
StdErr io.Writer | ||
} | ||
|
||
type RunOption func(*RunConfig) | ||
|
||
// StdOutRunOption sets the StdOut writer for the Run command | ||
func StdOutRunOption(w io.Writer) RunOption { | ||
return func(cfg *RunConfig) { | ||
cfg.StdOut = w | ||
} | ||
} | ||
|
||
// SdErrRunOption sets the StdErr writer for the Run command | ||
func StdErrRunOption(w io.Writer) RunOption { | ||
return func(cfg *RunConfig) { | ||
cfg.StdErr = w | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,88 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
cverrors "github.com/cosmos/cosmos-sdk/cosmovisor/errors" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
// Version represents Cosmovisor version value. Overwritten during build | ||
var Version = "1.1.0" | ||
|
||
// VersionArgs is the strings that indicate a cosmovisor version command. | ||
var VersionArgs = []string{"version", "--version"} | ||
var ( | ||
// FlagJSON formats the output in json | ||
FlagJSON = "--json" | ||
// Version represents Cosmovisor version value. Overwritten during build | ||
Version = "1.1.0" | ||
// VersionArgs is the strings that indicate a cosmovisor version command. | ||
VersionArgs = []string{"version", "--version"} | ||
) | ||
|
||
// IsVersionCommand checks if the given args indicate that the version is being requested. | ||
func IsVersionCommand(arg string) bool { | ||
return isOneOf(arg, VersionArgs) | ||
} | ||
|
||
// PrintVersion prints the cosmovisor version. | ||
func PrintVersion() error { | ||
func PrintVersion(logger *zerolog.Logger, args []string) error { | ||
for _, arg := range args { | ||
if strings.Contains(arg, FlagJSON) { | ||
return printVersionJSON(logger, args) | ||
} | ||
} | ||
|
||
return printVersion(logger, args) | ||
} | ||
|
||
func printVersion(logger *zerolog.Logger, args []string) 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) | ||
if err := Run(logger, append([]string{"version"}, args...)); err != nil { | ||
handleRunVersionFailure(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func printVersionJSON(logger *zerolog.Logger, args []string) error { | ||
buf := new(strings.Builder) | ||
|
||
// disable logger | ||
l := logger.Level(zerolog.Disabled) | ||
logger = &l | ||
|
||
if err := Run( | ||
logger, | ||
[]string{"version", "--long", "--output", "json"}, | ||
StdOutRunOption(buf), | ||
); err != nil { | ||
handleRunVersionFailure(err) | ||
} | ||
|
||
out, err := json.Marshal(struct { | ||
Version string `json:"cosmovisor_version"` | ||
AppVersion json.RawMessage `json:"app_version"` | ||
}{ | ||
Version: Version, | ||
AppVersion: json.RawMessage(buf.String()), | ||
}) | ||
if err != nil { | ||
l := logger.Level(zerolog.TraceLevel) | ||
logger = &l | ||
return fmt.Errorf("Can't print version output, expected valid json from APP, got: %s - %w", buf.String(), err) | ||
} | ||
|
||
fmt.Println(string(out)) | ||
return nil | ||
} | ||
|
||
func handleRunVersionFailure(err error) { | ||
// 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.