/
listServers.go
186 lines (159 loc) · 4.47 KB
/
listServers.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"math"
"sort"
"sysStarter/config"
)
func printServerName(intro string, strLen int, svr config.ServerDetails, svrOk bool) {
fmt.Printf("%s%-*.*s",
intro, strLen, strLen, svr.Name)
if !svrOk {
fmt.Println(" server not found")
return
}
fmt.Printf(" hosts: %6d", len(svr.Hosts))
var totInstances int
for _, instances := range svr.Hosts {
totInstances += instances
}
fmt.Printf(" instances: %6d\n", totInstances)
}
func printServerHosts(sc *config.SysConfig,
intro string,
strLen int,
svr config.ServerDetails,
hostsPrompt string) {
if len(svr.Hosts) > 0 {
indent2 := printPrompt(intro, strLen, hostsPrompt)
indent := ""
for hostName, val := range svr.Hosts {
fmt.Printf(" %s%-*.*s %4d instance",
indent, sc.MaxHostNameLen(), sc.MaxHostNameLen(), hostName, val)
if val > 1 {
fmt.Print("s")
}
fmt.Print(":\n")
indent = indent2
if hd, ok := sc.FindHost(hostName); ok {
if hd.Servers != nil {
for _, cmd := range hd.Servers[svr.Name] {
fmt.Printf(" %s %s\n", indent, cmd.Command)
}
}
}
}
}
}
func printPrompt(intro string, strLen int, prompt string) string {
fmt.Printf("%s%*.*s", intro, strLen, strLen, prompt)
indent := fmt.Sprintf("%s%*.*s", intro, strLen, strLen, "")
return indent
}
func printServerClasses(intro string, strLen int, classes []config.ClassName, prompt string) {
if len(classes) > 0 {
indent2 := printPrompt(intro, strLen, prompt)
indent := ""
for _, className := range classes {
fmt.Printf(" %s%s\n", indent, className)
indent = indent2
}
}
}
func printServerFull(sc *config.SysConfig, intro string, strLen int, svr config.ServerDetails,
classesPrompt, hostsPrompt, needsPrompt, neededByPrompt string) {
printServerClasses(intro, strLen, svr.Classes, classesPrompt)
printServerHosts(sc, intro, strLen, svr, hostsPrompt)
if len(svr.Needs) > 0 {
indent2 := printPrompt(intro, strLen, needsPrompt)
indent := ""
for needs := range svr.Needs {
fmt.Printf(" %s%s\n", indent, needs)
indent = indent2
}
}
if len(svr.NeededBy) > 0 {
indent2 := printPrompt(intro, strLen, neededByPrompt)
indent := ""
for neededBy := range svr.NeededBy {
fmt.Printf(" %s%s\n", indent, neededBy)
indent = indent2
}
}
fmt.Print("\n")
}
func printServerExpanded(sc *config.SysConfig, svr config.ServerDetails,
svrPrompt, classesPrompt, hostsPrompt, needsPrompt, neededByPrompt string) {
for _, className := range svr.Classes {
fmt.Println(svrPrompt, svr.Name, classesPrompt, className)
}
for hostName := range svr.Hosts {
if hd, ok := sc.FindHost(hostName); ok {
if hd.Servers != nil {
for _, cmd := range hd.Servers[svr.Name] {
fmt.Println(svrPrompt, svr.Name,
hostsPrompt, hostName,
"command:", cmd.Command)
}
}
}
}
for needs := range svr.Needs {
fmt.Println(svrPrompt, svr.Name, needsPrompt, needs)
}
for neededBy := range svr.NeededBy {
fmt.Println(svrPrompt, svr.Name, neededByPrompt, neededBy)
}
}
func printServer(sc *config.SysConfig, svrName config.ServerName) {
classesPrompt := "class:"
hostsPrompt := "host:"
needsPrompt := "needs:"
neededByPrompt := "needed-by:"
strLen := sc.MaxServerNameLen()
strLen = int(math.Max(float64(strLen), float64(len(classesPrompt))))
strLen = int(math.Max(float64(strLen), float64(len(hostsPrompt))))
strLen = int(math.Max(float64(strLen), float64(len(needsPrompt))))
strLen = int(math.Max(float64(strLen), float64(len(neededByPrompt))))
intro := " "
svr, svrOk := sc.FindServer(svrName)
switch listStyle {
case "full":
printServerName(intro, strLen, svr, svrOk)
if !svrOk {
return
}
printServerFull(sc, intro, strLen, svr,
classesPrompt, hostsPrompt, needsPrompt, neededByPrompt)
case "expanded":
svrPrompt := "server:"
if !svrOk {
fmt.Println(svrPrompt, svrName, "NOT-FOUND")
return
}
printServerExpanded(sc, svr,
svrPrompt, classesPrompt, hostsPrompt, needsPrompt, neededByPrompt)
case "short":
printServerName(intro, strLen, svr, svrOk)
}
}
type byServerName []config.ServerName
func (s byServerName) Len() int {
return len(s)
}
func (s byServerName) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byServerName) Less(i, j int) bool {
return s[i] < s[j]
}
func printAllServers(sc *config.SysConfig) {
serverNames := make([]config.ServerName, 0, len(servers))
for name := range servers {
serverNames = append(serverNames, name)
}
sort.Sort(byServerName(serverNames))
for _, name := range serverNames {
printServer(sc, name)
}
}