/
cmd_chat_download.go
93 lines (85 loc) · 2.07 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
package client
import (
"context"
"errors"
"strconv"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/chat1"
)
type CmdChatDownload struct {
libkb.Contextified
tlf string
public bool
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)
},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "p, preview",
Usage: "Download preview",
},
cli.BoolFlag{
Name: "public",
Usage: "Download attachment from public conversation",
},
cli.StringFlag{
Name: "o, outfile",
Usage: "Specify output file (stdout default)",
},
},
}
}
func (c *CmdChatDownload) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) != 2 {
return errors.New("usage: keybase chat download <conversation> <attachment id> [-o filename]")
}
c.tlf = ctx.Args()[0]
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
}
c.preview = ctx.Bool("preview")
c.public = ctx.Bool("public")
return nil
}
func (c *CmdChatDownload) Run() error {
opts := downloadOptionsV1{
Channel: ChatChannel{
Name: c.tlf,
Public: c.public,
},
MessageID: chat1.MessageID(c.messageID),
Output: c.outputFile,
Preview: c.preview,
}
h := newChatServiceHandler(c.G())
reply := h.DownloadV1(context.Background(), opts)
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,
}
}