Skip to content

Commit 741b01c

Browse files
mergify[bot]mmsqe
andauthoredApr 15, 2024
fix(client/v2): respect output format from client ctx (backport #20033) (#20046)
Co-authored-by: mmsqe <[email protected]>
1 parent 521ebe5 commit 741b01c

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed
 

‎client/v2/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
5252
* [#19976](https://github.com/cosmos/cosmos-sdk/pull/19976) Add encoder for `cosmos.base.v1beta1.DecCoin`.
5353
* [#19377](https://github.com/cosmos/cosmos-sdk/pull/19377) Partly fix comment parsing in autocli.
5454
* [#19060](https://github.com/cosmos/cosmos-sdk/pull/19060) Simplify key flag parsing logic in flag handler.
55+
* [#20033](https://github.com/cosmos/cosmos-sdk/pull/20033) Respect output format from client ctx.
5556

5657
## [v2.0.0-beta.1] - 2023-11-07
5758

‎client/v2/autocli/common.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
1111
"cosmossdk.io/client/v2/internal/flags"
1212
"cosmossdk.io/client/v2/internal/util"
13+
14+
"github.com/cosmos/cosmos-sdk/client"
1315
)
1416

1517
type cmdType int
@@ -225,11 +227,21 @@ func enhanceCustomCmd(builder *Builder, cmd *cobra.Command, cmdType cmdType, mod
225227

226228
// outOrStdoutFormat formats the output based on the output flag and writes it to the command's output stream.
227229
func (b *Builder) outOrStdoutFormat(cmd *cobra.Command, out []byte) error {
230+
clientCtx := client.Context{}
231+
if v := cmd.Context().Value(client.ClientContextKey); v != nil {
232+
clientCtx = *(v.(*client.Context))
233+
}
234+
flagSet := cmd.Flags()
235+
if clientCtx.OutputFormat == "" || flagSet.Changed(flags.FlagOutput) {
236+
output, _ := flagSet.GetString(flags.FlagOutput)
237+
clientCtx = clientCtx.WithOutputFormat(output)
238+
}
239+
228240
var err error
229-
outputType := cmd.Flag(flags.FlagOutput)
241+
outputType := clientCtx.OutputFormat
230242
// if the output type is text, convert the json to yaml
231243
// if output type is json or nil, default to json
232-
if outputType != nil && outputType.Value.String() == flags.OutputFormatText {
244+
if outputType == flags.OutputFormatText {
233245
out, err = yaml.JSONToYAML(out)
234246
if err != nil {
235247
return err

0 commit comments

Comments
 (0)