github.com/keybase/client/go@v0.0.0-20240520164431-4f512a4c85a3/client/cmd_contacts_debug.go (about)

     1  // Copyright 2019 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  //go:build !production
     5  // +build !production
     6  
     7  package client
     8  
     9  import (
    10  	"encoding/json"
    11  	"errors"
    12  	"fmt"
    13  	"io"
    14  	"os"
    15  
    16  	"github.com/keybase/cli"
    17  	"github.com/keybase/client/go/libcmdline"
    18  	"github.com/keybase/client/go/libkb"
    19  	"github.com/keybase/client/go/protocol/keybase1"
    20  	"golang.org/x/net/context"
    21  )
    22  
    23  // Devel commands for testing contact syncing.
    24  
    25  func NewCmdContacts(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
    26  	return cli.Command{
    27  		Name:         "contacts",
    28  		Usage:        "commands for testing contact sync on desktop",
    29  		ArgumentHelp: "[arguments...]",
    30  		Subcommands: []cli.Command{
    31  			NewCmdContactLookup(cl, g),
    32  			NewCmdSaveContacts(cl, g),
    33  		},
    34  	}
    35  }
    36  
    37  // ------------------------
    38  
    39  // `keybase contacts lookup`
    40  
    41  type CmdContactLookup struct {
    42  	libkb.Contextified
    43  }
    44  
    45  func NewCmdContactLookup(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
    46  	cmd := &CmdContactLookup{
    47  		Contextified: libkb.NewContextified(g),
    48  	}
    49  	return cli.Command{
    50  		Name:         "lookup",
    51  		Usage:        "resolve contact list",
    52  		ArgumentHelp: "",
    53  		Action: func(c *cli.Context) {
    54  			cl.ChooseCommand(cmd, "lookup", c)
    55  		},
    56  	}
    57  }
    58  
    59  func (c *CmdContactLookup) ParseArgv(ctx *cli.Context) error {
    60  	if len(ctx.Args()) != 0 {
    61  		return errors.New("command takes no arguments")
    62  	}
    63  	return nil
    64  }
    65  
    66  func (c *CmdContactLookup) Run() error {
    67  	cli, err := GetContactsClient(c.G())
    68  	if err != nil {
    69  		return err
    70  	}
    71  	bytes, err := io.ReadAll(os.Stdin)
    72  	if err != nil {
    73  		return err
    74  	}
    75  	var contacts []keybase1.Contact
    76  	err = json.Unmarshal(bytes, &contacts)
    77  	if err != nil {
    78  		return err
    79  	}
    80  	ret, err := cli.LookupContactList(context.Background(), keybase1.LookupContactListArg{
    81  		Contacts: contacts,
    82  	})
    83  	if err != nil {
    84  		return err
    85  	}
    86  	s, err := json.MarshalIndent(ret, "", "  ")
    87  	if err != nil {
    88  		return err
    89  	}
    90  	c.G().UI.GetTerminalUI().Printf("%s", s)
    91  	return nil
    92  }
    93  
    94  func (c *CmdContactLookup) GetUsage() libkb.Usage {
    95  	return libkb.Usage{
    96  		Config: true,
    97  		API:    true,
    98  	}
    99  }
   100  
   101  // ------------------------
   102  
   103  // `keybase contacts save`
   104  
   105  type CmdSaveContacts struct {
   106  	libkb.Contextified
   107  }
   108  
   109  func NewCmdSaveContacts(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
   110  	cmd := &CmdSaveContacts{
   111  		Contextified: libkb.NewContextified(g),
   112  	}
   113  	return cli.Command{
   114  		Name:         "save",
   115  		Usage:        "save contact list",
   116  		ArgumentHelp: "",
   117  		Action: func(c *cli.Context) {
   118  			cl.ChooseCommand(cmd, "save", c)
   119  		},
   120  	}
   121  }
   122  
   123  func (c *CmdSaveContacts) ParseArgv(ctx *cli.Context) error {
   124  	if len(ctx.Args()) != 0 {
   125  		return errors.New("command takes no arguments")
   126  	}
   127  	return nil
   128  }
   129  
   130  func (c *CmdSaveContacts) Run() error {
   131  	cli, err := GetContactsClient(c.G())
   132  	if err != nil {
   133  		return err
   134  	}
   135  	bytes, err := io.ReadAll(os.Stdin)
   136  	if err != nil {
   137  		return err
   138  	}
   139  	var contacts []keybase1.Contact
   140  	err = json.Unmarshal(bytes, &contacts)
   141  	if err != nil {
   142  		return err
   143  	}
   144  	_, err = cli.SaveContactList(context.Background(), keybase1.SaveContactListArg{
   145  		Contacts: contacts,
   146  	})
   147  	if err != nil {
   148  		return err
   149  	}
   150  	fmt.Fprintf(c.G().UI.GetTerminalUI().ErrorWriter(), "Contacts saved.\n")
   151  	return nil
   152  }
   153  
   154  func (c *CmdSaveContacts) GetUsage() libkb.Usage {
   155  	return libkb.Usage{
   156  		Config: true,
   157  		API:    true,
   158  	}
   159  }