Skip to content

Commit

Permalink
feat(gov,group): allow draft-proposal to skip metatada prompt (back…
Browse files Browse the repository at this point in the history
…port #17220) (#17243)

Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
mergify[bot] and julienrbrt authored Aug 1, 2023
1 parent 1cc38b9 commit 2e76cae
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Improvements

* (x/group, x/gov) [#17220](https://github.com/cosmos/cosmos-sdk/pull/17220) Add `--skip-metadata` flag in `draft-proposal` to skip metadata prompt.

### Bug Fixes

* (x/gov,x/group) [#17220](https://github.com/cosmos/cosmos-sdk/pull/17220) Do not try validate `msgURL` as web URL in `draft-proposal` command.
* (cli) [#17188](https://github.com/cosmos/cosmos-sdk/pull/17188) Fix `--output-document` flag in `tx multi-sign`.
* (x/auth) [#17209](https://github.com/cosmos/cosmos-sdk/pull/17209) Internal error on AccountInfo when account's public key is not set.

Expand Down
65 changes: 53 additions & 12 deletions x/gov/client/cli/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,19 @@ func Prompt[T any](data T, namePrefix string) (T, error) {

// create prompts
prompt := promptui.Prompt{
Label: fmt.Sprintf("Enter %s's %s", namePrefix, strings.ToLower(client.CamelCaseToString(v.Type().Field(i).Name))),
Label: fmt.Sprintf("Enter %s %s", namePrefix, strings.ToLower(client.CamelCaseToString(v.Type().Field(i).Name))),
Validate: client.ValidatePromptNotEmpty,
}

fieldName := strings.ToLower(v.Type().Field(i).Name)
// validation per field name
if strings.Contains(fieldName, "url") {
prompt.Validate = client.ValidatePromptURL
}

if strings.EqualFold(fieldName, "authority") {
// pre-fill with gov address
prompt.Default = authtypes.NewModuleAddress(types.ModuleName).String()
prompt.Validate = client.ValidatePromptAddress
}

// TODO(@julienrbrt) use scalar annotation instead of dumb string name matching
if strings.Contains(fieldName, "addr") ||
strings.Contains(fieldName, "sender") ||
strings.Contains(fieldName, "voter") ||
Expand Down Expand Up @@ -150,9 +147,8 @@ type proposalType struct {
}

// Prompt the proposal type values and return the proposal and its metadata
func (p *proposalType) Prompt(cdc codec.Codec) (*proposal, types.ProposalMetadata, error) {
// set metadata
metadata, err := Prompt(types.ProposalMetadata{}, "proposal")
func (p *proposalType) Prompt(cdc codec.Codec, skipMetadata bool) (*proposal, types.ProposalMetadata, error) {
metadata, err := PromptMetadata(skipMetadata)
if err != nil {
return nil, metadata, fmt.Errorf("failed to set proposal metadata: %w", err)
}
Expand Down Expand Up @@ -201,8 +197,48 @@ func getProposalSuggestions() []string {
return types
}

// PromptMetadata prompts for proposal metadata or only title and summary if skip is true
func PromptMetadata(skip bool) (types.ProposalMetadata, error) {
var (
metadata types.ProposalMetadata
err error
)

if !skip {
metadata, err = Prompt(types.ProposalMetadata{}, "proposal")
if err != nil {
return metadata, fmt.Errorf("failed to set proposal metadata: %w", err)
}
} else {
// prompt for title and summary
titlePrompt := promptui.Prompt{
Label: "Enter proposal title",
Validate: client.ValidatePromptNotEmpty,
}

metadata.Title, err = titlePrompt.Run()
if err != nil {
return metadata, fmt.Errorf("failed to set proposal title: %w", err)
}

summaryPrompt := promptui.Prompt{
Label: "Enter proposal summary",
Validate: client.ValidatePromptNotEmpty,
}

metadata.Summary, err = summaryPrompt.Run()
if err != nil {
return metadata, fmt.Errorf("failed to set proposal summary: %w", err)
}
}

return metadata, nil
}

// NewCmdDraftProposal let a user generate a draft proposal.
func NewCmdDraftProposal() *cobra.Command {
flagSkipMetadata := "skip-metadata"

cmd := &cobra.Command{
Use: "draft-proposal",
Short: "Generate a draft proposal json file. The generated proposal json contains only one message (skeleton).",
Expand Down Expand Up @@ -260,7 +296,9 @@ func NewCmdDraftProposal() *cobra.Command {
}
}

result, metadata, err := proposal.Prompt(clientCtx.Codec)
skipMetadataPrompt, _ := cmd.Flags().GetBool(flagSkipMetadata)

result, metadata, err := proposal.Prompt(clientCtx.Codec, skipMetadataPrompt)
if err != nil {
return err
}
Expand All @@ -269,17 +307,20 @@ func NewCmdDraftProposal() *cobra.Command {
return err
}

if err := writeFile(draftMetadataFileName, metadata); err != nil {
return err
if !skipMetadataPrompt {
if err := writeFile(draftMetadataFileName, metadata); err != nil {
return err
}
}

fmt.Printf("The draft proposal has successfully been generated.\nProposals should contain off-chain metadata, please upload the metadata JSON to IPFS.\nThen, replace the generated metadata field with the IPFS CID.\n")
cmd.Println("The draft proposal has successfully been generated.\nProposals should contain off-chain metadata, please upload the metadata JSON to IPFS.\nThen, replace the generated metadata field with the IPFS CID.")

return nil
},
}

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().Bool(flagSkipMetadata, false, "skip metadata prompt")

return cmd
}
Expand Down
19 changes: 13 additions & 6 deletions x/group/client/cli/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ type proposalType struct {
}

// Prompt the proposal type values and return the proposal and its metadata.
func (p *proposalType) Prompt(cdc codec.Codec) (*Proposal, govtypes.ProposalMetadata, error) {
func (p *proposalType) Prompt(cdc codec.Codec, skipMetadata bool) (*Proposal, govtypes.ProposalMetadata, error) {
// set metadata
metadata, err := govcli.Prompt(govtypes.ProposalMetadata{}, "proposal")
metadata, err := govcli.PromptMetadata(skipMetadata)
if err != nil {
return nil, metadata, fmt.Errorf("failed to set proposal metadata: %w", err)
}
Expand Down Expand Up @@ -86,6 +86,8 @@ func (p *proposalType) Prompt(cdc codec.Codec) (*Proposal, govtypes.ProposalMeta

// NewCmdDraftProposal let a user generate a draft proposal.
func NewCmdDraftProposal() *cobra.Command {
flagSkipMetadata := "skip-metadata"

cmd := &cobra.Command{
Use: "draft-proposal",
Short: "Generate a draft proposal json file. The generated proposal json contains only one message (skeleton).",
Expand Down Expand Up @@ -137,7 +139,9 @@ func NewCmdDraftProposal() *cobra.Command {
panic("unexpected proposal type")
}

result, metadata, err := proposal.Prompt(clientCtx.Codec)
skipMetadataPrompt, _ := cmd.Flags().GetBool(flagSkipMetadata)

result, metadata, err := proposal.Prompt(clientCtx.Codec, skipMetadataPrompt)
if err != nil {
return err
}
Expand All @@ -146,17 +150,20 @@ func NewCmdDraftProposal() *cobra.Command {
return err
}

if err := writeFile(draftMetadataFileName, metadata); err != nil {
return err
if !skipMetadataPrompt {
if err := writeFile(draftMetadataFileName, metadata); err != nil {
return err
}
}

fmt.Printf("The draft proposal has successfully been generated.\nProposals should contain off-chain metadata, please upload the metadata JSON to IPFS.\nThen, replace the generated metadata field with the IPFS CID.\n")
cmd.Println("The draft proposal has successfully been generated.\nProposals should contain off-chain metadata, please upload the metadata JSON to IPFS.\nThen, replace the generated metadata field with the IPFS CID.")

return nil
},
}

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().Bool(flagSkipMetadata, false, "skip metadata prompt")

return cmd
}
Expand Down

0 comments on commit 2e76cae

Please sign in to comment.