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

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package client
     5  
     6  import (
     7  	"golang.org/x/net/context"
     8  
     9  	"github.com/keybase/cli"
    10  	"github.com/keybase/client/go/libcmdline"
    11  	"github.com/keybase/client/go/libkb"
    12  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    13  	"github.com/keybase/go-framed-msgpack-rpc/rpc"
    14  )
    15  
    16  func newCmdBotSignup(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
    17  	cmd := cli.Command{
    18  		Name:  "signup",
    19  		Usage: "Signup a bot that will have a paper key but no device",
    20  		Action: func(c *cli.Context) {
    21  			cl.ChooseCommand(NewCmdBotSignupRunner(g), "signup", c)
    22  		},
    23  		Flags: []cli.Flag{
    24  			cli.StringFlag{
    25  				Name:  "c, invite-code",
    26  				Usage: "Specify an invite code (optional)",
    27  			},
    28  			cli.StringFlag{
    29  				Name:  "e, email",
    30  				Usage: "Specify an account email (optional)",
    31  			},
    32  			cli.StringFlag{
    33  				Name:  "u, username",
    34  				Usage: "Specify a username.",
    35  			},
    36  			cli.StringFlag{
    37  				Name:  "t, token",
    38  				Usage: "Specify a bot token",
    39  			},
    40  		},
    41  	}
    42  	return cmd
    43  }
    44  
    45  type CmdBotSignup struct {
    46  	libkb.Contextified
    47  	scli     keybase1.SignupClient
    48  	ccli     keybase1.ConfigClient
    49  	code     string
    50  	email    string
    51  	username string
    52  	token    keybase1.BotToken
    53  }
    54  
    55  func NewCmdBotSignupRunner(g *libkb.GlobalContext) *CmdBotSignup {
    56  	return &CmdBotSignup{
    57  		Contextified: libkb.NewContextified(g),
    58  	}
    59  }
    60  
    61  func (s *CmdBotSignup) ParseArgv(ctx *cli.Context) (err error) {
    62  	nargs := len(ctx.Args())
    63  	if nargs != 0 {
    64  		return BadArgsError{"Signup doesn't take arguments."}
    65  	}
    66  
    67  	s.username = ctx.String("username")
    68  	if len(s.username) == 0 {
    69  		return BadArgsError{"must supply a username"}
    70  	}
    71  	s.token, err = keybase1.NewBotToken(ctx.String("token"))
    72  	if err != nil {
    73  		return BadArgsError{"bad bot token"}
    74  	}
    75  
    76  	s.code = ctx.String("invite-code")
    77  	s.email = ctx.String("email")
    78  
    79  	return nil
    80  }
    81  
    82  func (s *CmdBotSignup) Run() (err error) {
    83  
    84  	if err = s.initClient(); err != nil {
    85  		return err
    86  	}
    87  
    88  	rarg := keybase1.SignupArg{
    89  		Username:    s.username,
    90  		InviteCode:  s.code,
    91  		RandomPw:    true,
    92  		StoreSecret: false,
    93  		SkipMail:    false,
    94  		BotToken:    s.token,
    95  		GenPGPBatch: false,
    96  		GenPaper:    false,
    97  		SkipGPG:     true,
    98  	}
    99  	res, err := s.scli.Signup(context.TODO(), rarg)
   100  	if err != nil {
   101  		return err
   102  	}
   103  	_ = s.G().UI.GetTerminalUI().Output(res.PaperKey + "\n")
   104  	return nil
   105  }
   106  
   107  func (s *CmdBotSignup) GetUsage() libkb.Usage {
   108  	return libkb.Usage{
   109  		Config:     true,
   110  		GpgKeyring: true,
   111  		KbKeyring:  true,
   112  		API:        true,
   113  	}
   114  }
   115  
   116  func (s *CmdBotSignup) initClient() error {
   117  	var err error
   118  	if s.scli, err = GetSignupClient(s.G()); err != nil {
   119  		return err
   120  	}
   121  
   122  	if s.ccli, err = GetConfigClient(s.G()); err != nil {
   123  		return err
   124  	}
   125  	var protocols []rpc.Protocol
   126  	return RegisterProtocolsWithContext(protocols, s.G())
   127  }