-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathssh.go
42 lines (34 loc) · 876 Bytes
/
ssh.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
package main
import (
"io/ioutil"
"os"
"regexp"
)
// getLocalSSHKeys returns list of keys located in ~/.ssh, this is probably
// Unix only way so we return empty list when on Windows (~/.ssh doesn't exist)
func getLocalSSHKeys(sshKeysDirectory string) ([]string, error) {
var sshKeys []string
// Check where the SSH keys directory exists
_, err := os.Stat(sshKeysDirectory)
if os.IsNotExist(err) {
return sshKeys, nil
}
files, err := ioutil.ReadDir(sshKeysDirectory)
if err != nil {
return sshKeys, err
}
for _, file := range files {
matched, err := regexp.Match("^id_", []byte(file.Name()))
if err != nil {
return sshKeys, err
}
matchedTail, err := regexp.Match(".pub$", []byte(file.Name()))
if err != nil {
return sshKeys, err
}
if matched && !matchedTail {
sshKeys = append(sshKeys, file.Name())
}
}
return sshKeys, err
}