github.com/decred/politeia@v1.4.0/politeiawww/cmd/pictl/cmduserproposals.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  	rcv1 "github.com/decred/politeia/politeiawww/api/records/v1"
    11  	pclient "github.com/decred/politeia/politeiawww/client"
    12  )
    13  
    14  // cmdUserProposals retrieves the proposal records for a user.
    15  type cmdUserProposals struct {
    16  	Args struct {
    17  		UserID string `positional-arg-name:"userID" optional:"true"`
    18  	} `positional-args:"true"`
    19  }
    20  
    21  // Execute executes the cmdUserProposals command.
    22  //
    23  // This function satisfies the go-flags Commander interface.
    24  func (c *cmdUserProposals) Execute(args []string) error {
    25  	// Setup client
    26  	opts := pclient.Opts{
    27  		HTTPSCert:  cfg.HTTPSCert,
    28  		Cookies:    cfg.Cookies,
    29  		HeaderCSRF: cfg.CSRF,
    30  		Verbose:    cfg.Verbose,
    31  		RawJSON:    cfg.RawJSON,
    32  	}
    33  	pc, err := pclient.New(cfg.Host, opts)
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	// Setup user ID
    39  	userID := c.Args.UserID
    40  	if userID == "" {
    41  		// No user ID provided. Use the user ID of the logged in user.
    42  		lr, err := client.Me()
    43  		if err != nil {
    44  			if err.Error() == "401" {
    45  				return fmt.Errorf("no user ID provided and no logged in user found")
    46  			}
    47  			return err
    48  		}
    49  		userID = lr.UserID
    50  	}
    51  
    52  	// Get user proposals
    53  	ur := rcv1.UserRecords{
    54  		UserID: userID,
    55  	}
    56  	urr, err := pc.UserRecords(ur)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	// Print record tokens to stdout
    62  	printJSON(urr)
    63  
    64  	return nil
    65  }
    66  
    67  // userProposalsHelpMsg is printed to stdout by the help command.
    68  const userProposalsHelpMsg = `userproposals "userID"
    69  
    70  Retrieve the proprosals that were submitted by a user. If no user ID is given,
    71  the ID of the logged in user will be used.`