This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProgram.cs
executable file
·280 lines (248 loc) · 10.6 KB
/
Program.cs
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System;
using System.Windows.Forms;
using System.Drawing;
using Newtonsoft.Json;
using System.Threading.Tasks;
namespace tray_windows
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new TrayContext());
}
}
internal class TrayContext : ApplicationContext
{
//tray Icon
private NotifyIcon notifyIcon;
// MenuStrip Items
private ToolStripItem status;
private ToolStripItem detailedStatusMenu;
private ToolStripItem startMenu;
private ToolStripItem stopMenu;
private ToolStripItem deleteMenu;
private ToolStripItem openWebConsoleMenu;
private ToolStripItem copyOCLoginCommand;
private ToolStripItem copyOCLoginForDeveloperMenu;
private ToolStripItem copyOCLoginForKubeadminMenu;
private ToolStripItem aboutMenu;
private ToolStripItem exitMenu;
// Initialize tray
public TrayContext()
{
Bitmap bm = new Bitmap(Resource.ocp_logo);
notifyIcon = new NotifyIcon
{
Icon = Icon.FromHandle(bm.GetHicon()),
Visible = true
};
notifyIcon.MouseClick += NotifyIcon_Click;
SetConextMenu();
}
// event handlers and methods
async private void NotifyIcon_Click(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
typeof(NotifyIcon).GetMethod("ShowContextMenu", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).Invoke(notifyIcon, null);
var status = await Task.Run(() => Handlers.HandleStatus());
if (status != null)
{
UpdateClusterStatusMenu(status);
SyncMenuItemStates(status);
}
}
}
// populate the context menu for tray icon
private void SetConextMenu()
{
ContextMenuStrip cm = new ContextMenuStrip();
// Status Menu
status = cm.Items.Add(@"Unknown");
status.Enabled = false;
cm.Items.Add(new ToolStripSeparator());
// Detailed status menu
detailedStatusMenu = cm.Items.Add(@"Detailed Status");
detailedStatusMenu.Click += ShowDetailedStatusForm;
cm.Items.Add(new ToolStripSeparator());
// Start Menu
startMenu = cm.Items.Add(@"Start");
startMenu.Click += StartMenu_Click;
// Stop Menu
stopMenu = cm.Items.Add(@"Stop");
stopMenu.Click += StopMenu_Click;
// Delete Menu
deleteMenu = cm.Items.Add(@"Delete");
deleteMenu.Click += DeleteMenu_Click;
cm.Items.Add(new ToolStripSeparator());
// Open web console menu
openWebConsoleMenu = cm.Items.Add(@"Launch Web Console");
openWebConsoleMenu.Click += OpenWebConsoleMenu_Click;
// Copy oc login command
copyOCLoginCommand = cm.Items.Add(@"Copy OC Login Command");
// Copy oc login command: developer
copyOCLoginForDeveloperMenu = (copyOCLoginCommand as ToolStripMenuItem).DropDownItems.Add(@"Developer");
copyOCLoginForDeveloperMenu.Click += CopyOCLoginForDeveloperMenu_Click;
// Copy oc login command: kubeadmin
copyOCLoginForKubeadminMenu = (copyOCLoginCommand as ToolStripMenuItem).DropDownItems.Add(@"Kubeadmin");
copyOCLoginForKubeadminMenu.Click += CopyOCLoginForKubeadminMenu_Click;
cm.Items.Add(new ToolStripSeparator());
// About menu
aboutMenu = cm.Items.Add(@"About");
aboutMenu.Click += ShowAboutForm;
// Exit menu
exitMenu = cm.Items.Add(@"Exit");
exitMenu.Click += ExitMenu_Click;
this.notifyIcon.ContextMenuStrip = cm;
}
async private void CopyOCLoginForKubeadminMenu_Click(object sender, EventArgs e)
{
var consoleResult = await Task.Run(() => Handlers.HandleOCLoginForKubeadmin());
if (consoleResult != null)
{
if (consoleResult.Success)
Clipboard.SetText(string.Format("oc.exe login -u kubeadmin -p {0} {1}", consoleResult.ClusterConfig.KubeAdminPass, consoleResult.ClusterConfig.ClusterAPI));
else
ShowNotification(@"Could not find credentials, is CRC runnning?", ToolTipIcon.Error);
}
}
async private void CopyOCLoginForDeveloperMenu_Click(object sender, EventArgs e)
{
var consoleResult = await Task.Run(() => Handlers.HandleOCLogingForDeveloper());
if (consoleResult != null)
{
if (consoleResult.Success)
Clipboard.SetText(string.Format("oc.exe login -u developer -p developer {0}", consoleResult.ClusterConfig.ClusterAPI));
else
ShowNotification(@"Could not find credentials, is CRC running?", ToolTipIcon.Error);
}
}
async private void OpenWebConsoleMenu_Click(object sender, EventArgs e)
{
var consoleResult = await Task.Run(() => Handlers.HandleOpenWebConsole());
if (consoleResult != null)
{
if (consoleResult.Success)
System.Diagnostics.Process.Start(consoleResult.ClusterConfig.WebConsoleURL);
else
ShowNotification(@"Could not open web console, is CRC running?", ToolTipIcon.Error);
}
}
async private void DeleteMenu_Click(object sender, EventArgs e)
{
ShowNotification(@"Deleting Cluster", ToolTipIcon.Warning);
var deleteResult = await Task.Run(() => Handlers.HandleDelete());
if (deleteResult != null)
{
if (deleteResult.Success)
DisplayMessageBox.Info(@"CodeReady Containers Cluster has been deleted", @"Cluster Deleted");
else
DisplayMessageBox.Warn(@"Could not delete the cluster");
}
}
async private void StopMenu_Click(object sender, EventArgs e)
{
ShowNotification(@"Stopping Cluster", ToolTipIcon.Info);
var stopResult = await Task.Run(() => Handlers.HandleStop());
if (stopResult != null)
{
if (stopResult.Success)
DisplayMessageBox.Info(@"CodeReady Containers Cluster has stopped", @"Cluster Stopped");
else
DisplayMessageBox.Warn(@"Cluster did not stop. Please check detailed status");
}
}
async private void StartMenu_Click(object sender, EventArgs e)
{
ShowNotification(@"Starting Cluster", ToolTipIcon.Info);
var startResult = await Task.Run(() => Handlers.HandleStart());
if (startResult != null)
{
if (startResult.KubeletStarted)
DisplayMessageBox.Info(@"CodeReady Containers Cluster has started", @"Cluster Started");
else
DisplayMessageBox.Warn(@"Cluster did not start. Please check detailed status");
}
}
private void ExitMenu_Click(object sender, EventArgs e)
{
notifyIcon.Visible = false;
Application.Exit();
}
private void ShowAboutForm(object sender, EventArgs e)
{
Form about = new AboutForm();
if (!about.Visible)
about.Show();
}
private void ShowDetailedStatusForm(object sender, EventArgs e)
{
Form status = new StatusForm();
if (!status.Visible)
status.Show();
}
private void UpdateClusterStatusMenu(StatusResult status)
{
if (!string.IsNullOrEmpty(status.CrcStatus))
this.status.Text = status.CrcStatus;
else
this.status.Text = @"Unknown";
}
private void SyncMenuItemStates(StatusResult status)
{
if (status.CrcStatus == @"Running")
{
startMenu.Enabled = false;
deleteMenu.Enabled = true;
stopMenu.Enabled = true;
openWebConsoleMenu.Enabled = true;
copyOCLoginCommand.Enabled = true;
copyOCLoginForDeveloperMenu.Enabled = true;
copyOCLoginForKubeadminMenu.Enabled = true;
}
else if (status.CrcStatus == @"Stopped")
{
startMenu.Enabled = true;
deleteMenu.Enabled = true;
stopMenu.Enabled = false;
openWebConsoleMenu.Enabled = false;
copyOCLoginCommand.Enabled = false;
copyOCLoginForDeveloperMenu.Enabled = false;
copyOCLoginForKubeadminMenu.Enabled = false;
}
else if (!string.IsNullOrEmpty(status.Error) && status.Error.Contains("does not exist"))
{
startMenu.Enabled = true;
deleteMenu.Enabled = false;
stopMenu.Enabled = false;
openWebConsoleMenu.Enabled = false;
copyOCLoginCommand.Enabled = false;
copyOCLoginForDeveloperMenu.Enabled = false;
copyOCLoginForKubeadminMenu.Enabled = false;
}
else
{
startMenu.Enabled = true;
stopMenu.Enabled = false;
deleteMenu.Enabled = true;
openWebConsoleMenu.Enabled = false;
copyOCLoginCommand.Enabled = false;
copyOCLoginForDeveloperMenu.Enabled = false;
copyOCLoginForKubeadminMenu.Enabled = false;
}
}
public void ShowNotification(string msg, ToolTipIcon toolTipIcon)
{
notifyIcon.BalloonTipIcon = toolTipIcon;
notifyIcon.BalloonTipText = msg;
notifyIcon.ShowBalloonTip(10);
}
}
}