-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
list.go
99 lines (80 loc) · 1.94 KB
/
list.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
package file
import (
"github.com/guumaster/hostctl/pkg/types"
)
// ListOptions contains available options for listing.
type ListOptions struct {
Renderer types.Renderer
Profiles []string
ProfilesOnly bool
StatusFilter types.Status
}
func includeProfile(needle string, stack []string) bool {
if len(stack) == 0 {
return true
}
for _, s := range stack {
if s == needle {
return true
}
}
return false
}
// ProfileStatus shows a table only with profile names status.
func (f *File) ProfileStatus(r types.Renderer, profiles []string) {
for _, name := range f.data.ProfileNames {
currProfile := f.data.Profiles[name]
if profiles != nil && !includeProfile(name, profiles) {
continue
}
r.AppendRow(&types.Row{
Profile: currProfile.Name,
Status: currProfile.GetStatus(),
})
}
_ = r.Render()
}
// List shows a table with profile names status and routing information.
func (f *File) List(r types.Renderer, opts *ListOptions) {
addDefault(f, r, opts)
for _, name := range f.data.ProfileNames {
addProfiles(f.data.Profiles[name], r, opts)
}
_ = r.Render()
}
func addDefault(f *File, r types.Renderer, opts *ListOptions) {
// First check if default should be shown
if !includeProfile(types.Default, opts.Profiles) {
return
}
for _, row := range f.data.DefaultProfile {
if row.Comment == "" && row.Profile != "" {
r.AppendRow(row)
}
}
if len(f.data.DefaultProfile) > 0 && len(f.data.Profiles) > 0 {
r.AddSeparator()
}
}
func addProfiles(p *types.Profile, r types.Renderer, opts *ListOptions) {
if !includeProfile(p.Name, opts.Profiles) {
return
}
if opts.StatusFilter != "" && p.Status != opts.StatusFilter {
return
}
for _, ip := range p.IPList {
route := p.Routes[ip]
for _, h := range route.HostNames {
r.AppendRow(&types.Row{
Profile: p.Name,
Status: p.GetStatus(),
IP: route.IP.String(),
Host: h,
})
}
}
if len(p.IPList) > 0 {
r.AddSeparator()
}
}