Skip to content

Commit 1adc1f0

Browse files
authoredApr 15, 2024
fix(client/v2): respect output format from client ctx (#20033)
1 parent 84c64b2 commit 1adc1f0

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
@@ -56,6 +56,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
5656
* [#19976](https://github.com/cosmos/cosmos-sdk/pull/19976) Add encoder for `cosmos.base.v1beta1.DecCoin`.
5757
* [#19377](https://github.com/cosmos/cosmos-sdk/pull/19377) Partly fix comment parsing in autocli.
5858
* [#19060](https://github.com/cosmos/cosmos-sdk/pull/19060) Simplify key flag parsing logic in flag handler.
59+
* [#20033](https://github.com/cosmos/cosmos-sdk/pull/20033) Respect output format from client ctx.
5960

6061
### API Breaking Changes
6162

‎client/v2/autocli/common.go

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

1618
type cmdType int
@@ -224,11 +226,21 @@ func enhanceCustomCmd(builder *Builder, cmd *cobra.Command, cmdType cmdType, mod
224226

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

0 commit comments

Comments
 (0)