/
cmd_pgp_export.go
121 lines (106 loc) · 2.87 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
113
114
115
116
117
118
119
120
121
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"fmt"
"os"
"strings"
"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/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
)
func NewCmdPGPExport(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "export",
Usage: "Export a PGP key from keybase",
Action: func(c *cli.Context) {
cl.ChooseCommand(&CmdPGPExport{Contextified: libkb.NewContextified(g)}, "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.",
},
cli.BoolFlag{
Name: "unencrypted",
Usage: "When exporting private keys, do not protect with a passphrase.",
},
},
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 GnuPG keychain at all. By default, when
exporting private keys, you will be asked for passphrase to encrypt
the exported keys.`,
}
}
type CmdPGPExport struct {
UnixFilter
arg keybase1.PGPExportArg
outfile string
libkb.Contextified
}
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.arg.Encrypted = !ctx.Bool("unencrypted")
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(s.G()),
}
cli, err := GetPGPClient(s.G())
if err != nil {
return err
}
if err = RegisterProtocolsWithContext(protocols, s.G()); 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 {
s.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.G(), s.outfile)
if err := snk.Open(); err != nil {
return err
}
snk.Write([]byte(strings.TrimSpace(res[0].Key) + "\n"))
return snk.Close()
}
func (s *CmdPGPExport) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
KbKeyring: true,
}
}