github.com/decred/politeia@v1.4.0/politeiawww/cmd/pictl/cmdcommentcount.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  	cmv1 "github.com/decred/politeia/politeiawww/api/comments/v1"
     9  	pclient "github.com/decred/politeia/politeiawww/client"
    10  )
    11  
    12  // cmdCommentCount retreives the comments for the specified proposal.
    13  type cmdCommentCount struct {
    14  	Args struct {
    15  		Tokens []string `positional-arg-name:"tokens"`
    16  	} `positional-args:"true" required:"true"`
    17  }
    18  
    19  // Execute executes the cmdCommentCount command.
    20  //
    21  // This function satisfies the go-flags Commander interface.
    22  func (c *cmdCommentCount) Execute(args []string) error {
    23  	_, err := commentCount(c)
    24  	if err != nil {
    25  		return err
    26  	}
    27  	return nil
    28  }
    29  
    30  // commentCount returns the number of comments that have been made on the
    31  // provided records. This function has been pulled out of the Execute method so
    32  // that it can be used in test commands.
    33  func commentCount(c *cmdCommentCount) (map[string]uint32, error) {
    34  	// Setup client
    35  	opts := pclient.Opts{
    36  		HTTPSCert:  cfg.HTTPSCert,
    37  		Cookies:    cfg.Cookies,
    38  		HeaderCSRF: cfg.CSRF,
    39  		Verbose:    cfg.Verbose,
    40  		RawJSON:    cfg.RawJSON,
    41  	}
    42  	pc, err := pclient.New(cfg.Host, opts)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	// Get comments
    48  	cc := cmv1.Count{
    49  		Tokens: c.Args.Tokens,
    50  	}
    51  	cr, err := pc.CommentCount(cc)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	// Print counts
    57  	for k, v := range cr.Counts {
    58  		printf("%v %v\n", k, v)
    59  	}
    60  
    61  	return cr.Counts, nil
    62  }
    63  
    64  // commentCountHelpMsg is printed to stdout by the help command.
    65  const commentCountHelpMsg = `commentcount "tokens..." 
    66  
    67  Get the number of comments that have been made on each of the provided
    68  records.
    69  
    70  Arguments:
    71  1. token  (string, required)  Proposal censorship token
    72  
    73  Examples:
    74  $ pictl commentcount f6458c2d8d9ef41c 9f9af91cf609d839 917c6fde9bcc2118
    75  $ pictl commentcount f6458c2 9f9af91 917c6fd`