Skip to content

Commit

Permalink
refactor: remove cometbft/libs/cli import (#15673)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Apr 3, 2023
1 parent f69fdad commit c1974e4
Show file tree
Hide file tree
Showing 21 changed files with 72 additions and 84 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* (client) [#15673](https://github.com/cosmos/cosmos-sdk/pull/15673) Move `client/keys.OutputFormatJSON` and `client/keys.OutputFormatText` to `client/flags` package.
* (x/nft) [#15588](https://github.com/cosmos/cosmos-sdk/pull/15588) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey` and methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context`.
* (x/auth) [#15520](https://github.com/cosmos/cosmos-sdk/pull/15520) `NewAccountKeeper` now takes a `KVStoreService` instead of a `StoreKey` and methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context`.
* (x/consensus) [#15517](https://github.com/cosmos/cosmos-sdk/pull/15517) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey`.
Expand Down Expand Up @@ -144,11 +145,11 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/capability) [#15344](https://github.com/cosmos/cosmos-sdk/pull/15344) Capability module was removed and is now housed in [IBC-GO](https://github.com/cosmos/ibc-go).
* [#15299](https://github.com/cosmos/cosmos-sdk/pull/15299) remove `StdTx` transaction and signing APIs. No SDK version has actually supported `StdTx` since before Stargate.
* [#15600](https://github.com/cosmos/cosmos-sdk/pull/15600) add support for getting signers to `codec.Codec` and protoregistry support to `InterfaceRegistry`:
* `Codec` is now a private interface and has the methods `InterfaceRegistry`, `GetMsgAnySigners`, `GetMsgV1Signers`, and `GetMsgV2Signers` which will fail when using `AminoCodec`.
* `Codec` is now a private interface and has the methods `InterfaceRegistry`, `GetMsgAnySigners`, `GetMsgV1Signers`, and `GetMsgV2Signers` which will fail when using `AminoCodec`.
All implementations of `Codec` by other users must now embed an official implementation from the `codec` package.
* `InterfaceRegistry` is now a private interface and implements `protodesc.Resolver` plus the `RangeFiles` method
* `InterfaceRegistry` is now a private interface and implements `protodesc.Resolver` plus the `RangeFiles` method
All implementations of `InterfaceRegistry` by other users must now embed the official implementation.
* `AminoCodec` is marked as deprecated.
* `AminoCodec` is marked as deprecated.

### Client Breaking Changes

Expand Down
9 changes: 4 additions & 5 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/cockroachdb/errors"
"github.com/cometbft/cometbft/libs/cli"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"google.golang.org/grpc"
Expand Down Expand Up @@ -92,8 +91,8 @@ func ValidateCmd(cmd *cobra.Command, args []string) error {
// - client.Context field pre-populated & flag not set: uses pre-populated value
// - client.Context field pre-populated & flag set: uses set flag value
func ReadPersistentCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, error) {
if clientCtx.OutputFormat == "" || flagSet.Changed(cli.OutputFlag) {
output, _ := flagSet.GetString(cli.OutputFlag)
if clientCtx.OutputFormat == "" || flagSet.Changed(flags.FlagOutput) {
output, _ := flagSet.GetString(flags.FlagOutput)
clientCtx = clientCtx.WithOutputFormat(output)
}

Expand Down Expand Up @@ -305,8 +304,8 @@ func readTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err
if isAux {
// If the user didn't explicitly set an --output flag, use JSON by
// default.
if clientCtx.OutputFormat == "" || !flagSet.Changed(cli.OutputFlag) {
clientCtx = clientCtx.WithOutputFormat("json")
if clientCtx.OutputFormat == "" || !flagSet.Changed(flags.FlagOutput) {
clientCtx = clientCtx.WithOutputFormat(flags.OutputFormatJSON)
}

// If the user didn't explicitly set a --sign-mode flag, use
Expand Down
17 changes: 6 additions & 11 deletions client/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ import (
"github.com/cosmos/cosmos-sdk/types/module/testutil"
)

const (
text = "text"
jsonText = "json"
)

func TestMain(m *testing.M) {
viper.Set(flags.FlagKeyringBackend, keyring.BackendMemory)
os.Exit(m.Run())
Expand All @@ -51,7 +46,7 @@ func TestContext_PrintProto(t *testing.T) {
// json
buf := &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = jsonText
ctx.OutputFormat = flags.OutputFormatJSON
err = ctx.PrintProto(hasAnimal)
require.NoError(t, err)
require.Equal(t,
Expand All @@ -61,7 +56,7 @@ func TestContext_PrintProto(t *testing.T) {
// yaml
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = text
ctx.OutputFormat = flags.OutputFormatText
err = ctx.PrintProto(hasAnimal)
require.NoError(t, err)
require.Equal(t,
Expand Down Expand Up @@ -94,7 +89,7 @@ func TestContext_PrintObjectLegacy(t *testing.T) {
// json
buf := &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = jsonText
ctx.OutputFormat = flags.OutputFormatJSON
err = ctx.PrintObjectLegacy(hasAnimal)
require.NoError(t, err)
require.Equal(t,
Expand All @@ -104,7 +99,7 @@ func TestContext_PrintObjectLegacy(t *testing.T) {
// yaml
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = text
ctx.OutputFormat = flags.OutputFormatText
err = ctx.PrintObjectLegacy(hasAnimal)
require.NoError(t, err)
require.Equal(t,
Expand All @@ -126,7 +121,7 @@ func TestContext_PrintRaw(t *testing.T) {
// json
buf := &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = jsonText
ctx.OutputFormat = flags.OutputFormatJSON
err := ctx.PrintRaw(hasAnimal)
require.NoError(t, err)
require.Equal(t,
Expand All @@ -136,7 +131,7 @@ func TestContext_PrintRaw(t *testing.T) {
// yaml
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = text
ctx.OutputFormat = flags.OutputFormatText
err = ctx.PrintRaw(hasAnimal)
require.NoError(t, err)
require.Equal(t,
Expand Down
14 changes: 9 additions & 5 deletions client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"strconv"

cmtcli "github.com/cometbft/cometbft/libs/cli"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

Expand Down Expand Up @@ -45,7 +44,7 @@ const (

// List of CLI flags
const (
FlagHome = cmtcli.HomeFlag
FlagHome = "home"
FlagKeyringDir = "keyring-dir"
FlagUseLedger = "ledger"
FlagChainID = "chain-id"
Expand Down Expand Up @@ -86,13 +85,18 @@ const (
FlagInitHeight = "initial-height"
// FlagOutput is the flag to set the output format.
// This differs from FlagOutputDocument that is used to set the output file.
FlagOutput = cmtcli.OutputFlag

FlagOutput = "output"
// Logging flags
FlagLogLevel = "log_level"
FlagLogFormat = "log_format"
)

// List of supported output formats
const (
OutputFormatJSON = "json"
OutputFormatText = "text"
)

// LineBreak can be included in a command list to provide a blank line
// to help with readability
var LineBreak = &cobra.Command{Run: func(*cobra.Command, []string) {}}
Expand All @@ -113,7 +117,7 @@ func AddQueryFlagsToCmd(cmd *cobra.Command) {
// AddTxFlagsToCmd adds common flags to a module tx command.
func AddTxFlagsToCmd(cmd *cobra.Command) {
f := cmd.Flags()
f.StringP(FlagOutput, "o", "json", "Output format (text|json)")
f.StringP(FlagOutput, "o", OutputFormatJSON, "Output format (text|json)")
f.String(FlagFrom, "", "Name or address of private key with which to sign")
f.Uint64P(FlagAccountNumber, "a", 0, "The account number of the signing account (offline mode only)")
f.Uint64P(FlagSequence, "s", 0, "The sequence number of the signing account (offline mode only)")
Expand Down
4 changes: 2 additions & 2 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf

func printCreate(cmd *cobra.Command, k *keyring.Record, showMnemonic bool, mnemonic, outputFormat string) error {
switch outputFormat {
case OutputFormatText:
case flags.OutputFormatText:
cmd.PrintErrln()
if err := printKeyringRecord(cmd.OutOrStdout(), k, MkAccKeyOutput, outputFormat); err != nil {
return err
Expand All @@ -314,7 +314,7 @@ func printCreate(cmd *cobra.Command, k *keyring.Record, showMnemonic bool, mnemo
return fmt.Errorf("failed to print mnemonic: %v", err)
}
}
case OutputFormatJSON:
case flags.OutputFormatJSON:
out, err := MkAccKeyOutput(k)
if err != nil {
return err
Expand Down
5 changes: 2 additions & 3 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"io"
"testing"

"github.com/cometbft/cometbft/libs/cli"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -54,7 +53,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
fmt.Sprintf("--%s=0", flagAccount),
fmt.Sprintf("--%s=0", flagIndex),
fmt.Sprintf("--%s=330", flagCoinType),
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagOutput, flags.OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
Expand Down Expand Up @@ -104,7 +103,7 @@ func Test_runAddCmdLedger(t *testing.T) {
cmd.SetArgs([]string{
"keyname1",
fmt.Sprintf("--%s=true", flags.FlagUseLedger),
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagOutput, flags.OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type),
fmt.Sprintf("--%s=%d", flagCoinType, sdk.CoinType),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
Expand Down
11 changes: 5 additions & 6 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"testing"

"github.com/cometbft/cometbft/libs/cli"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -43,7 +42,7 @@ func Test_runAddCmdBasic(t *testing.T) {
cmd.SetArgs([]string{
"keyname1",
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagOutput, flags.OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
Expand All @@ -56,7 +55,7 @@ func Test_runAddCmdBasic(t *testing.T) {
cmd.SetArgs([]string{
"keyname2",
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagOutput, flags.OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
Expand All @@ -70,7 +69,7 @@ func Test_runAddCmdBasic(t *testing.T) {
cmd.SetArgs([]string{
"keyname4",
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagOutput, flags.OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
Expand All @@ -82,7 +81,7 @@ func Test_runAddCmdBasic(t *testing.T) {
"keyname5",
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=true", flags.FlagDryRun),
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagOutput, flags.OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type),
})

Expand Down Expand Up @@ -245,7 +244,7 @@ func TestAddRecoverFileBackend(t *testing.T) {
cmd.SetArgs([]string{
"keyname1",
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagOutput, flags.OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendFile),
fmt.Sprintf("--%s", flagRecover),
Expand Down
8 changes: 4 additions & 4 deletions client/keys/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"io"
"strings"

"github.com/cometbft/cometbft/libs/cli"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"

"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
)
Expand Down Expand Up @@ -98,7 +98,7 @@ func doParseKey(cmd *cobra.Command, config *sdk.Config, args []string) error {
return errors.New("couldn't parse empty input")
}

output, _ := cmd.Flags().GetString(cli.OutputFlag)
output, _ := cmd.Flags().GetString(flags.FlagOutput)
if !(runFromBech32(outstream, addr, output) || runFromHex(config, outstream, addr, output)) {
return errors.New("couldn't find valid bech32 nor hex data")
}
Expand Down Expand Up @@ -137,10 +137,10 @@ func displayParseKeyInfo(w io.Writer, stringer fmt.Stringer, output string) {
)

switch output {
case OutputFormatText:
case flags.OutputFormatText:
out, err = yaml.Marshal(&stringer)

case OutputFormatJSON:
case flags.OutputFormatJSON:
out, err = json.Marshal(&stringer)
}

Expand Down
3 changes: 1 addition & 2 deletions client/keys/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keys

import (
"github.com/cometbft/cometbft/libs/cli"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -52,7 +51,7 @@ The pass backend requires GnuPG: https://gnupg.org/
)

cmd.PersistentFlags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
cmd.PersistentFlags().String(cli.OutputFlag, "text", "Output format (text|json)")
cmd.PersistentFlags().String(flags.FlagOutput, "text", "Output format (text|json)")
flags.AddKeyringFlags(cmd.PersistentFlags())

return cmd
Expand Down
6 changes: 3 additions & 3 deletions client/keys/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"errors"
"fmt"

"github.com/cometbft/cometbft/libs/cli"
"github.com/spf13/cobra"

errorsmod "cosmossdk.io/errors"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/ledger"
Expand Down Expand Up @@ -97,7 +97,7 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
isShowDevice, _ := cmd.Flags().GetBool(FlagDevice)

isOutputSet := false
tmp := cmd.Flag(cli.OutputFlag)
tmp := cmd.Flag(flags.FlagOutput)
if tmp != nil {
isOutputSet = tmp.Changed
}
Expand All @@ -117,7 +117,7 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
}

if isOutputSet {
clientCtx.OutputFormat, _ = cmd.Flags().GetString(cli.OutputFlag)
clientCtx.OutputFormat, _ = cmd.Flags().GetString(flags.FlagOutput)
}

switch {
Expand Down
15 changes: 5 additions & 10 deletions client/keys/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@ import (

"sigs.k8s.io/yaml"

"github.com/cosmos/cosmos-sdk/client/flags"
cryptokeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
)

// available output formats.
const (
OutputFormatText = "text"
OutputFormatJSON = "json"
)

type bechKeyOutFn func(k *cryptokeyring.Record) (KeyOutput, error)

func printKeyringRecord(w io.Writer, k *cryptokeyring.Record, bechKeyOut bechKeyOutFn, output string) error {
Expand All @@ -25,12 +20,12 @@ func printKeyringRecord(w io.Writer, k *cryptokeyring.Record, bechKeyOut bechKey
}

switch output {
case OutputFormatText:
case flags.OutputFormatText:
if err := printTextRecords(w, []KeyOutput{ko}); err != nil {
return err
}

case OutputFormatJSON:
case flags.OutputFormatJSON:
out, err := json.Marshal(ko)
if err != nil {
return err
Expand All @@ -51,12 +46,12 @@ func printKeyringRecords(w io.Writer, records []*cryptokeyring.Record, output st
}

switch output {
case OutputFormatText:
case flags.OutputFormatText:
if err := printTextRecords(w, kos); err != nil {
return err
}

case OutputFormatJSON:
case flags.OutputFormatJSON:
out, err := json.Marshal(kos)
if err != nil {
return err
Expand Down
Loading

0 comments on commit c1974e4

Please sign in to comment.