github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/client/cmd_team_list_requests.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  	"text/tabwriter"
     8  
     9  	"github.com/keybase/cli"
    10  	"github.com/keybase/client/go/libcmdline"
    11  	"github.com/keybase/client/go/libkb"
    12  	"github.com/keybase/client/go/protocol/keybase1"
    13  	"golang.org/x/net/context"
    14  )
    15  
    16  type CmdTeamListRequests struct {
    17  	libkb.Contextified
    18  	json bool
    19  	team string
    20  }
    21  
    22  func newCmdTeamListRequests(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
    23  	return cli.Command{
    24  		Name:  "list-requests",
    25  		Usage: "List requests to join teams.",
    26  		Action: func(c *cli.Context) {
    27  			cmd := NewCmdTeamListRequestsRunner(g)
    28  			cl.ChooseCommand(cmd, "list-requests", c)
    29  		},
    30  		Flags: []cli.Flag{
    31  			cli.BoolFlag{
    32  				Name:  "j, json",
    33  				Usage: "Output requests as JSON",
    34  			},
    35  			cli.StringFlag{
    36  				Name:  "t, team",
    37  				Usage: "List request for specific team",
    38  			},
    39  		},
    40  	}
    41  }
    42  
    43  func NewCmdTeamListRequestsRunner(g *libkb.GlobalContext) *CmdTeamListRequests {
    44  	return &CmdTeamListRequests{Contextified: libkb.NewContextified(g)}
    45  }
    46  
    47  func (c *CmdTeamListRequests) ParseArgv(ctx *cli.Context) error {
    48  	c.json = ctx.Bool("json")
    49  	c.team = ctx.String("team")
    50  	return nil
    51  }
    52  
    53  func (c *CmdTeamListRequests) Run() error {
    54  	cli, err := GetTeamsClient(c.G())
    55  	if err != nil {
    56  		return err
    57  	}
    58  	arg := keybase1.TeamListRequestsArg{}
    59  	if c.team != "" {
    60  		arg.TeamName = &c.team
    61  	}
    62  	reqs, err := cli.TeamListRequests(context.Background(), arg)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	if c.json {
    68  		return c.outputJSON(reqs)
    69  	}
    70  
    71  	return c.outputTerminal(reqs)
    72  
    73  }
    74  
    75  func (c *CmdTeamListRequests) outputJSON(reqs []keybase1.TeamJoinRequest) error {
    76  	b, err := json.MarshalIndent(reqs, "", "    ")
    77  	if err != nil {
    78  		return err
    79  	}
    80  	dui := c.G().UI.GetDumbOutputUI()
    81  	_, err = dui.Printf(string(b) + "\n")
    82  	return err
    83  }
    84  
    85  func (c *CmdTeamListRequests) outputTerminal(reqs []keybase1.TeamJoinRequest) error {
    86  	dui := c.G().UI.GetTerminalUI()
    87  	if len(reqs) == 0 {
    88  		dui.Printf("No requests at this time.\n")
    89  		return nil
    90  	}
    91  
    92  	tabw := new(tabwriter.Writer)
    93  	tabw.Init(dui.OutputWriter(), 0, 8, 2, ' ', 0)
    94  	for _, req := range reqs {
    95  		fmt.Fprintf(tabw, "%s\t%s wants to join\n", req.Name, req.Username)
    96  	}
    97  	tabw.Flush()
    98  
    99  	werr := dui.ErrorWriter()
   100  	fmt.Fprintf(werr, "%s\n", strings.Repeat("-", 70))
   101  	fmt.Fprintf(werr, "To handle requests, use `keybase team add-member` or `keybase team ignore-request`.\n")
   102  
   103  	return nil
   104  }
   105  
   106  func (c *CmdTeamListRequests) GetUsage() libkb.Usage {
   107  	return libkb.Usage{
   108  		Config:    true,
   109  		API:       true,
   110  		KbKeyring: true,
   111  	}
   112  }