github.com/decred/politeia@v1.4.0/politeiawww/cmd/pictl/cmdproposals.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 piv1 "github.com/decred/politeia/politeiawww/api/pi/v1" 9 rcv1 "github.com/decred/politeia/politeiawww/api/records/v1" 10 pclient "github.com/decred/politeia/politeiawww/client" 11 ) 12 13 // cmdProposals retrieves the proposal records for the provided tokens. 14 type cmdProposals struct { 15 Args struct { 16 Tokens []string `positional-arg-name:"proposals" required:"true"` 17 } `positional-args:"true"` 18 } 19 20 // Execute executes the cmdProposals command. 21 // 22 // This function satisfies the go-flags Commander interface. 23 func (c *cmdProposals) Execute(args []string) error { 24 records, err := proposals(c) 25 if err != nil { 26 return err 27 } 28 29 // Print proposals to stdout 30 for _, v := range records { 31 err = printProposal(v) 32 if err != nil { 33 return err 34 } 35 printf("-----\n") 36 } 37 38 return nil 39 } 40 41 // proposals fetches the records API Records route for a page of 42 // tokens. This function has been pulled out of the Execute method so that 43 // it can be used in the test commands. 44 func proposals(c *cmdProposals) (map[string]rcv1.Record, error) { 45 // Setup client 46 opts := pclient.Opts{ 47 HTTPSCert: cfg.HTTPSCert, 48 Cookies: cfg.Cookies, 49 HeaderCSRF: cfg.CSRF, 50 Verbose: cfg.Verbose, 51 RawJSON: cfg.RawJSON, 52 } 53 pc, err := pclient.New(cfg.Host, opts) 54 if err != nil { 55 return nil, err 56 } 57 58 // Get records 59 reqs := make([]rcv1.RecordRequest, 0, len(c.Args.Tokens)) 60 for _, v := range c.Args.Tokens { 61 reqs = append(reqs, rcv1.RecordRequest{ 62 Token: v, 63 Filenames: []string{ 64 piv1.FileNameProposalMetadata, 65 piv1.FileNameVoteMetadata, 66 }, 67 }) 68 } 69 r := rcv1.Records{ 70 Requests: reqs, 71 } 72 records, err := pc.Records(r) 73 if err != nil { 74 return nil, err 75 } 76 77 return records, nil 78 } 79 80 // proposalsHelpMsg is printed to stdout by the help command. 81 const proposalsHelpMsg = `proposals [flags] "tokens..." 82 83 Retrieve the proposals for the provided tokens. The proposal index file and the 84 proposal attachments are not returned from this command. Use the proposal 85 details command if you are trying to retieve the full proposal. 86 87 This command defaults to retrieving vetted proposals unless the --unvetted flag 88 is used. This command accepts both the full tokens or the token prefixes. 89 90 Arguments: 91 1. tokens ([]string, required) Proposal tokens. 92 93 Example: 94 $ pictl proposals f6458c2d8d9ef41c 9f9af91cf609d839 917c6fde9bcc2118 95 $ pictl proposals f6458c2 9f9af91 917c6fd`