-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcmd_keys.go
120 lines (100 loc) · 2.54 KB
/
cmd_keys.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
package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
"code.google.com/p/go.crypto/ssh"
)
var argSSHPubKeyPath string
func init() {
cmdKeyAdd.flags.StringVar(&argSSHPubKeyPath, "key-path", "", "path to an ssh-key to upload")
}
var cmdKeyAdd = &cmd{
name: "add-key",
run: func() (err error) {
existingKeyFingerprints := make(map[string]bool)
listKeyResp, listKeyErr := client.ListKeys()
if listKeyErr == nil {
for _, key := range listKeyResp.Keys {
existingKeyFingerprints[key.Fingerprint] = true
}
}
var key ssh.PublicKey
if argSSHPubKeyPath != "" {
key, _, err = sshReadPubKey(argSSHPubKeyPath)
} else {
keys := getSSHKeys(existingKeyFingerprints)
if len(keys) == 0 && shouldContinue("No SSH keys to add. Create a new key?") {
createSSHKey()
keys = getSSHKeys(existingKeyFingerprints)
}
if len(keys) == 0 {
return fmt.Errorf("No SSH keys to add.")
}
key, err = pickSSHKey(keys, os.Stdout)
}
if err != nil {
return fmt.Errorf("refusing to upload: %s", err)
}
var keyName string
keyName = getText("Name this key [optional]")
if keyName == "" {
keyName = time.Now().Format("2006-01-02")
}
keyStr := string(ssh.MarshalAuthorizedKey(key))
err = client.UploadKey(keyName, strings.TrimSpace(keyStr))
if err != nil {
return err
}
fmt.Printf("Added key '%s'.\n", keyName)
return nil
},
flags: flag.NewFlagSet("add-key", flag.ExitOnError),
usage: func() string {
return "add an ssh key to your account"
},
}
var cmdKeysList = &cmd{
name: "list-keys",
run: func() error {
resp, err := client.ListKeys()
if err != nil {
return err
}
for _, key := range resp.Keys {
fmt.Printf("%-20s %s %s\n", key.Name, key.Fingerprint, key.Preview)
}
return nil
},
usage: func() string {
return "list added ssh keys"
},
}
var cmdKeyRemove = &cmd{
name: "remove-key",
run: func() error {
resp, err := client.ListKeys()
if err != nil {
return err
}
for i, key := range resp.Keys {
fmt.Printf("[ %d ] %-20s %s\n", i+1, key.Name, key.Preview)
}
choice, err := pick("key", len(resp.Keys))
if err != nil {
return err
}
chosenKey := resp.Keys[choice]
_, err = client.DeleteKey(chosenKey.Fingerprint)
if err != nil {
return err
}
fmt.Println("Removed key", chosenKey.Preview)
return nil
},
usage: func() string {
return "diassocate an ssh key"
},
}