-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cmd_chat_download.go
131 lines (119 loc) · 3.4 KB
/
cmd_chat_download.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package client
import (
"context"
"errors"
"fmt"
"os"
"strconv"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
isatty "github.com/mattn/go-isatty"
)
type CmdChatDownload struct {
libkb.Contextified
hasTTY bool
resolvingRequest chatConversationResolvingRequest
messageID uint64
outputFile string
preview bool
}
func newCmdChatDownload(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "download",
Usage: "Download an attachment from a conversation",
ArgumentHelp: "<conversation> <attachment id> [-o filename]",
Action: func(c *cli.Context) {
cmd := &CmdChatDownload{Contextified: libkb.NewContextified(g)}
cl.ChooseCommand(cmd, "download", c)
cl.SetLogForward(libcmdline.LogForwardNone)
},
Flags: append([]cli.Flag{
cli.BoolFlag{
Name: "p, preview",
Usage: "Download preview",
},
cli.StringFlag{
Name: "o, outfile",
Usage: "Specify output file (stdout default)",
},
}, getConversationResolverFlags()...),
}
}
func (c *CmdChatDownload) ParseArgv(ctx *cli.Context) (err error) {
if len(ctx.Args()) != 2 {
return errors.New("usage: keybase chat download <conversation> <attachment id> [-o filename]")
}
tlfName := ctx.Args()[0]
if c.resolvingRequest, err = parseConversationResolvingRequest(ctx, tlfName); err != nil {
return err
}
id, err := strconv.ParseUint(ctx.Args()[1], 10, 64)
if err != nil {
return err
}
c.messageID = id
c.outputFile = ctx.String("outfile")
if len(c.outputFile) == 0 {
c.outputFile = "-" // stdout
} else {
c.outputFile = libkb.GetSafePath(c.outputFile)
}
c.preview = ctx.Bool("preview")
c.hasTTY = isatty.IsTerminal(os.Stdin.Fd())
return nil
}
func (c *CmdChatDownload) Run() (err error) {
ui := NewChatCLIUI(c.G())
protocols := []rpc.Protocol{
chat1.ChatUiProtocol(ui),
}
if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
return err
}
if err = annotateResolvingRequest(c.G(), &c.resolvingRequest); err != nil {
return err
}
if err := CheckAndStartStandaloneChat(c.G(), c.resolvingRequest.MembersType); err != nil {
return err
}
resolver, err := newChatConversationResolver(c.G())
if err != nil {
return err
}
conversation, _, err := resolver.Resolve(context.TODO(), c.resolvingRequest, chatConversationResolvingBehavior{
CreateIfNotExists: false,
MustNotExist: false,
Interactive: c.hasTTY,
IdentifyBehavior: keybase1.TLFIdentifyBehavior_CHAT_CLI,
})
switch err.(type) {
case nil:
case libkb.ResolutionError:
return fmt.Errorf("could not resolve `%s` into Keybase user(s) or a team", c.resolvingRequest.TlfName)
default:
return err
}
opts := downloadOptionsV1{
ConversationID: conversation.Info.Id.ConvIDStr(),
MessageID: chat1.MessageID(c.messageID),
Output: c.outputFile,
Preview: c.preview,
}
h := newChatServiceHandler(c.G())
reply := h.DownloadV1(context.Background(), opts, NewChatCLIUI(c.G()), NewChatCLINotifications(c.G()))
if reply.Error != nil {
return errors.New(reply.Error.Message)
}
return nil
}
func (c *CmdChatDownload) GetUsage() libkb.Usage {
return libkb.Usage{
API: true,
KbKeyring: true,
Config: true,
}
}