cosmos-sdk/x/gov/client/cli/parse.go
atheeshp 1955fc84ed
chore: change metadata field type byte to string (#11269)
## Description

ref: #10490
[comment](https://github.com/cosmos/cosmos-sdk/issues/10490#issuecomment-1047110188)



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-03-01 08:35:37 +00:00

122 lines
2.7 KiB
Go

package cli
import (
"encoding/json"
"fmt"
"os"
"github.com/spf13/pflag"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
govutils "github.com/cosmos/cosmos-sdk/x/gov/client/utils"
)
type legacyProposal struct {
Title string
Description string
Type string
Deposit string
}
func (p legacyProposal) validate() error {
if p.Type == "" {
return fmt.Errorf("proposal type is required")
}
if p.Title == "" {
return fmt.Errorf("proposal title is required")
}
if p.Description == "" {
return fmt.Errorf("proposal description is required")
}
return nil
}
func parseSubmitLegacyProposalFlags(fs *pflag.FlagSet) (*legacyProposal, error) {
proposal := &legacyProposal{}
proposalFile, _ := fs.GetString(FlagProposal)
if proposalFile == "" {
proposalType, _ := fs.GetString(FlagProposalType)
title, _ := fs.GetString(FlagTitle)
description, _ := fs.GetString(FlagDescription)
if proposalType == "" && title == "" && description == "" {
return nil, fmt.Errorf("one of the --proposal or (--title, --description and --type) flags are required")
}
proposal.Title, _ = fs.GetString(FlagTitle)
proposal.Description, _ = fs.GetString(FlagDescription)
proposal.Type = govutils.NormalizeProposalType(proposalType)
proposal.Deposit, _ = fs.GetString(FlagDeposit)
if err := proposal.validate(); err != nil {
return nil, err
}
return proposal, nil
}
for _, flag := range ProposalFlags {
if v, _ := fs.GetString(flag); v != "" {
return nil, fmt.Errorf("--%s flag provided alongside --proposal, which is a noop", flag)
}
}
contents, err := os.ReadFile(proposalFile)
if err != nil {
return nil, err
}
err = json.Unmarshal(contents, proposal)
if err != nil {
return nil, err
}
if err := proposal.validate(); err != nil {
return nil, err
}
return proposal, nil
}
// proposal defines the new Msg-based proposal.
type proposal struct {
// Msgs defines an array of sdk.Msgs proto-JSON-encoded as Anys.
Messages []json.RawMessage
Metadata string
Deposit string
}
func parseSubmitProposal(cdc codec.Codec, path string) ([]sdk.Msg, string, sdk.Coins, error) {
var proposal proposal
contents, err := os.ReadFile(path)
if err != nil {
return nil, "", nil, err
}
err = json.Unmarshal(contents, &proposal)
if err != nil {
return nil, "", nil, err
}
msgs := make([]sdk.Msg, len(proposal.Messages))
for i, anyJSON := range proposal.Messages {
var msg sdk.Msg
err := cdc.UnmarshalInterfaceJSON(anyJSON, &msg)
if err != nil {
return nil, "", nil, err
}
msgs[i] = msg
}
deposit, err := sdk.ParseCoinsNormalized(proposal.Deposit)
if err != nil {
return nil, "", nil, err
}
return msgs, proposal.Metadata, deposit, nil
}