Skip to content

Commit 408484e

Browse files
authored
Merge PR #3503: CLI flags to args
1 parent fc85167 commit 408484e

File tree

22 files changed

+252
-298
lines changed

22 files changed

+252
-298
lines changed

client/utils/utils.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package utils
33
import (
44
"bytes"
55
"fmt"
6-
"io"
76
"os"
87

98
"github.com/cosmos/cosmos-sdk/client"
@@ -28,6 +27,14 @@ func (gr GasEstimateResponse) String() string {
2827
return fmt.Sprintf("gas estimate: %d", gr.GasEstimate)
2928
}
3029

30+
// GenerateOrBroadcastMsgs respects CLI flags and outputs a message
31+
func GenerateOrBroadcastMsgs(cliCtx context.CLIContext, txBldr authtxb.TxBuilder, msgs []sdk.Msg, offline bool) error {
32+
if cliCtx.GenerateOnly {
33+
return PrintUnsignedStdTx(txBldr, cliCtx, msgs, offline)
34+
}
35+
return CompleteAndBroadcastTxCLI(txBldr, cliCtx, msgs)
36+
}
37+
3138
// CompleteAndBroadcastTxCLI implements a utility function that facilitates
3239
// sending a series of messages in a signed transaction given a TxBuilder and a
3340
// QueryContext. It ensures that the account exists, has a proper number and
@@ -103,7 +110,7 @@ func CalculateGas(queryFunc func(string, common.HexBytes) ([]byte, error), cdc *
103110

104111
// PrintUnsignedStdTx builds an unsigned StdTx and prints it to os.Stdout.
105112
// Don't perform online validation or lookups if offline is true.
106-
func PrintUnsignedStdTx(w io.Writer, txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg, offline bool) (err error) {
113+
func PrintUnsignedStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg, offline bool) (err error) {
107114
var stdTx auth.StdTx
108115
if offline {
109116
stdTx, err = buildUnsignedStdTxOffline(txBldr, cliCtx, msgs)
@@ -115,7 +122,7 @@ func PrintUnsignedStdTx(w io.Writer, txBldr authtxb.TxBuilder, cliCtx context.CL
115122
}
116123
json, err := cliCtx.Codec.MarshalJSON(stdTx)
117124
if err == nil {
118-
fmt.Fprintf(w, "%s\n", json)
125+
fmt.Fprintf(cliCtx.Output, "%s\n", json)
119126
}
120127
return
121128
}

cmd/gaia/cli_test/cli_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,7 @@ func TestGaiaCLIQueryTxPagination(t *testing.T) {
533533
barAddr := f.KeyAddress(keyBar)
534534

535535
for i := 1; i <= 30; i++ {
536-
success := executeWrite(t, fmt.Sprintf(
537-
"gaiacli tx send %s --amount=%dfootoken --to=%s --from=foo",
538-
f.Flags(), i, barAddr), app.DefaultKeyPass)
536+
success, _, _ := f.TxSend(keyFoo, barAddr, sdk.NewInt64Coin(fooDenom, int64(i)))
539537
require.True(t, success)
540538
tests.WaitForNextNBlocksTM(1, f.Port)
541539
}

cmd/gaia/cli_test/test_helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func (f *Fixtures) CLIConfig(key, value string, flags ...string) {
269269

270270
// TxSend is gaiacli tx send
271271
func (f *Fixtures) TxSend(from string, to sdk.AccAddress, amount sdk.Coin, flags ...string) (bool, string, string) {
272-
cmd := fmt.Sprintf("gaiacli tx send %v --amount=%s --to=%s --from=%s", f.Flags(), amount, to, from)
272+
cmd := fmt.Sprintf("gaiacli tx send %s %s %v --from=%s", to, amount, f.Flags(), from)
273273
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), app.DefaultKeyPass)
274274
}
275275

@@ -308,7 +308,7 @@ func (f *Fixtures) TxStakingCreateValidator(from, consPubKey string, amount sdk.
308308

309309
// TxStakingUnbond is gaiacli tx staking unbond
310310
func (f *Fixtures) TxStakingUnbond(from, shares string, validator sdk.ValAddress, flags ...string) bool {
311-
cmd := fmt.Sprintf("gaiacli tx staking unbond %v --from=%s --validator=%s --shares-amount=%v", f.Flags(), from, validator, shares)
311+
cmd := fmt.Sprintf("gaiacli tx staking unbond %s %v --from=%s %v", validator, shares, from, f.Flags())
312312
return executeWrite(f.T, addFlags(cmd, flags), app.DefaultKeyPass)
313313
}
314314

cmd/gaia/init/gentx.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ following delegation and commission default parameters:
128128

129129
// write the unsigned transaction to the buffer
130130
w := bytes.NewBuffer([]byte{})
131-
if err := utils.PrintUnsignedStdTx(w, txBldr, cliCtx, []sdk.Msg{msg}, true); err != nil {
131+
cliCtx = cliCtx.WithOutput(w)
132+
if err = utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, true); err != nil {
132133
return err
133134
}
134135

@@ -214,7 +215,7 @@ func prepareFlagsForTxCreateValidator(config *cfg.Config, nodeID, ip, chainID st
214215
viper.Set(cli.FlagNodeID, nodeID) // --node-id
215216
viper.Set(cli.FlagIP, ip) // --ip
216217
viper.Set(cli.FlagPubKey, sdk.MustBech32ifyConsPub(valPubKey)) // --pubkey
217-
viper.Set(cli.FlagGenesisFormat, true) // --genesis-format
218+
viper.Set(client.FlagGenerateOnly, true) // --genesis-format
218219
viper.Set(cli.FlagMoniker, config.Moniker) // --moniker
219220
if config.Moniker == "" {
220221
viper.Set(cli.FlagMoniker, viper.GetString(client.FlagName))

docs/gaia/gaiacli.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,9 @@ When you query an account balance with zero tokens, you will get this error: `No
179179
The following command could be used to send coins from one account to another:
180180

181181
```bash
182-
gaiacli tx send \
183-
--amount=10faucetToken \
182+
gaiacli tx send <destination_cosmos> 10faucetToken \
184183
--chain-id=<chain_id> \
185184
--from=<key_name> \
186-
--to=<destination_cosmos>
187185
```
188186

189187
::: warning Note
@@ -213,23 +211,19 @@ You can simulate a transaction without actually broadcasting it by appending the
213211
`--dry-run` flag to the command line:
214212

215213
```bash
216-
gaiacli tx send \
217-
--amount=10faucetToken \
214+
gaiacli tx send <destination_cosmosaccaddr> 10faucetToken \
218215
--chain-id=<chain_id> \
219216
--from=<key_name> \
220-
--to=<destination_cosmosaccaddr> \
221217
--dry-run
222218
```
223219

224220
Furthermore, you can build a transaction and print its JSON format to STDOUT by
225221
appending `--generate-only` to the list of the command line arguments:
226222

227223
```bash
228-
gaiacli tx send \
229-
--amount=10faucetToken \
224+
gaiacli tx send <destination_cosmosaccaddr> 10faucetToken \
230225
--chain-id=<chain_id> \
231226
--from=<key_name> \
232-
--to=<destination_cosmosaccaddr> \
233227
--generate-only > unsignedSendTx.json
234228
```
235229

@@ -722,10 +716,8 @@ The first step to create a multisig transaction is to initiate it on behalf
722716
of the multisig address created above:
723717

724718
```bash
725-
gaiacli tx send \
719+
gaiacli tx send cosmos1570v2fq3twt0f0x02vhxpuzc9jc4yl30q2qned 10stake \
726720
--from=<multisig_address> \
727-
--to=cosmos1570v2fq3twt0f0x02vhxpuzc9jc4yl30q2qned \
728-
--amount=10stake \
729721
--generate-only > unsignedTx.json
730722
```
731723

docs/gaia/ledger.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ NAME: TYPE: ADDRESS: PUBKEY:
2323
This key will only be accessible while the Ledger is plugged in and unlocked. To send some coins with this key, run the following:
2424
2525
```bash
26-
$ gaiacli tx send --from { .Key.Name } --to { .Destination.AccAddr } --chain-id=gaia-7000
26+
$ gaiacli tx send { .Destination.AccAddr } 10stake --from { .Key.Name } --chain-id=gaia-7000
2727
```
2828
2929
You will be asked to review and confirm the transaction on the Ledger. Once you do this you should see the result in the console! Now you can use your Ledger to manage your Atoms and Stake!

x/auth/client/cli/account.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,5 @@ func GetAccountCmd(storeName string, cdc *codec.Codec) *cobra.Command {
3838
return cliCtx.PrintOutput(acc)
3939
},
4040
}
41-
42-
// Add the flags here and return the command
4341
return client.GetCommands(cmd)[0]
4442
}

x/auth/client/cli/multisign.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import (
2222
// GetSignCommand returns the sign command
2323
func GetMultiSignCommand(codec *amino.Codec) *cobra.Command {
2424
cmd := &cobra.Command{
25-
Use: "multisign <file> <name> <<signature>...>",
25+
Use: "multisign [file] [name] [[signature]...]",
2626
Short: "Generate multisig signatures for transactions generated offline",
2727
Long: `Sign transactions created with the --generate-only flag that require multisig signatures.
2828
29-
Read signature(s) from <signature> file(s), generate a multisig signature compliant to the
30-
multisig key <name>, and attach it to the transaction read from <file>. Example:
29+
Read signature(s) from [signature] file(s), generate a multisig signature compliant to the
30+
multisig key [name], and attach it to the transaction read from [file]. Example:
3131
3232
gaiacli multisign transaction.json k1k2k3 k1sig.json k2sig.json k3sig.json
3333
@@ -43,8 +43,7 @@ recommended to set such parameters manually.
4343
}
4444
cmd.Flags().Bool(flagSigOnly, false, "Print only the generated signature, then exit")
4545
cmd.Flags().Bool(flagOffline, false, "Offline mode. Do not query a full node")
46-
cmd.Flags().String(flagOutfile, "",
47-
"The document will be written to the given file instead of STDOUT")
46+
cmd.Flags().String(flagOutfile, "", "The document will be written to the given file instead of STDOUT")
4847

4948
// Add the flags here and return the command
5049
return client.PostCommands(cmd)[0]

x/auth/client/cli/sign.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ const (
3030
// GetSignCommand returns the sign command
3131
func GetSignCommand(codec *amino.Codec) *cobra.Command {
3232
cmd := &cobra.Command{
33-
Use: "sign <file>",
33+
Use: "sign [file]",
3434
Short: "Sign transactions generated offline",
3535
Long: `Sign transactions created with the --generate-only flag.
36-
Read a transaction from <file>, sign it, and print its JSON encoding.
36+
Read a transaction from [file], sign it, and print its JSON encoding.
3737
3838
If the flag --signature-only flag is on, it outputs a JSON representation
3939
of the generated signature only.

x/bank/client/cli/broadcast.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
"io/ioutil"
55
"os"
6+
"strings"
67

78
"github.com/spf13/cobra"
89
amino "github.com/tendermint/go-amino"
@@ -15,11 +16,14 @@ import (
1516
// GetSignCommand returns the sign command
1617
func GetBroadcastCommand(codec *amino.Codec) *cobra.Command {
1718
cmd := &cobra.Command{
18-
Use: "broadcast <file>",
19+
Use: "broadcast [file_path]",
1920
Short: "Broadcast transactions generated offline",
20-
Long: `Broadcast transactions created with the --generate-only flag and signed with the sign command.
21-
Read a transaction from <file> and broadcast it to a node. If you supply a dash (-) argument
22-
in place of an input filename, the command reads from standard input.`,
21+
Long: strings.TrimSpace(`Broadcast transactions created with the --generate-only flag and signed with the sign command.
22+
Read a transaction from [file_path] and broadcast it to a node. If you supply a dash (-) argument
23+
in place of an input filename, the command reads from standard input.
24+
25+
$ gaiacli tx broadcast ./mytxn.json
26+
`),
2327
Args: cobra.ExactArgs(1),
2428
RunE: func(cmd *cobra.Command, args []string) (err error) {
2529
cliCtx := context.NewCLIContext().WithCodec(codec)

0 commit comments

Comments
 (0)