59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package rpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
cmtjson "github.com/cometbft/cometbft/libs/json"
|
|
coretypes "github.com/cometbft/cometbft/rpc/core/types"
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
)
|
|
|
|
// StatusCommand returns the command to return the status of the network.
|
|
func StatusCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "status",
|
|
Short: "Query remote node for status",
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
clientCtx, err := client.GetClientQueryContext(cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
status, err := getNodeStatus(clientCtx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
output, err := cmtjson.Marshal(status)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// In order to maintain backwards compatibility, the default json format output
|
|
outputFormat, _ := cmd.Flags().GetString(flags.FlagOutput)
|
|
if outputFormat == flags.OutputFormatJSON {
|
|
clientCtx = clientCtx.WithOutputFormat(flags.OutputFormatJSON)
|
|
}
|
|
|
|
return clientCtx.PrintRaw(output)
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
|
|
cmd.Flags().StringP(flags.FlagOutput, "o", "json", "Output format (text|json)")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func getNodeStatus(clientCtx client.Context) (*coretypes.ResultStatus, error) {
|
|
node, err := clientCtx.GetNode()
|
|
if err != nil {
|
|
return &coretypes.ResultStatus{}, err
|
|
}
|
|
|
|
return node.Status(context.Background())
|
|
}
|