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

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"github.com/keybase/cli"
     9  	"github.com/keybase/client/go/libcmdline"
    10  	"github.com/keybase/client/go/libkb"
    11  	"github.com/keybase/client/go/protocol/chat1"
    12  	"github.com/keybase/client/go/protocol/keybase1"
    13  )
    14  
    15  type CmdChatAddEmoji struct {
    16  	libkb.Contextified
    17  	resolvingRequest chatConversationResolvingRequest
    18  	alias, filename  string
    19  	allowOverwrite   bool
    20  }
    21  
    22  func newCmdChatAddEmoji(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
    23  	return cli.Command{
    24  		Name:         "emoji-add",
    25  		Usage:        "Add an emoji",
    26  		ArgumentHelp: "<conversation> <alias> <filename or url>",
    27  		Action: func(c *cli.Context) {
    28  			cmd := &CmdChatAddEmoji{Contextified: libkb.NewContextified(g)}
    29  			cl.ChooseCommand(cmd, "emoji-add", c)
    30  		},
    31  		Flags: []cli.Flag{
    32  			cli.BoolFlag{
    33  				Name:  "allow-overwrite",
    34  				Usage: "Allow editing of an existing emoji.",
    35  			},
    36  		},
    37  	}
    38  }
    39  
    40  func (c *CmdChatAddEmoji) ParseArgv(ctx *cli.Context) (err error) {
    41  	if len(ctx.Args()) != 3 {
    42  		return fmt.Errorf("must specify an alias, filename, and conversation name")
    43  	}
    44  
    45  	tlfName := ctx.Args()[0]
    46  	c.alias = ctx.Args()[1]
    47  	c.filename = ctx.Args()[2]
    48  	c.resolvingRequest, err = parseConversationResolvingRequest(ctx, tlfName)
    49  	c.allowOverwrite = ctx.Bool("allow-overwrite")
    50  	if err != nil {
    51  		return err
    52  	}
    53  	return nil
    54  }
    55  
    56  func (c *CmdChatAddEmoji) Run() error {
    57  	ctx := context.Background()
    58  	resolver, err := newChatConversationResolver(c.G())
    59  	if err != nil {
    60  		return err
    61  	}
    62  	if err = annotateResolvingRequest(c.G(), &c.resolvingRequest); err != nil {
    63  		return err
    64  	}
    65  	conversation, _, err := resolver.Resolve(ctx, c.resolvingRequest, chatConversationResolvingBehavior{
    66  		CreateIfNotExists: false,
    67  		MustNotExist:      false,
    68  		Interactive:       false,
    69  		IdentifyBehavior:  keybase1.TLFIdentifyBehavior_CHAT_CLI,
    70  	})
    71  	if err != nil {
    72  		return err
    73  	}
    74  	res, err := resolver.ChatClient.AddEmoji(ctx, chat1.AddEmojiArg{
    75  		ConvID:         conversation.GetConvID(),
    76  		Alias:          c.alias,
    77  		Filename:       c.filename,
    78  		AllowOverwrite: c.allowOverwrite,
    79  	})
    80  	if err != nil {
    81  		return err
    82  	}
    83  	if res.Error != nil {
    84  		return errors.New(res.Error.Clidisplay)
    85  	}
    86  	return nil
    87  }
    88  
    89  func (c *CmdChatAddEmoji) GetUsage() libkb.Usage {
    90  	return libkb.Usage{
    91  		API:       true,
    92  		KbKeyring: true,
    93  		Config:    true,
    94  	}
    95  }