Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add output flag for query sub commands results printed to console. #1281

Merged
merged 8 commits into from
Sep 13, 2023
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
}
119 changes: 84 additions & 35 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func feegrantQueryCmd(a *appState) *cobra.Command {
cmd.AddCommand(
feegrantBasicGrantsCmd(a),
)

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

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

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

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

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

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

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

Expand Down Expand Up @@ -213,7 +213,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 +259,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 "json":
vimystic marked this conversation as resolved.
Show resolved Hide resolved
fmt.Fprint(cmd.OutOrStdout(), string(jsonOutput))
case "legacy":
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 +333,21 @@ $ %s query header ibc-0 1400`,
return err
}

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

return nil
},
}

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

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

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

Expand Down Expand Up @@ -406,8 +440,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 +486,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 +534,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 +589,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 +635,7 @@ $ %s q conn ibc-1 ibconeconn`,
return nil
},
}

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

Expand Down Expand Up @@ -644,7 +684,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 +739,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 +961,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 +1005,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 +1058,7 @@ $ %s query unrelayed-pkts demo-path channel-0`,
return nil
},
}

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

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

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

Expand Down Expand Up @@ -1105,24 +1151,27 @@ $ %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)

fmt.Fprintln(cmd.OutOrStdout(), relayer.SPrintClientExpiration(c[src], srcExpiration, srcClientInfo))
fmt.Fprintln(cmd.OutOrStdout(), relayer.SPrintClientExpiration(c[dst], dstExpiration, dstClientInfo))
if output == "json" {
srcClientExpiration = relayer.SPrintClientExpirationJson(c[src], srcExpiration, srcClientInfo)
dstClientExpiration = relayer.SPrintClientExpirationJson(c[dst], dstExpiration, dstClientInfo)
}

if errSrc == nil && errDst == nil {
fmt.Fprintln(cmd.OutOrStdout(), srcClientExpiration)
fmt.Fprintln(cmd.OutOrStdout(), dstClientExpiration)
} else if errSrc == nil && errDst != nil {
fmt.Fprintln(cmd.OutOrStdout(), dstClientExpiration)
} else if errDst == nil && errSrc != nil {
fmt.Fprintln(cmd.OutOrStdout(), srcClientExpiration)
}
vimystic marked this conversation as resolved.
Show resolved Hide resolved
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