-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.go
216 lines (182 loc) · 4.45 KB
/
app.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"context"
"fmt"
"hosts-switch/hosts"
"log"
)
// App struct
type App struct {
ctx context.Context
myHosts *hosts.MyHosts
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{
myHosts: hosts.NewMyHosts(),
}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
err := a.myHosts.Read()
if err != nil {
log.Println(err.Error())
}
a.myHosts.Split()
a.myHosts.PrettyByGroup()
a.myHosts.Print()
}
// GetMyHosts .
func (a *App) GetMyHosts() *hosts.MyHosts {
return a.myHosts
}
// GetHostsText .
func (a *App) GetHostsText() string {
return a.myHosts.HostsText
}
// GetInUseHostsText .
func (a *App) GetInUseHostsText() string {
return a.myHosts.InUseHostsText
}
// SaveAllHosts .
func (a *App) SaveAllHosts(hostsText string) string {
a.myHosts.HostsText = hostsText
a.myHosts.Split()
a.myHosts.PrettyByGroup()
if err := a.myHosts.Write(); err != nil {
return err.Error()
}
return ""
}
// SaveAllInUseHosts .
func (a *App) SaveAllInUseHosts(hostsText string) string {
a.myHosts.HostsText = a.myHosts.NoInUseHostsText + "\n" + hostsText
a.myHosts.Split()
a.myHosts.PrettyByGroup()
fmt.Println(a.myHosts.HostsText)
if err := a.myHosts.Write(); err != nil {
return err.Error()
}
return ""
}
// SaveAllGroupHosts .
func (a *App) SaveAllGroupHosts(groupName, hostsText string) string {
a.myHosts.SetGroup(groupName, hostsText)
a.myHosts.PrettyByGroup()
a.myHosts.Split()
a.myHosts.PrettyByGroup()
if err := a.myHosts.Write(); err != nil {
return err.Error()
}
return ""
}
// AddHost .
func (a *App) AddHost(groupName string, ip string, hostName string) string {
a.myHosts.Add(groupName, ip, hostName)
a.myHosts.PrettyByGroup()
a.myHosts.Split()
a.myHosts.PrettyByGroup()
if err := a.myHosts.Write(); err != nil {
return err.Error()
}
return ""
}
// SaveAddHostsText .
func (a *App) SaveAddHostsText(hostsText string) string {
a.myHosts.HostsText = a.myHosts.HostsText + "\n" + hostsText
a.myHosts.Split()
a.myHosts.PrettyByGroup()
fmt.Println(a.myHosts.HostsText)
if err := a.myHosts.Write(); err != nil {
return err.Error()
}
return ""
}
// DeleteHost .
func (a *App) DeleteHost(groupName string, hostNameID int) string {
a.myHosts.Delete(groupName, hostNameID)
a.myHosts.PrettyByGroup()
a.myHosts.Split()
a.myHosts.PrettyByGroup()
if err := a.myHosts.Write(); err != nil {
return err.Error()
}
return ""
}
// DeleteHosts .
func (a *App) DeleteHosts(hosts []hosts.Host) string {
for _, host := range hosts {
a.myHosts.Delete(host.GroupName, host.ID)
}
a.myHosts.PrettyByGroup()
a.myHosts.Split()
a.myHosts.PrettyByGroup()
if err := a.myHosts.Write(); err != nil {
return err.Error()
}
return ""
}
// SetGroupName .
func (a *App) SetGroupName(oldGroupName, groupName string) string {
if oldGroupName == groupName {
return ""
}
a.myHosts.SetGroupNameByOldGroupName(oldGroupName, groupName)
a.myHosts.PrettyByGroup()
a.myHosts.Split()
a.myHosts.PrettyByGroup()
if err := a.myHosts.Write(); err != nil {
return err.Error()
}
return ""
}
// SetGroupNameByHostnameId .
func (a *App) SetGroupNameByHostnameId(hostNameID int, groupName string) string {
a.myHosts.SetGroupNameByHostnameId(hostNameID, groupName)
a.myHosts.PrettyByList()
a.myHosts.Split()
a.myHosts.PrettyByGroup()
if err := a.myHosts.Write(); err != nil {
return err.Error()
}
return ""
}
// SwitchByGroupName .
func (a *App) SwitchByGroupName(groupName string, show bool) string {
a.myHosts.SwitchByGroupName(groupName, show)
a.myHosts.PrettyByGroup()
a.myHosts.Split()
a.myHosts.PrettyByGroup()
if err := a.myHosts.Write(); err != nil {
return err.Error()
}
return ""
}
// GetAllGroupNames .
func (a *App) GetAllGroupNames() []string {
return a.myHosts.GetAllGroupNames()
}
// SwitchByHostnameId .
func (a *App) SwitchByHostnameId(groupName string, hostNameID int, show bool) string {
a.myHosts.SwitchByHostNameId(groupName, hostNameID, show)
a.myHosts.PrettyByGroup()
if err := a.myHosts.Write(); err != nil {
return err.Error()
}
return ""
}
// SetGroupNameByHosts .
func (a *App) SetGroupNameByHosts(hosts []hosts.Host, groupName string) string {
for _, host := range hosts {
a.myHosts.SetGroupNameByHostnameId(host.ID, groupName)
}
a.myHosts.PrettyByList()
a.myHosts.Split()
a.myHosts.PrettyByGroup()
if err := a.myHosts.Write(); err != nil {
return err.Error()
}
return ""
}