/
connectors.go
97 lines (84 loc) · 2.28 KB
/
connectors.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
package connector
import (
"fmt"
"strconv"
"strings"
)
type ConnectorCollection struct {
Connectors []*Connector `json:"hosts"`
}
func (s *ConnectorCollection) Add(h *Connector) *ConnectorCollection {
s.Connectors = append(s.Connectors, h)
s.ResetPosition()
return s
}
func (s *ConnectorCollection) ToTopPosition(h *Connector) *ConnectorCollection {
position := h.Position
var tempHosts []*Connector
tempHosts = append(tempHosts, h)
for k, v := range s.Connectors {
if k != position {
tempHosts = append(tempHosts, v)
}
}
s.Connectors = tempHosts
s.ResetPosition()
return s
}
func (s *ConnectorCollection) ResetPosition() *ConnectorCollection {
for k, v := range s.Connectors {
v.Position = k
}
return s
}
func (s *ConnectorCollection) Index(position int) *Connector {
return s.Connectors[position]
}
func (s *ConnectorCollection) Count() int {
return len(s.Connectors)
}
func (s *ConnectorCollection) Has(h *Connector) (bool, *Connector) {
for _, v := range s.Connectors {
if h.Equals(v) {
return true, v
}
}
return false, nil
}
func (s *ConnectorCollection) PrintTable() {
hostLen, portLen, userLen := s.maxLength(4)
hostStr := "Host"
portStr := "Port"
userStr := "User"
hostStr = hostStr + strings.Repeat(" ", hostLen-len(hostStr))
portStr = portStr + strings.Repeat(" ", portLen-len(portStr))
userStr = userStr + strings.Repeat(" ", userLen-len(userStr))
lenOfNo := len(strconv.Itoa(s.Count()))
noSpace := strings.Repeat(" ", lenOfNo-1)
println(fmt.Sprintf("[#]"+noSpace+" %s %s %s %s", hostStr, userStr, portStr, "Explanation"))
noStr := "[%d]"
for k, v := range s.Connectors {
noSpace = strings.Repeat(" ", lenOfNo-len(strconv.Itoa(k)))
host := v.Host + strings.Repeat(" ", hostLen-len(v.Host))
port := v.Port + strings.Repeat(" ", portLen-len(v.Port))
user := v.User + strings.Repeat(" ", userLen-len(v.User))
println(fmt.Sprintf(noStr+noSpace+" %s %s %s %s", k, host, user, port, v.Explanation))
}
}
func (s *ConnectorCollection) maxLength(def int) (hostLen, portLen, userLen int) {
hostLen = def
portLen = def
userLen = def
for _, v := range s.Connectors {
if len(v.Host) > hostLen {
hostLen = len(v.Host)
}
if len(v.Port) > portLen {
portLen = len(v.Port)
}
if len(v.User) > userLen {
userLen = len(v.User)
}
}
return
}