forked from eoscanada/eosc
/
forumPropose.go
65 lines (53 loc) · 2.16 KB
/
forumPropose.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright © 2018 EOS Canada <info@eoscanada.com>
package cmd
import (
"context"
"errors"
"time"
"github.com/zhongshuwen/zswchain-go"
"github.com/zhongshuwen/zswchain-go/forum"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tidwall/sjson"
)
var forumProposeCmd = &cobra.Command{
Use: "propose [proposer] [proposal_name] [title] [proposal_expiration_date]",
Short: "Submit a proposition for votes",
Args: cobra.ExactArgs(4),
Run: func(cmd *cobra.Command, args []string) {
targetAccount := toAccount(viper.GetString("forum-cmd-target-contract"), "--target-contract")
proposer := toAccount(args[0], "proposer")
proposalName := toName(args[1], "proposal_name")
title := args[2]
expiresAtStr := args[3]
var expiresAt zsw.JSONTime
var err error
if expiresAtStr != "" {
expiresAt, err = zsw.ParseJSONTime(expiresAtStr)
errorCheck("no valid proposal expiration date provided. Must be set as ISO-8601 format. Maximum 6 months in the future.", err)
}
if expiresAt.Before(time.Now()) {
errorCheck("proposal expiration date must in the future", errors.New("provided time is in the past"))
}
proposalJSON := viper.GetString("forum-propose-cmd-json")
content := viper.GetString("forum-propose-cmd-content")
jsonType := viper.GetString("forum-propose-cmd-type")
if proposalJSON == "" && content != "" {
proposalJSON = "{}"
}
proposalJSON, err = sjson.Set(proposalJSON, "content", content)
// Defaults JSON schema type to `bps-proposal-v1`
proposalJSON, err = sjson.Set(proposalJSON, "type", jsonType)
errorCheck("setting content in json", err)
action := forum.NewPropose(proposer, proposalName, title, proposalJSON, expiresAt)
action.Account = targetAccount
api := getAPI()
pushEOSCActions(context.Background(), api, action)
},
}
func init() {
forumCmd.AddCommand(forumProposeCmd)
forumProposeCmd.Flags().String("content", "", "Markdown 'content' to be injected in the JSON (whether you propose a --json or not).")
forumProposeCmd.Flags().String("json", "", "Proposal JSON body.")
forumProposeCmd.Flags().String("type", "bps-proposal-v1", "The JSON schema of the proposal, set as a `type` in the JSON payload.")
}