Skip to content

Commit

Permalink
amendment cli utility can also use input file
Browse files Browse the repository at this point in the history
  • Loading branch information
giunatale committed Sep 25, 2024
1 parent 5a0e5f1 commit c7ceddb
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions x/gov/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,21 +383,24 @@ $ %s tx gov weighted-vote 1 yes=0.6,no=0.3,abstain=0.1 --from mykey
// between the current constitution (queried) and the updated constitution
// from the provided markdown file.
func NewCmdGenerateConstitutionAmendment() *cobra.Command {
flagCurrentConstitution := "current-constitution"

cmd := &cobra.Command{
Use: "generate-constitution-amendment [path/to/updated/constitution.md]",
Args: cobra.ExactArgs(1),
Short: "Generate a constitution amendment proposal message",
Long: strings.TrimSpace(
fmt.Sprintf(`Generate a constitution amendment proposal message from the current
constitution and the provided updated constitution.
Queries the current constitution from the node and generates a
valid constitution amendment proposal message containing the unified diff
Queries the current constitution from the node (unless --current-constitution is used)
and generates a valid constitution amendment proposal message containing the unified diff
between the current constitution and the updated constitution provided
in a markdown file.
NOTE: this is just a utility command, it is not able to generate or submit a valid Tx
to submit on-chain. Use the 'tx gov submit-proposal' command in conjunction with the
result of this one to submit the proposal.
NOTE: this is just a utility command, it is not able to generate or
submit a valid Tx. Use the 'tx gov submit-proposal' command in
conjunction with the result of this one to submit the proposal.
See also 'tx gov draft-proposal' for a more general proposal drafting tool.
Example:
$ %s tx gov generate-constitution-amendment path/to/updated/constitution.md
Expand All @@ -412,19 +415,35 @@ $ %s tx gov generate-constitution-amendment path/to/updated/constitution.md
return err
}

// Query the current constitution
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
queryClient := v1.NewQueryClient(clientCtx)
resp, err := queryClient.Constitution(cmd.Context(), &v1.QueryConstitutionRequest{})

var currentConstitution string
currentConstitutionPath, err := cmd.Flags().GetString(flagCurrentConstitution)
if err != nil {
return err
}

if currentConstitutionPath != "" {
// Read the current constitution from the provided file
currentConstitution, err = readFromMarkdownFile(currentConstitutionPath)
if err != nil {
return err
}
} else {
// Query the current constitution from the node
queryClient := v1.NewQueryClient(clientCtx)
resp, err := queryClient.Constitution(cmd.Context(), &v1.QueryConstitutionRequest{})
if err != nil {
return err
}
currentConstitution = resp.Constitution
}

// Generate the unified diff between the current and updated constitutions
diff, err := govutils.GenerateUnifiedDiff(resp.Constitution, updatedConstitution)
diff, err := govutils.GenerateUnifiedDiff(currentConstitution, updatedConstitution)
if err != nil {
return err
}
Expand All @@ -445,6 +464,9 @@ $ %s tx gov generate-constitution-amendment path/to/updated/constitution.md
if err != nil {
panic(err)
}
// add flag to pass input constitution file instead of querying node
// for the current constitution
cmd.Flags().String(flagCurrentConstitution, "", "Path to the current constitution markdown file (optional, if not provided, the current constitution will be queried from the node)")

return cmd
}

0 comments on commit c7ceddb

Please sign in to comment.