-
Notifications
You must be signed in to change notification settings - Fork 13
/
config.go
98 lines (86 loc) · 1.93 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package ssh
import (
"os"
"path"
"time"
)
type Config struct {
User string
Host string
Port int
Password string
KeyFiles []string
Passphrase string
StickySession bool
// DisableAgentForwarding, if true, will not forward the SSH agent.
DisableAgentForwarding bool
// HandshakeTimeout limits the amount of time we'll wait to handshake before
// saying the connection failed.
HandshakeTimeout time.Duration
// KeepAliveInterval sets how often we send a channel request to the
// server. A value < 0 disables.
KeepAliveInterval time.Duration
// Timeout is how long to wait for a read or write to succeed.
Timeout time.Duration
}
var DefaultConfig = &Config{
Host: "localhost",
Port: 22,
User: "root",
// KeyFiles: []string{path.Join(os.Getenv("HOME"), "/.ssh/id_rsa")},
}
var Default = DefaultConfig
func WithUser(user string) *Config {
return Default.WithUser(user)
}
func (c *Config) WithUser(user string) *Config {
if user == "" {
user = "root"
}
c.User = user
return c
}
func WithHost(host string) *Config {
return Default.WithHost(host)
}
func (c *Config) WithHost(host string) *Config {
if host == "" {
host = "localhost"
}
c.Host = host
return c
}
func WithPassword(password string) *Config {
return Default.WithPassword(password)
}
func (c *Config) WithPassword(password string) *Config {
c.Password = password
return c
}
func WithKey(keyfile, passphrase string) *Config {
return Default.WithKey(keyfile, passphrase)
}
func (c *Config) WithKey(keyfile, passphrase string) *Config {
if keyfile == "" {
if home := os.Getenv("HOME"); home != "" {
keyfile = path.Join(home, "/.ssh/id_rsa")
}
}
for _, s := range c.KeyFiles {
if s == keyfile {
return c
}
}
c.KeyFiles = append(c.KeyFiles, keyfile)
return c
}
//
func (c *Config) SetKeys(keyfiles []string) *Config {
if keyfiles == nil {
return c
}
t := make([]string, len(keyfiles))
copy(t, keyfiles)
c.KeyFiles = t
return c
}