/
target.go
146 lines (127 loc) · 3.14 KB
/
target.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package target
import (
"net"
"strings"
"github.com/abesto/easyssh/util"
)
/*
Target describes a single machine that easyssh will operate on.
*/
type Target struct {
Host string // Used to reference the host from the outside, generally an Host
Hostname string // What the host calls itself
IP string
User string
CoalesceOrder []string
}
func (t Target) withUser(s string) string {
if t.User != "" {
return t.User + "@" + s
}
return s
}
func firstNonEmptyString(strs ...string) string {
for _, s := range strs {
if s != "" {
return s
}
}
return ""
}
func (t Target) firstNonEmptyStringWithUser(strs ...string) string {
t.verify()
return t.withUser(firstNonEmptyString(strs...))
}
var Coalescers = map[string](func(Target) string){
"host": func(t Target) string { return t.Host },
"hostname": func(t Target) string { return t.Hostname },
"ip": func(t Target) string { return t.IP },
}
/*
Return the first non-empty field defined in t.CoalesceOrder. Default is ["ip", "host"]
*/
func (t Target) SSHTarget() string {
coalescers := t.CoalesceOrder
if len(coalescers) == 0 {
coalescers = []string{"ip", "host"}
}
candidates := make([]string, len(coalescers))
for i, coalescerName := range coalescers {
candidates[i] = Coalescers[coalescerName](t)
}
return t.firstNonEmptyStringWithUser(candidates...)
}
/*
FriendlyName returns the most descriptive name available for the target.
Specifically, the first non-empty value of Hostname, Host, IP
*/
func (t Target) FriendlyName() string {
return t.firstNonEmptyStringWithUser(t.Hostname, t.Host, t.IP)
}
func (t Target) String() string {
return t.FriendlyName()
}
func (t Target) IsEmpty() bool {
return firstNonEmptyString(t.IP, t.Host) == ""
}
func (t Target) verify() {
if t.IsEmpty() {
util.Panicf("At least one of Target.IP and Target.Host must be set")
}
}
/*
SSHTargets maps Target.SSHTarget over a []Target
*/
func SSHTargets(ts []Target) []string {
strs := []string{}
for _, t := range ts {
strs = append(strs, t.SSHTarget())
}
return strs
}
/*
FriendlyNames maps Target.FriendlyName over a []Target
*/
func FriendlyNames(ts []Target) []string {
strs := []string{}
for _, t := range ts {
strs = append(strs, t.FriendlyName())
}
return strs
}
/*
FromString creates a Target from a string description of the form [user@]<ip|fqdn>
*/
func FromString(str string) Target {
if len(str) == 0 {
util.Panicf("FromString(str string) Target got an empty string")
}
var parts = strings.Split(str, "@")
var hostDef string
var target Target
if len(parts) == 1 {
hostDef = parts[0]
} else if len(parts) == 2 {
target.User = parts[0]
hostDef = parts[1]
} else {
util.Panicf("FromString(str string) Target got a string containing more than one @ character")
}
if net.ParseIP(hostDef) != nil {
target.IP = hostDef
} else {
target.Host = hostDef
}
target.verify()
return target
}
/*
FromStrings maps FromString over...string
*/
func FromStrings(targetStrings ...string) []Target {
targets := make([]Target, len(targetStrings))
for i := 0; i < len(targetStrings); i++ {
targets[i] = FromString(targetStrings[i])
}
return targets
}