mesg-foundation/core

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

Summary

Maintainability
D
1 day
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/service"
    "github.com/mesg-foundation/engine/x/service/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 service queries under a subcommand
    serviceQueryCmd := &cobra.Command{
        Use:                        types.ModuleName,
        Short:                      fmt.Sprintf("Query commands for the %s module", types.ModuleName),
        DisableFlagParsing:         true,
        SuggestionsMinimumDistance: 2,
        RunE:                       client.ValidateCmd,
    }

    serviceQueryCmd.AddCommand(
        flags.GetCommands(
            GetCmdGet(queryRoute, cdc),
            GetCmdList(queryRoute, cdc),
            GetCmdExist(queryRoute, cdc),
        )...,
    )

    return serviceQueryCmd
}

// GetCmdGet implements the get query command.
func GetCmdGet(queryRoute string, cdc *codec.Codec) *cobra.Command {
    return &cobra.Command{
        Use:   "get [hash]",
        Short: "Fetch a service by its hash",
        Args:  cobra.ExactArgs(1),
        RunE: func(cmd *cobra.Command, args []string) error {
            cliCtx := context.NewCLIContext().WithCodec(cdc)
            res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, types.QueryGet, args[0]), nil)
            if err != nil {
                fmt.Printf("could not get service\n%s\n", err.Error())
                return nil
            }

            var out *service.Service
            cdc.MustUnmarshalJSON(res, &out)
            return cliCtx.PrintOutput(out)
        },
    }
}

// GetCmdList implements the list query command.
func GetCmdList(queryRoute string, cdc *codec.Codec) *cobra.Command {
    return &cobra.Command{
        Use:   "list",
        Short: "Query all the services",
        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 list services\n%s\n", err.Error())
                return nil
            }

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

// GetCmdExist implements the exist query command.
func GetCmdExist(queryRoute string, cdc *codec.Codec) *cobra.Command {
    return &cobra.Command{
        Use:   "exist [hash]",
        Short: "Check if the service exist",
        Args:  cobra.ExactArgs(1),
        RunE: func(cmd *cobra.Command, args []string) error {
            cliCtx := context.NewCLIContext().WithCodec(cdc)
            res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, types.QueryExist, args[0]), nil)
            if err != nil {
                fmt.Printf("could not check service\n%s\n", err.Error())
                return nil
            }

            var out bool
            cdc.MustUnmarshalJSON(res, &out)
            return cliCtx.PrintOutput(out)
        },
    }
}