github.com/decred/politeia@v1.4.0/politeiawww/cmd/pictl/cmdvoteinv.go (about) 1 // Copyright (c) 2020-2021 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "fmt" 9 "strconv" 10 11 tkv1 "github.com/decred/politeia/politeiawww/api/ticketvote/v1" 12 pclient "github.com/decred/politeia/politeiawww/client" 13 ) 14 15 // cmdVoteInv retrieves the censorship record tokens of the public records in 16 // the inventory, categorized by their vote status. 17 type cmdVoteInv struct { 18 Args struct { 19 Status string `positional-arg-name:"status"` 20 Page uint32 `positional-arg-name:"page"` 21 } `positional-args:"true" optional:"true"` 22 } 23 24 // Execute executes the cmdVoteInv command. 25 // 26 // This function satisfies the go-flags Commander interface. 27 func (c *cmdVoteInv) Execute(args []string) error { 28 _, err := voteInv(c) 29 if err != nil { 30 return err 31 } 32 return nil 33 } 34 35 // voteInv returns the vote inventory. It has been pulled out of Execute so 36 // that it can be used in test commands. 37 func voteInv(c *cmdVoteInv) (map[string][]string, error) { 38 // Setup client 39 opts := pclient.Opts{ 40 HTTPSCert: cfg.HTTPSCert, 41 Verbose: cfg.Verbose, 42 RawJSON: cfg.RawJSON, 43 } 44 pc, err := pclient.New(cfg.Host, opts) 45 if err != nil { 46 return nil, err 47 } 48 49 // Setup status and page number 50 var status tkv1.VoteStatusT 51 if c.Args.Status != "" { 52 // Parse status. This can be either the numeric status code or the 53 // human readable equivalent. 54 status, err = parseVoteStatus(c.Args.Status) 55 if err != nil { 56 return nil, err 57 } 58 59 // If a status was given but no page number was give, default 60 // to page number 1. 61 if c.Args.Page == 0 { 62 c.Args.Page = 1 63 } 64 } 65 66 // Get vote inventory 67 i := tkv1.Inventory{ 68 Status: status, 69 Page: c.Args.Page, 70 } 71 ir, err := pc.TicketVoteInventory(i) 72 if err != nil { 73 return nil, err 74 } 75 76 // Print inventory 77 printJSON(ir) 78 79 return ir.Vetted, nil 80 81 } 82 83 func parseVoteStatus(status string) (tkv1.VoteStatusT, error) { 84 // Parse status. This can be either the numeric status code or the 85 // human readable equivalent. 86 var ( 87 vs tkv1.VoteStatusT 88 89 statuses = map[string]tkv1.VoteStatusT{ 90 "unauthorized": tkv1.VoteStatusUnauthorized, 91 "authorized": tkv1.VoteStatusAuthorized, 92 "started": tkv1.VoteStatusStarted, 93 "approved": tkv1.VoteStatusApproved, 94 "rejected": tkv1.VoteStatusRejected, 95 "ineligible": tkv1.VoteStatusIneligible, 96 "1": tkv1.VoteStatusUnauthorized, 97 "2": tkv1.VoteStatusAuthorized, 98 "3": tkv1.VoteStatusStarted, 99 "5": tkv1.VoteStatusApproved, 100 "6": tkv1.VoteStatusRejected, 101 "7": tkv1.VoteStatusIneligible, 102 } 103 ) 104 u, err := strconv.ParseUint(status, 10, 32) 105 if err == nil { 106 // Numeric status code found 107 vs = tkv1.VoteStatusT(u) 108 } else if s, ok := statuses[status]; ok { 109 // Human readable status code found 110 vs = s 111 } else { 112 return vs, fmt.Errorf("invalid status '%v'", status) 113 } 114 115 return vs, nil 116 } 117 118 // voteInvHelpMsg is printed to stdout by the help command. 119 const voteInvHelpMsg = `voteinv 120 121 Inventory requests the tokens of public records in the inventory categorized by 122 vote status. 123 124 The status and page arguments can be provided to request a specific page of 125 record tokens. 126 127 If no status is provided then a page of tokens for all statuses will be 128 returned. The page argument will be ignored. 129 130 Valid statuses: 131 ("1") "unauthorized" 132 ("2") "authorized" 133 ("3") "started" 134 ("5") "approved" 135 ("6") "rejected" 136 ("7") "ineligible" 137 138 Arguments: 139 1. status (string, optional) Status of tokens being requested. 140 2. page (uint32, optional) Page number. 141 `