github.com/decred/politeia@v1.4.0/politeiawww/cmd/pictl/cmdvotesummaries.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  	tkv1 "github.com/decred/politeia/politeiawww/api/ticketvote/v1"
     9  	pclient "github.com/decred/politeia/politeiawww/client"
    10  )
    11  
    12  // cmdVoteSummaries retrieves the vote summaries for the provided records.
    13  type cmdVoteSummaries struct {
    14  	Args struct {
    15  		Tokens []string `positional-arg-name:"tokens"`
    16  	} `positional-args:"true" required:"true"`
    17  }
    18  
    19  // Execute executes the cmdVoteSummaries command.
    20  //
    21  // This function satisfies the go-flags Commander interface.
    22  func (c *cmdVoteSummaries) Execute(args []string) error {
    23  	_, err := voteSummaries(c)
    24  	if err != nil {
    25  		return err
    26  	}
    27  	return nil
    28  }
    29  
    30  // voteSummaries fetches the ticketvote API Summaries route for a page of
    31  // tokens. This function has been pulled out of the Execute method so that
    32  // it can be used in the test commands.
    33  func voteSummaries(c *cmdVoteSummaries) (map[string]tkv1.Summary, error) {
    34  	// Setup client
    35  	opts := pclient.Opts{
    36  		HTTPSCert: cfg.HTTPSCert,
    37  		Verbose:   cfg.Verbose,
    38  		RawJSON:   cfg.RawJSON,
    39  	}
    40  	pc, err := pclient.New(cfg.Host, opts)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	// Get vote summaries
    46  	s := tkv1.Summaries{
    47  		Tokens: c.Args.Tokens,
    48  	}
    49  	sr, err := pc.TicketVoteSummaries(s)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	// Print summaries
    55  	for k, v := range sr.Summaries {
    56  		printf("%v\n", voteSummaryString(k, v, 0))
    57  		printf("-----\n")
    58  	}
    59  
    60  	return sr.Summaries, nil
    61  }
    62  
    63  // voteSummariesHelpMsg is printed to stdout by the help command.
    64  const voteSummariesHelpMsg = `votesummaries "tokens..."
    65  
    66  Fetch the vote summaries for the provided records. This command accepts both
    67  full length tokens and token prefixes.
    68  
    69  Example usage:
    70  $ pictl votesummaries cda97ace0a476514 71dd3a110500fb6a
    71  $ pictl votesummaries cda97ac 71dd3a1`