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

feat(autocli): add json file parsing support #15451

Merged
merged 15 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/groups) [#14879](https://github.com/cosmos/cosmos-sdk/pull/14879) Add `Query/Groups` query to get all the groups.
* (x/genutil,cli) [#15147](https://github.com/cosmos/cosmos-sdk/pull/15147) Add `--initial-height` flag to cli init cmd to provide `genesis.json` with user defined initial block height
* (x/gov) [#15151](https://github.com/cosmos/cosmos-sdk/pull/15151) Add `burn_vote_quorum`, `burn_proposal_deposit_prevote` and `burn_vote_veto` params to allow applications to decide if they would like to burn deposits
* (client/autocli) [#15451](https://github.com/cosmos/cosmos-sdk/pull/15451) Add support to reading a json file to AutoCli generated commands.
JeancarloBarrios marked this conversation as resolved.
Show resolved Hide resolved
JeancarloBarrios marked this conversation as resolved.
Show resolved Hide resolved


JeancarloBarrios marked this conversation as resolved.
Show resolved Hide resolved
### Improvements

Expand Down
20 changes: 19 additions & 1 deletion client/v2/autocli/flag/message_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package flag
import (
"context"
"fmt"
"io"
"os"
"regexp"

"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
Expand All @@ -11,6 +14,8 @@ import (
"cosmossdk.io/client/v2/internal/util"
)

var isJsonFileRegex = regexp.MustCompile(`\.json$`)
JeancarloBarrios marked this conversation as resolved.
Show resolved Hide resolved

type jsonMessageFlagType struct {
messageDesc protoreflect.MessageDescriptor
}
Expand Down Expand Up @@ -55,7 +60,20 @@ func (j *jsonMessageFlagValue) String() string {

func (j *jsonMessageFlagValue) Set(s string) error {
j.message = j.messageType.New().Interface()
return j.jsonUnmarshalOptions.Unmarshal([]byte(s), j.message)
var messageBytes []byte
if isJsonFileRegex.MatchString(s) {
jsonFile, err := os.Open(s)

Check failure

Code scanning / gosec

Potential file inclusion via variable

Potential file inclusion via variable
if err != nil {
return err
}
messageBytes, err = io.ReadAll(jsonFile)
if err != nil {
return err
}
} else {
messageBytes = []byte(s)
}
return j.jsonUnmarshalOptions.Unmarshal(messageBytes, j.message)
}

func (j *jsonMessageFlagValue) Type() string {
Expand Down
20 changes: 20 additions & 0 deletions client/v2/autocli/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,26 @@ func TestEverything(t *testing.T) {
assert.DeepEqual(t, conn.lastRequest, conn.lastResponse.(*testpb.EchoResponse).Request, protocmp.Transform())
}

func TestJsonFlag(t *testing.T) {
JeancarloBarrios marked this conversation as resolved.
Show resolved Hide resolved
conn := testExecCommon(t, buildModuleQueryCommand,
"echo",
"1", "abc", `{"denom":"foo","amount":"1"}`,
"--some-messages", `{"bar":"baz"}`,
"-u", "27", // shorthand
)
lastReq := conn.lastRequest.(*testpb.EchoRequest)
assert.Equal(t, uint32(27), lastReq.U32)

conn = testExecCommon(t, buildModuleQueryCommand,
"echo",
"1", "abc", `{"denom":"foo","amount":"1"}`,
"--some-messages", "testdata/some_message.json",
"-u", "27", // shorthand
)
lastReq = conn.lastRequest.(*testpb.EchoRequest)
assert.Equal(t, uint32(27), lastReq.U32)
JeancarloBarrios marked this conversation as resolved.
Show resolved Hide resolved
}

func TestOptions(t *testing.T) {
conn := testExecCommon(t, buildModuleQueryCommand,
"echo",
Expand Down
1 change: 1 addition & 0 deletions client/v2/autocli/testdata/some_message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"bar":"baz"}