-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmodels.go
77 lines (65 loc) · 1.71 KB
/
models.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
package manssh
import (
"fmt"
"github.com/xwjdsh/ssh_config"
)
// HostConfig struct include alias, connect string and other config
type HostConfig struct {
// Alias alias
Alias string
// Path found in which file
Path string
// PathMap key is file path, value is the alias's hosts
PathMap map[string][]*ssh_config.Host
// OwnConfig own config
OwnConfig map[string]string
// ImplicitConfig implicit config
ImplicitConfig map[string]string
}
// NewHostConfig new HostConfig
func NewHostConfig(alias, path string, host *ssh_config.Host) *HostConfig {
return &HostConfig{
Alias: alias,
Path: path,
PathMap: map[string][]*ssh_config.Host{path: {host}},
OwnConfig: map[string]string{},
ImplicitConfig: map[string]string{},
}
}
// ConnectionStr return the connection string
func (hc *HostConfig) ConnectionStr() string {
if !hc.Display() {
return ""
}
var (
user, hostname, port string
ok bool
)
if user, ok = hc.OwnConfig["user"]; !ok {
user = hc.ImplicitConfig["user"]
delete(hc.ImplicitConfig, "user")
} else {
delete(hc.OwnConfig, "user")
}
if hostname, ok = hc.OwnConfig["hostname"]; !ok {
delete(hc.ImplicitConfig, "hostname")
hostname = hc.ImplicitConfig["hostname"]
} else {
delete(hc.OwnConfig, "hostname")
}
if port, ok = hc.OwnConfig["port"]; !ok {
port = hc.ImplicitConfig["port"]
delete(hc.ImplicitConfig, "port")
} else {
delete(hc.OwnConfig, "port")
}
return fmt.Sprintf("%s@%s:%s", user, hostname, port)
}
// Display Whether to display connection string
func (hc *HostConfig) Display() bool {
hostname := hc.OwnConfig["hostname"]
if hostname == "" {
hostname = hc.ImplicitConfig["hostname"]
}
return hostname != ""
}