forked from yinheli/sshw
/
config.go
74 lines (62 loc) 路 1.08 KB
/
config.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
package sshw
import (
"github.com/go-yaml/yaml"
"golang.org/x/crypto/ssh"
"io/ioutil"
"os/user"
"path"
)
type Node struct {
Name string `json:"name"`
Host string `json:"host"`
User string `json:"user"`
Port int `json:"port"`
KeyPath string `json:"keypath"`
Passphrase string `json:"passphrase"`
Password string `json:"password"`
Children []*Node `json:"children"`
}
func (n *Node) String() string {
return n.Name
}
func (n *Node) user() string {
if n.User == "" {
return "root"
}
return n.User
}
func (n *Node) port() int {
if n.Port <= 0 {
return 22
}
return n.Port
}
func (n *Node) password() ssh.AuthMethod {
if n.Password == "" {
return nil
}
return ssh.Password(n.Password)
}
var (
config []*Node
)
func GetConfig() []*Node {
return config
}
func LoadConfig() error {
u, err := user.Current()
if err != nil {
return err
}
b, err := ioutil.ReadFile(path.Join(u.HomeDir, ".sshw"))
if err != nil {
return err
}
var c []*Node
err = yaml.Unmarshal(b, &c)
if err != nil {
return err
}
config = c
return nil
}