Skip to content

Commit

Permalink
feat(go/cli): pull all cosmos-sdk commands into repo
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <troian.ap@gmail.com>
  • Loading branch information
troian committed Sep 8, 2024
1 parent ca833df commit 13f27c6
Show file tree
Hide file tree
Showing 91 changed files with 16,972 additions and 1,123 deletions.
30 changes: 16 additions & 14 deletions go/cli/audit_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"github.com/spf13/cobra"

sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"

cflags "pkg.akt.dev/go/cli/flags"
types "pkg.akt.dev/go/node/audit/v1"
)

func GetAuditQueryCmd() *cobra.Command {
func GetQueryAuditCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Audit query commands",
Expand All @@ -21,17 +21,18 @@ func GetAuditQueryCmd() *cobra.Command {
}

cmd.AddCommand(
cmdAuditGetProviders(),
cmdAuditGetProvider(),
GetAuditProvidersCmd(),
GetAuditProviderCmd(),
)

return cmd
}

func cmdAuditGetProviders() *cobra.Command {
func GetAuditProvidersCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "Query for all providers",
Use: "list",
Short: "Query for all providers",
PersistentPreRunE: QueryPersistentPreRunE,
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
cl := MustQueryClientFromContext(ctx)
Expand All @@ -54,17 +55,18 @@ func cmdAuditGetProviders() *cobra.Command {
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "providers")
cflags.AddQueryFlagsToCmd(cmd)
cflags.AddPaginationFlagsToCmd(cmd, "providers")

return cmd
}

func cmdAuditGetProvider() *cobra.Command {
func GetAuditProviderCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get [owner address] [auditor address]",
Short: "Query provider",
Args: cobra.RangeArgs(1, 2),
Use: "get [owner address] [auditor address]",
Short: "Query provider",
Args: cobra.RangeArgs(1, 2),
PersistentPreRunE: QueryPersistentPreRunE,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
cl := MustQueryClientFromContext(ctx)
Expand Down Expand Up @@ -103,7 +105,7 @@ func cmdAuditGetProvider() *cobra.Command {
},
}

flags.AddQueryFlagsToCmd(cmd)
cflags.AddQueryFlagsToCmd(cmd)

return cmd
}
33 changes: 17 additions & 16 deletions go/cli/audit_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/spf13/cobra"

sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"

cflags "pkg.akt.dev/go/cli/flags"
Expand All @@ -16,8 +15,8 @@ import (
attrtypes "pkg.akt.dev/go/node/types/attributes/v1"
)

// GetAuditTxCmd returns the transaction commands for audit module
func GetAuditTxCmd() *cobra.Command {
// GetTxAuditCmd returns the transaction commands for audit module
func GetTxAuditCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Audit transaction subcommands",
Expand All @@ -26,31 +25,32 @@ func GetAuditTxCmd() *cobra.Command {
}

cmd.AddCommand(
cmdAttributes(),
GetTxAuditAttributesCmd(),
)

return cmd
}

func cmdAttributes() *cobra.Command {
func GetTxAuditAttributesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "attr",
Short: "Manage provider attributes",
}

cmd.AddCommand(
cmdCreateProviderAttributes(),
cmdDeleteProviderAttributes(),
CmdCreateProviderAttributes(),
CmdDeleteProviderAttributes(),
)

return cmd
}

func cmdCreateProviderAttributes() *cobra.Command {
func CmdCreateProviderAttributes() *cobra.Command {
cmd := &cobra.Command{
Use: "create [provider]",
Short: "Create/update provider attributes",
Args: cobra.MinimumNArgs(1),
Use: "create [provider]",
Short: "Create/update provider attributes",
Args: cobra.MinimumNArgs(1),
PersistentPreRunE: TxPersistentPreRunE,
RunE: func(cmd *cobra.Command, args []string) error {
if ((len(args) - 1) % 2) != 0 {
return fmt.Errorf("attributes must be provided as pairs")
Expand Down Expand Up @@ -98,11 +98,12 @@ func cmdCreateProviderAttributes() *cobra.Command {
return cmd
}

func cmdDeleteProviderAttributes() *cobra.Command {
func CmdDeleteProviderAttributes() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [provider]",
Short: "Delete provider attributes",
Args: cobra.MinimumNArgs(1),
Use: "delete [provider]",
Short: "Delete provider attributes",
Args: cobra.MinimumNArgs(1),
PersistentPreRunE: TxPersistentPreRunE,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
cl := MustClientFromContext(ctx)
Expand Down Expand Up @@ -145,7 +146,7 @@ func cmdDeleteProviderAttributes() *cobra.Command {
func setCmdProviderFlags(cmd *cobra.Command) {
cflags.AddTxFlagsToCmd(cmd)

if err := cmd.MarkFlagRequired(flags.FlagFrom); err != nil {
if err := cmd.MarkFlagRequired(cflags.FlagFrom); err != nil {
panic(err.Error())
}
}
Expand Down
103 changes: 103 additions & 0 deletions go/cli/auth_encode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package cli_test

// import (
// "context"
// "encoding/base64"
// "testing"
//
// "github.com/stretchr/testify/require"
//
// "cosmossdk.io/depinject"
// "github.com/cosmos/cosmos-sdk/client"
// "github.com/cosmos/cosmos-sdk/codec"
// "github.com/cosmos/cosmos-sdk/testutil"
// sdk "github.com/cosmos/cosmos-sdk/types"
// authtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutil"
//
// "pkg.akt.dev/go/cli"
// )
//
// func TestGetCommandEncode(t *testing.T) {
// var (
// txCfg client.TxConfig
// legacyAmino *codec.LegacyAmino
// codec codec.Codec
// )
//
// err := depinject.Inject(
// authtestutil.AppConfig,
// &txCfg,
// &legacyAmino,
// &codec,
// )
// require.NoError(t, err)
//
// cmd := cli.GetEncodeCommand()
// _ = testutil.ApplyMockIODiscardOutErr(cmd)
//
// // Build a test transaction
// builder := txCfg.NewTxBuilder()
// builder.SetGasLimit(50000)
// builder.SetFeeAmount(sdk.Coins{sdk.NewInt64Coin("atom", 150)})
// builder.SetMemo("foomemo")
// jsonEncoded, err := txCfg.TxJSONEncoder()(builder.GetTx())
// require.NoError(t, err)
//
// txFile := testutil.WriteToNewTempFile(t, string(jsonEncoded))
// txFileName := txFile.Name()
//
// ctx := context.Background()
// clientCtx := client.Context{}.
// WithTxConfig(txCfg).
// WithCodec(codec)
// ctx = context.WithValue(ctx, cli.ClientContextKey, &clientCtx)
//
// cmd.SetArgs([]string{txFileName})
// err = cmd.ExecuteContext(ctx)
// require.NoError(t, err)
// }
//
// func TestGetCommandDecode(t *testing.T) {
// var (
// txCfg client.TxConfig
// legacyAmino *codec.LegacyAmino
// codec codec.Codec
// )
//
// err := depinject.Inject(
// authtestutil.AppConfig,
// &txCfg,
// &legacyAmino,
// &codec,
// )
// require.NoError(t, err)
//
// clientCtx := client.Context{}.
// WithTxConfig(txCfg).
// WithCodec(codec)
//
// cmd := cli.GetDecodeCommand()
// _ = testutil.ApplyMockIODiscardOutErr(cmd)
//
// clientCtx = clientCtx.WithTxConfig(txCfg)
//
// // Build a test transaction
// builder := txCfg.NewTxBuilder()
// builder.SetGasLimit(50000)
// builder.SetFeeAmount(sdk.Coins{sdk.NewInt64Coin("atom", 150)})
// builder.SetMemo("foomemo")
//
// // Encode transaction
// txBytes, err := clientCtx.TxConfig.TxEncoder()(builder.GetTx())
// require.NoError(t, err)
//
// // Convert the transaction into base64 encoded string
// base64Encoded := base64.StdEncoding.EncodeToString(txBytes)
//
// ctx := context.Background()
// ctx = context.WithValue(ctx, cli.ClientContextKey, &clientCtx)
//
// // Execute the command
// cmd.SetArgs([]string{base64Encoded})
// require.NoError(t, cmd.ExecuteContext(ctx))
// }
16 changes: 8 additions & 8 deletions go/cli/auth_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ func SignTxWithSignerAddress(txFactory tx.Factory, clientCtx client.Context, add

// ReadTxFromFile and decode a StdTx from the given filename. Can pass "-" to read from stdin.
func ReadTxFromFile(ctx client.Context, filename string) (tx sdk.Tx, err error) {
var bytes []byte
var data []byte

if filename == "-" {
bytes, err = io.ReadAll(os.Stdin)
data, err = io.ReadAll(os.Stdin)
} else {
bytes, err = os.ReadFile(filename)
data, err = os.ReadFile(filename)
}

if err != nil {
return
}

return ctx.TxConfig.TxJSONDecoder()(bytes)
return ctx.TxConfig.TxJSONDecoder()(data)
}

// ReadTxsFromInput reads multiples txs from the given filename(s). Can pass "-" to read from stdin.
Expand All @@ -119,12 +119,12 @@ func ReadTxsFromInput(txCfg client.TxConfig, filenames ...string) (scanner *Batc
if filenames[0] != "-" {
buf := new(bytes.Buffer)
for _, f := range filenames {
bytes, err := os.ReadFile(filepath.Clean(f))
data, err := os.ReadFile(filepath.Clean(f))
if err != nil {
return nil, fmt.Errorf("couldn't read %s: %w", f, err)
}

if _, err := buf.WriteString(string(bytes)); err != nil {
if _, err := buf.WriteString(string(data)); err != nil {
return nil, fmt.Errorf("couldn't write to merged file: %w", err)
}
}
Expand Down Expand Up @@ -161,8 +161,8 @@ func (bs *BatchScanner) Scan() bool {
return false
}

tx, err := bs.cfg.TxJSONDecoder()(bs.Bytes())
bs.theTx = tx
txb, err := bs.cfg.TxJSONDecoder()(bs.Bytes())
bs.theTx = txb
if err != nil && bs.unmarshalErr == nil {
bs.unmarshalErr = err
return false
Expand Down
Loading

0 comments on commit 13f27c6

Please sign in to comment.