github.com/decred/politeia@v1.4.0/politeiawww/cmd/pictl/cmdvotedetails.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  
    10  	tkv1 "github.com/decred/politeia/politeiawww/api/ticketvote/v1"
    11  	pclient "github.com/decred/politeia/politeiawww/client"
    12  )
    13  
    14  // cmdVoteDetails retrieves vote details for the provided record.
    15  type cmdVoteDetails struct {
    16  	Args struct {
    17  		Token string `positional-arg-name:"token"`
    18  	} `positional-args:"true" required:"true"`
    19  }
    20  
    21  // Execute executes the cmdVoteDetails command.
    22  //
    23  // This function satisfies the go-flags Commander interface.
    24  func (c *cmdVoteDetails) Execute(args []string) error {
    25  	dr, err := voteDetails(c)
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	// Print results
    31  	for _, v := range dr.Auths {
    32  		fmt.Printf("Vote authorization\n")
    33  		printAuthDetails(v)
    34  		printf("\n")
    35  	}
    36  	if dr.Vote != nil {
    37  		fmt.Printf("Vote details\n")
    38  		printVoteDetails(*dr.Vote)
    39  	}
    40  
    41  	return nil
    42  }
    43  
    44  // voteDetails fetches the ticketvote API Details route for a token. This
    45  // function has been pulled out of the Execute method so that it can be used
    46  // in the test commands.
    47  func voteDetails(c *cmdVoteDetails) (*tkv1.DetailsReply, error) {
    48  	// Setup client
    49  	opts := pclient.Opts{
    50  		HTTPSCert: cfg.HTTPSCert,
    51  		Verbose:   cfg.Verbose,
    52  		RawJSON:   cfg.RawJSON,
    53  	}
    54  	pc, err := pclient.New(cfg.Host, opts)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	// Get vote details
    60  	d := tkv1.Details{
    61  		Token: c.Args.Token,
    62  	}
    63  	dr, err := pc.TicketVoteDetails(d)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	return dr, nil
    69  }
    70  
    71  // voteDetailsHelpMsg is printed to stdout by the help command.
    72  const voteDetailsHelpMsg = `votedetails "token"
    73  
    74  Fetch the vote details for a record.
    75  
    76  Arguments:
    77  1. token  (string, required)  Record token.`