-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
ssh.go
55 lines (45 loc) · 1.07 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
package yagu
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/kevinburke/ssh_config"
)
type SSHMachine struct {
User string
Keys *ssh.PublicKeys
}
func SSHCredentials(machine string) (SSHMachine, error) {
fmt.Println("ssh.CredsLookup")
// try to ssh config file
pk := ""
pka, err := ssh_config.GetAllStrict(machine, "IdentityFile")
fmt.Println(pka, err)
if err != nil {
// try to load id_rsa.pub
hdir, err := os.UserHomeDir()
if err != nil {
// no home dir?
return SSHMachine{}, err
}
// set pk file name to git's expected default, often the one uploaded per GitHub's docs
pk = filepath.Join(hdir, ".ssh", "id_rsa.pub")
}
if strings.HasPrefix(pk, "~") {
if hdir, err := os.UserHomeDir(); err == nil {
pk = strings.Replace(pk, "~", hdir, 1)
}
}
usr := ssh_config.Get(machine, "User")
if usr == "" {
usr = "git"
}
fmt.Println(" ", usr, pk)
pks, err := ssh.NewPublicKeysFromFile(usr, pk, "")
if err != nil {
return SSHMachine{}, err
}
return SSHMachine{usr, pks}, nil
}