mesg-foundation/core

View on GitHub
x/ownership/client/cli/query.go

Summary

Maintainability
A
0 mins
Test Coverage
package cli

import (
    "fmt"

    "github.com/cosmos/cosmos-sdk/client"
    "github.com/cosmos/cosmos-sdk/client/context"
    "github.com/cosmos/cosmos-sdk/client/flags"
    "github.com/cosmos/cosmos-sdk/codec"
    "github.com/mesg-foundation/engine/ownership"
    "github.com/mesg-foundation/engine/x/ownership/internal/types"
    "github.com/spf13/cobra"
)

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
    // Group ownership queries under a subcommand
    ownershipQueryCmd := &cobra.Command{
        Use:                        types.ModuleName,
        Short:                      fmt.Sprintf("Query commands for the %s module", types.ModuleName),
        DisableFlagParsing:         true,
        SuggestionsMinimumDistance: 2,
        RunE:                       client.ValidateCmd,
    }

    ownershipQueryCmd.AddCommand(
        flags.GetCommands(
            GetCmdList(queryRoute, cdc),
        )...,
    )

    return ownershipQueryCmd
}

// GetCmdList returns command listing ownerships
func GetCmdList(queryRoute string, cdc *codec.Codec) *cobra.Command {
    return &cobra.Command{
        Use:   "list",
        Short: "Query all the ownership",
        Args:  cobra.NoArgs,
        RunE: func(cmd *cobra.Command, args []string) error {
            cliCtx := context.NewCLIContext().WithCodec(cdc)
            res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryList), nil)
            if err != nil {
                fmt.Printf("could not get ownerships\n%s\n", err.Error())
                return nil
            }

            var out []*ownership.Ownership
            cdc.MustUnmarshalJSON(res, &out)
            return cliCtx.PrintOutput(out)
        },
    }
}