forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_pgp_export.go
112 lines (97 loc) · 2.43 KB
/
cmd_pgp_export.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"fmt"
"os"
"golang.org/x/net/context"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol"
rpc "github.com/keybase/go-framed-msgpack-rpc"
)
func NewCmdPGPExport(cl *libcmdline.CommandLine) cli.Command {
return cli.Command{
Name: "export",
Usage: "Export a PGP key from keybase",
Action: func(c *cli.Context) {
cl.ChooseCommand(&CmdPGPExport{}, "export", c)
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "o, outfile",
Usage: "Specify an outfile (stdout by default).",
},
cli.BoolFlag{
Name: "s, secret",
Usage: "Export secret key.",
},
cli.StringFlag{
Name: "q, query",
Usage: "Only export keys matching that query.",
},
},
Description: `"keybase pgp export" exports public (and optionally private) PGP keys
from Keybase, and into a file or to standard output. It doesn't access
the GnuGP keychain at all.`,
}
}
type CmdPGPExport struct {
UnixFilter
arg keybase1.PGPExportArg
outfile string
}
func (s *CmdPGPExport) ParseArgv(ctx *cli.Context) error {
nargs := len(ctx.Args())
var err error
s.arg.Options.Secret = ctx.Bool("secret")
s.arg.Options.Query = ctx.String("query")
s.outfile = ctx.String("outfile")
if nargs > 0 {
err = fmt.Errorf("export doesn't take args")
}
return err
}
func (s *CmdPGPExport) Run() (err error) {
protocols := []rpc.Protocol{
NewSecretUIProtocol(G),
}
cli, err := GetPGPClient()
if err != nil {
return err
}
if err = RegisterProtocols(protocols); err != nil {
return err
}
return s.finish(cli.PGPExport(context.TODO(), s.arg))
}
func (s *CmdPGPExport) finish(res []keybase1.KeyInfo, inErr error) error {
if inErr != nil {
return inErr
}
if len(res) > 1 {
G.Log.Warning("Found several matches:")
for _, k := range res {
// XXX os.Stderr? why not Log?
os.Stderr.Write([]byte(k.Desc + "\n\n"))
}
return fmt.Errorf("Specify a key to export")
}
if len(res) == 0 {
return fmt.Errorf("No matching keys found")
}
snk := initSink(s.outfile)
if err := snk.Open(); err != nil {
return err
}
snk.Write([]byte(res[0].Key))
return snk.Close()
}
func (s *CmdPGPExport) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
KbKeyring: true,
}
}