Skip to content

Commit

Permalink
Add output flag for query sub commands results printed to console. (#…
Browse files Browse the repository at this point in the history
…1281)

* output json for query cmd's  balance & clients-expirations
* print proper json instead of bytes for headers command
* Add output flag . Use legacy,json options
* Update according to reviews
  • Loading branch information
vimystic authored Sep 13, 2023
1 parent 4d47b66 commit 49290ac
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 35 deletions.
9 changes: 9 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
flagDstClientID = "dst-client-id"
flagSrcConnID = "src-connection-id"
flagDstConnID = "dst-connection-id"
flagOutput = "output"
)

const (
Expand Down Expand Up @@ -381,3 +382,11 @@ func OverwriteConfigFlag(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
}
return cmd
}

func addOutputFlag(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
cmd.Flags().StringP(flagOutput, "o", "legacy", "Specify the console output format. Can be 'legacy' or 'json'.")
if err := v.BindPFlag(flagOutput, cmd.Flags().Lookup(flagOutput)); err != nil {
panic(err)
}
return cmd
}
121 changes: 87 additions & 34 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import (
"github.com/spf13/cobra"
)

const (
formatJson = "json"
formatLegacy = "legacy"
)

// queryCmd represents the chain command
func queryCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -63,7 +68,7 @@ func feegrantQueryCmd(a *appState) *cobra.Command {
cmd.AddCommand(
feegrantBasicGrantsCmd(a),
)

cmd = addOutputFlag(a.viper, cmd)
return cmd
}

Expand Down Expand Up @@ -99,7 +104,7 @@ $ %s q ibc-denoms ibc-0`,
return nil
},
}

cmd = addOutputFlag(a.viper, cmd)
return cmd
}

Expand Down Expand Up @@ -127,7 +132,7 @@ $ %s q denom-trace osmosis 9BBA9A1C257E971E38C1422780CE6F0B0686F0A3085E2D61118D9
return nil
},
}

cmd = addOutputFlag(a.viper, cmd)
return cmd
}

Expand Down Expand Up @@ -161,7 +166,7 @@ $ %s q tx ibc-0 A5DF8D272F1C451CFF92BA6C41942C4D29B5CF180279439ED6AB038282F956BE
return nil
},
}

cmd = addOutputFlag(a.viper, cmd)
return cmd
}

Expand Down Expand Up @@ -213,7 +218,9 @@ $ %s q txs ibc-0 "message.action=transfer"`,
},
}

return paginationFlags(a.viper, cmd, "txs")
cmd = addOutputFlag(a.viper, cmd)
cmd = paginationFlags(a.viper, cmd, "txs")
return cmd
}

func queryBalanceCmd(a *appState) *cobra.Command {
Expand Down Expand Up @@ -257,12 +264,34 @@ $ %s query balance ibc-0 testkey`,
return err
}

fmt.Fprintf(cmd.OutOrStdout(), "address {%s} balance {%s} \n", addr, coins)
// Create a map to hold the data
data := map[string]string{
"address": addr,
"balance": coins.String(),
}

// Convert the map to a JSON string
jsonOutput, err := json.Marshal(data)
if err != nil {
return err
}

output, _ := cmd.Flags().GetString(flagOutput)
switch output {
case formatJson:
fmt.Fprint(cmd.OutOrStdout(), string(jsonOutput))
case formatLegacy:
fallthrough
default:
fmt.Fprintf(cmd.OutOrStdout(), "address {%s} balance {%s} \n", addr, coins)
}
return nil
},
}

return ibcDenomFlags(a.viper, cmd)
cmd = addOutputFlag(a.viper, cmd)
cmd = ibcDenomFlags(a.viper, cmd)
return cmd
}

func queryHeaderCmd(a *appState) *cobra.Command {
Expand Down Expand Up @@ -309,11 +338,21 @@ $ %s query header ibc-0 1400`,
return err
}

fmt.Fprintln(cmd.OutOrStdout(), s)
output, _ := cmd.Flags().GetString(flagOutput)
switch output {
case formatJson:
fmt.Fprintln(cmd.OutOrStdout(), string(s))
case formatLegacy:
fallthrough
default:
fmt.Fprintln(cmd.OutOrStdout(), s)
}

return nil
},
}

cmd = addOutputFlag(a.viper, cmd)
return cmd
}

Expand Down Expand Up @@ -355,7 +394,7 @@ $ %s q node-state ibc-1`,
return nil
},
}

cmd = addOutputFlag(a.viper, cmd)
return cmd
}

Expand Down Expand Up @@ -406,8 +445,9 @@ $ %s query client ibc-0 ibczeroclient --height 1205`,
return nil
},
}

return heightFlag(a.viper, cmd)
cmd = addOutputFlag(a.viper, cmd)
cmd = heightFlag(a.viper, cmd)
return cmd
}

func queryClientsCmd(a *appState) *cobra.Command {
Expand Down Expand Up @@ -451,8 +491,9 @@ $ %s query clients ibc-2 --offset 2 --limit 30`,
return nil
},
}

return paginationFlags(a.viper, cmd, "client states")
cmd = addOutputFlag(a.viper, cmd)
cmd = paginationFlags(a.viper, cmd, "client states")
return cmd
}

func queryConnections(a *appState) *cobra.Command {
Expand Down Expand Up @@ -498,7 +539,9 @@ $ %s q conns ibc-1`,
},
}

return paginationFlags(a.viper, cmd, "connections on a network")
cmd = addOutputFlag(a.viper, cmd)
cmd = paginationFlags(a.viper, cmd, "connections on a network")
return cmd
}

func queryConnectionsUsingClient(a *appState) *cobra.Command {
Expand Down Expand Up @@ -551,7 +594,9 @@ $ %s query client-connections ibc-0 ibczeroclient --height 1205`,
},
}

return heightFlag(a.viper, cmd)
cmd = addOutputFlag(a.viper, cmd)
cmd = heightFlag(a.viper, cmd)
return cmd
}

func queryConnection(a *appState) *cobra.Command {
Expand Down Expand Up @@ -595,7 +640,7 @@ $ %s q conn ibc-1 ibconeconn`,
return nil
},
}

cmd = addOutputFlag(a.viper, cmd)
return cmd
}

Expand Down Expand Up @@ -644,7 +689,9 @@ $ %s query connection-channels ibc-2 ibcconnection2 --offset 2 --limit 30`,
},
}

return paginationFlags(a.viper, cmd, "channels associated with a connection")
cmd = addOutputFlag(a.viper, cmd)
cmd = paginationFlags(a.viper, cmd, "channels associated with a connection")
return cmd
}

func queryChannel(a *appState) *cobra.Command {
Expand Down Expand Up @@ -697,7 +744,9 @@ $ %s query channel ibc-2 ibctwochannel transfer --height 1205`,
},
}

return heightFlag(a.viper, cmd)
cmd = addOutputFlag(a.viper, cmd)
cmd = heightFlag(a.viper, cmd)
return cmd
}

// chanExtendedInfo is an intermediate type for holding additional useful
Expand Down Expand Up @@ -917,7 +966,9 @@ $ %s query channels ibc-0 ibc-2`,
},
}

return paginationFlags(a.viper, cmd, "channels on a network")
cmd = addOutputFlag(a.viper, cmd)
cmd = paginationFlags(a.viper, cmd, "channels on a network")
return cmd
}

func queryPacketCommitment(a *appState) *cobra.Command {
Expand Down Expand Up @@ -959,7 +1010,7 @@ $ %s q packet-commit ibc-1 ibconechannel transfer 31`,
return nil
},
}

cmd = addOutputFlag(a.viper, cmd)
return cmd
}

Expand Down Expand Up @@ -1012,7 +1063,7 @@ $ %s query unrelayed-pkts demo-path channel-0`,
return nil
},
}

cmd = addOutputFlag(a.viper, cmd)
return cmd
}

Expand Down Expand Up @@ -1064,7 +1115,7 @@ $ %s query unrelayed-acks demo-path channel-0`,
return nil
},
}

cmd = addOutputFlag(a.viper, cmd)
return cmd
}

Expand Down Expand Up @@ -1105,24 +1156,26 @@ $ %s query clients-expiration demo-path`,
return errDst
}

// if only the src light client is found, just print info for source light client
if errSrc == nil && errDst != nil {
fmt.Fprintln(cmd.OutOrStdout(), relayer.SPrintClientExpiration(c[src], srcExpiration, srcClientInfo))
return nil
}
output, _ := cmd.Flags().GetString(flagOutput)

// if only the dst light client is found, just print info for destination light client
if errDst == nil && errSrc != nil {
fmt.Fprintln(cmd.OutOrStdout(), relayer.SPrintClientExpiration(c[dst], dstExpiration, dstClientInfo))
return nil
srcClientExpiration := relayer.SPrintClientExpiration(c[src], srcExpiration, srcClientInfo)
dstClientExpiration := relayer.SPrintClientExpiration(c[dst], dstExpiration, dstClientInfo)

if output == formatJson {
srcClientExpiration = relayer.SPrintClientExpirationJson(c[src], srcExpiration, srcClientInfo)
dstClientExpiration = relayer.SPrintClientExpirationJson(c[dst], dstExpiration, dstClientInfo)
}

fmt.Fprintln(cmd.OutOrStdout(), relayer.SPrintClientExpiration(c[src], srcExpiration, srcClientInfo))
fmt.Fprintln(cmd.OutOrStdout(), relayer.SPrintClientExpiration(c[dst], dstExpiration, dstClientInfo))
if errSrc == nil {
fmt.Fprintln(cmd.OutOrStdout(), srcClientExpiration)
}

if errDst == nil {
fmt.Fprintln(cmd.OutOrStdout(), dstClientExpiration)
}
return nil
},
}

cmd = addOutputFlag(a.viper, cmd)
return cmd
}
36 changes: 35 additions & 1 deletion relayer/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package relayer

import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -308,12 +310,44 @@ func SPrintClientExpiration(chain *Chain, expiration time.Time, clientInfo Clien
status = "GOOD"
}

return fmt.Sprintf(`
legacyOutput := fmt.Sprintf(`
client: %s (%s)
HEALTH: %s
TIME: %s (%s)
LAST UPDATE HEIGHT: %d
TRUSTING PERIOD: %s
`,
chain.ClientID(), chain.ChainID(), status, expirationFormatted, remainingTime.Round(time.Second), clientInfo.LatestHeight.GetRevisionHeight(), clientInfo.TrustingPeriod.String())

return legacyOutput

}

// Returns clientExpiration data in JSON format.
func SPrintClientExpirationJson(chain *Chain, expiration time.Time, clientInfo ClientStateInfo) string {
now := time.Now()
remainingTime := expiration.Sub(now)
expirationFormatted := expiration.Format(time.RFC822)

var status string
if remainingTime <= 0 {
status = "EXPIRED"
} else {
status = "GOOD"
}

data := map[string]string{
"client": fmt.Sprintf("%s (%s)", chain.ClientID(), chain.ChainID()),
"HEALTH": status,
"TIME": fmt.Sprintf("%s (%s)", expirationFormatted, remainingTime.Round(time.Second)),
"LAST UPDATE HEIGHT": strconv.FormatUint(clientInfo.LatestHeight.GetRevisionHeight(), 10),
"TRUSTING PERIOD": clientInfo.TrustingPeriod.String(),
}

jsonOutput, err := json.Marshal(data)
if err != nil {
jsonOutput = []byte{}
}

return string(jsonOutput)
}
Loading

0 comments on commit 49290ac

Please sign in to comment.