...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package remotecmd
18
19 import (
20 "encoding/json"
21 "errors"
22 "io/ioutil"
23 "net/url"
24 "os"
25
26 "github.com/sassoftware/relic/cmdline/shared"
27 "github.com/spf13/cobra"
28 )
29
30 var GetKeyCmd = &cobra.Command{
31 Use: "get-key",
32 Short: "Get a public key certificate from the remote server",
33 RunE: getKeyCmd,
34 }
35
36 func init() {
37 RemoteCmd.AddCommand(GetKeyCmd)
38 }
39
40 type keyInfo struct {
41 X509Certificate string
42 PGPCertificate string
43 }
44
45 func getKeyInfo(keyName string) (keyInfo, error) {
46 response, err := CallRemote("keys/"+url.PathEscape(keyName), "GET", nil, nil)
47 if err != nil {
48 return keyInfo{}, err
49 }
50 blob, err := ioutil.ReadAll(response.Body)
51 if err != nil {
52 return keyInfo{}, err
53 }
54 response.Body.Close()
55 var info keyInfo
56 if err := json.Unmarshal(blob, &info); err != nil {
57 return keyInfo{}, err
58 }
59 return info, nil
60 }
61
62 func getKeyCmd(cmd *cobra.Command, args []string) error {
63 if len(args) == 0 {
64 return errors.New("specify one or more key names. See also 'list-keys'")
65 }
66 for _, keyName := range args {
67 info, err := getKeyInfo(keyName)
68 if err != nil {
69 return shared.Fail(err)
70 }
71 os.Stdout.WriteString(info.X509Certificate)
72 os.Stdout.WriteString(info.PGPCertificate)
73 }
74 return nil
75 }
76
View as plain text