1// Copyright 2015 Keybase, Inc. All rights reserved. Use of
2// this source code is governed by the included BSD license.
3
4package client
5
6import (
7	"errors"
8	"strconv"
9	"strings"
10
11	"golang.org/x/net/context"
12
13	"github.com/keybase/cli"
14	"github.com/keybase/client/go/libcmdline"
15	"github.com/keybase/client/go/libkb"
16	keybase1 "github.com/keybase/client/go/protocol/keybase1"
17)
18
19type cmdTeamFTL struct {
20	libkb.Contextified
21	arg keybase1.FastTeamLoadArg
22}
23
24func newCmdTeamFTL(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
25	return cli.Command{
26		Name:         "ftl",
27		Description:  "trigger FTL (fast team loader) for debugging",
28		ArgumentHelp: "id",
29		Flags: []cli.Flag{
30			cli.BoolFlag{
31				Name:  "f, force",
32				Usage: "Force reload, as if it's been an hour since the last reload",
33			},
34			cli.StringFlag{
35				Name:  "g, gens",
36				Usage: "Which generation(s) to request; if multiple, then comma-separate them",
37			},
38		},
39		Action: func(c *cli.Context) {
40			cl.ChooseCommand(&cmdTeamFTL{Contextified: libkb.NewContextified(g)}, "ftl", c)
41		},
42	}
43}
44
45func (c *cmdTeamFTL) Run() error {
46	cli, err := GetTeamsClient(c.G())
47	if err != nil {
48		return err
49	}
50	res, err := cli.Ftl(context.TODO(), c.arg)
51	if err != nil {
52		return err
53	}
54	c.G().UI.GetTerminalUI().Printf("%+v\n", res)
55	return nil
56}
57
58func (c *cmdTeamFTL) parseTeamGenerations(s string) (gens []keybase1.PerTeamKeyGeneration, err error) {
59	v := strings.Split(s, ",")
60	for _, e := range v {
61		i, err := strconv.Atoi(e)
62		if err != nil {
63			return nil, err
64		}
65		gens = append(gens, keybase1.PerTeamKeyGeneration(i))
66	}
67	return gens, nil
68}
69
70func (c *cmdTeamFTL) ParseArgv(ctx *cli.Context) error {
71	if len(ctx.Args()) != 1 {
72		return errors.New("need a team ID argument")
73	}
74	var err error
75	c.arg.ID, err = keybase1.TeamIDFromString(ctx.Args()[0])
76	if err != nil {
77		return err
78	}
79
80	c.arg.ForceRefresh = ctx.Bool("force")
81	s := ctx.String("gens")
82	if len(s) > 0 {
83		c.arg.KeyGenerationsNeeded, err = c.parseTeamGenerations(s)
84		if err != nil {
85			return err
86		}
87	} else {
88		c.arg.NeedLatestKey = true
89	}
90	c.arg.Applications = []keybase1.TeamApplication{keybase1.TeamApplication_CHAT}
91
92	return nil
93}
94
95func (c *cmdTeamFTL) GetUsage() libkb.Usage {
96	return libkb.Usage{
97		Config: true,
98		API:    true,
99	}
100}
101