Skip to content

Commit 8130e3f

Browse files
committed
Refactor clusterViewLoader before creating base class for webviews
Signed-off-by: Denis Golovin dgolovin@redhat.com
1 parent aff5412 commit 8130e3f

File tree

1 file changed

+98
-98
lines changed

1 file changed

+98
-98
lines changed

src/webview/cluster/clusterViewLoader.ts

Lines changed: 98 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,101 @@ let panel: vscode.WebviewPanel;
1515

1616
const channel: vscode.OutputChannel = vscode.window.createOutputChannel('CRC Logs');
1717

18+
async function clusterEditorMessageListener (event: any ): Promise<any> {
19+
if (['openLaunchSandboxPage', 'openCreateClusterPage', 'openCrcAddClusterPage'].includes(event.action)) {
20+
await vscode.commands.executeCommand(`openshift.explorer.addCluster.${event.action}`, event.params?.url);
21+
}
22+
23+
if (event.action === 'run') {
24+
const terminal: vscode.Terminal = WindowUtil.createTerminal('OpenShift: CRC Setup', undefined);
25+
terminal.sendText(`${event.data} setup`);
26+
terminal.show();
27+
}
28+
if (event.action === 'start') {
29+
let startProcess: ChildProcess;
30+
channel.show();
31+
if (event.isSetting) {
32+
const binaryFromSetting = vscode.workspace.getConfiguration('openshiftConnector').get('crcBinaryLocation');
33+
const pullSecretFromSetting = vscode.workspace.getConfiguration('openshiftConnector').get('crcPullSecretPath');
34+
const cpuFromSetting = vscode.workspace.getConfiguration('openshiftConnector').get('crcCpuCores');
35+
const memoryFromSetting = vscode.workspace.getConfiguration('openshiftConnector').get('crcMemoryAllocated');
36+
const crcOptions = ['start', '-p', `${pullSecretFromSetting}`, '-c', `${cpuFromSetting}`, '-m', `${memoryFromSetting}`, '-ojson'];
37+
startProcess = spawn(`${binaryFromSetting}`, crcOptions);
38+
channel.append(`\n\n${binaryFromSetting} ${crcOptions.join(' ')}\n`);
39+
} else {
40+
const configuration = vscode.workspace.getConfiguration('openshiftConnector');
41+
configuration.update('crcBinaryLocation', event.crcLoc, vscode.ConfigurationTarget.Global);
42+
configuration.update('crcPullSecretPath', event.pullSecret, vscode.ConfigurationTarget.Global);
43+
configuration.update('crcCpuCores', event.cpuSize, vscode.ConfigurationTarget.Global);
44+
configuration.update('crcMemoryAllocated', Number.parseInt(event.memory, 10), vscode.ConfigurationTarget.Global);
45+
const [tool, ...params] = event.data.split(' ');
46+
startProcess = spawn(tool, params);
47+
channel.append(`\n\n${tool} ${params.join(' ')}\n`);
48+
}
49+
startProcess.stdout.setEncoding('utf8');
50+
startProcess.stderr.setEncoding('utf8');
51+
startProcess.stdout.on('data', (chunk) => {
52+
channel.append(chunk);
53+
});
54+
startProcess.stderr.on('data', (chunk) => {
55+
channel.append(chunk);
56+
});
57+
startProcess.on('close', (code) => {
58+
// eslint-disable-next-line no-console
59+
console.log(`crc start exited with code ${code}`);
60+
if (code !== 0) {
61+
panel.webview.postMessage({action: 'sendcrcstarterror'})
62+
}
63+
const binaryLoc = event.isSetting ? vscode.workspace.getConfiguration('openshiftConnector').get('crcBinaryLocation'): event.crcLoc;
64+
ClusterViewLoader.checkCrcStatus(binaryLoc, 'crcstartstatus', panel);
65+
});
66+
}
67+
if (event.action === 'stop') {
68+
let filePath: string;
69+
channel.show();
70+
if (event.data === '') {
71+
filePath = vscode.workspace.getConfiguration('openshiftConnector').get('crcBinaryLocation');
72+
} else {
73+
filePath = event.data;
74+
}
75+
const stopProcess = spawn(`${filePath}`, ['stop']);
76+
channel.append(`\n\n$${filePath} stop\n`);
77+
stopProcess.stdout.setEncoding('utf8');
78+
stopProcess.stderr.setEncoding('utf8');
79+
stopProcess.stdout.on('data', (chunk) => {
80+
channel.append(chunk);
81+
});
82+
stopProcess.stderr.on('data', (chunk) => {
83+
channel.append(chunk);
84+
});
85+
stopProcess.on('close', (code) => {
86+
// eslint-disable-next-line no-console
87+
console.log(`crc stop exited with code ${code}`);
88+
ClusterViewLoader.checkCrcStatus(filePath, 'crcstopstatus', panel);
89+
});
90+
}
91+
if (event.action === 'checksetting') {
92+
const binaryFromSetting:string = vscode.workspace.getConfiguration('openshiftConnector').get('crcBinaryLocation');
93+
if (binaryFromSetting) {
94+
panel.webview.postMessage({action: 'crcsetting'});
95+
ClusterViewLoader.checkCrcStatus(binaryFromSetting, 'crcstatus', panel);
96+
}
97+
}
98+
if (event.action === 'checkcrcstatus') {
99+
ClusterViewLoader.checkCrcStatus(event.data, 'crcstatus', panel);
100+
}
101+
102+
if (event.action === 'crclogin') {
103+
vscode.commands.executeCommand(
104+
'openshift.explorer.login.credentialsLogin',
105+
true,
106+
event.url,
107+
event.data.username,
108+
event.data.password
109+
);
110+
}
111+
}
112+
18113
export default class ClusterViewLoader {
19114
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
20115
static get extensionPath() {
@@ -38,9 +133,6 @@ export default class ClusterViewLoader {
38133

39134
// eslint-disable-next-line @typescript-eslint/require-await
40135
static async loadView(title: string): Promise<vscode.WebviewPanel> {
41-
42-
let startProcess: ChildProcess;
43-
let stopProcess: ChildProcess;
44136
const localResourceRoot = vscode.Uri.file(path.join(ClusterViewLoader.extensionPath, 'out', 'clusterViewer'));
45137
if (panel) {
46138
// If we already have a panel, show it in the target column
@@ -58,103 +150,11 @@ export default class ClusterViewLoader {
58150
panel.onDidDispose(()=> {
59151
panel = undefined;
60152
});
61-
panel.webview.onDidReceiveMessage(async (event) => {
62-
if (['openLaunchSandboxPage', 'openCreateClusterPage', 'openCrcAddClusterPage'].includes(event.action)) {
63-
await vscode.commands.executeCommand(`openshift.explorer.addCluster.${event.action}`, event.params?.url);
64-
}
65-
66-
if (event.action === 'run') {
67-
const terminal: vscode.Terminal = WindowUtil.createTerminal('OpenShift: CRC Setup', undefined);
68-
terminal.sendText(`${event.data} setup`);
69-
terminal.show();
70-
}
71-
if (event.action === 'start') {
72-
channel.show();
73-
if (event.isSetting) {
74-
const binaryFromSetting= vscode.workspace.getConfiguration('openshiftConnector').get('crcBinaryLocation');
75-
const pullSecretFromSetting= vscode.workspace.getConfiguration('openshiftConnector').get('crcPullSecretPath');
76-
const cpuFromSetting= vscode.workspace.getConfiguration('openshiftConnector').get('crcCpuCores');
77-
const memoryFromSetting= vscode.workspace.getConfiguration('openshiftConnector').get('crcMemoryAllocated');
78-
const crcOptions = ['start', '-p', `${pullSecretFromSetting}`, '-c', `${cpuFromSetting}`, '-m', `${memoryFromSetting}`, '-ojson'];
79-
startProcess = spawn(`${binaryFromSetting}`, crcOptions);
80-
channel.append(`\n\n${binaryFromSetting} ${crcOptions.join(' ')}\n`);
81-
} else {
82-
const configuration = vscode.workspace.getConfiguration('openshiftConnector');
83-
configuration.update('crcBinaryLocation', event.crcLoc, vscode.ConfigurationTarget.Global);
84-
configuration.update('crcPullSecretPath', event.pullSecret, vscode.ConfigurationTarget.Global);
85-
configuration.update('crcCpuCores', event.cpuSize, vscode.ConfigurationTarget.Global);
86-
configuration.update('crcMemoryAllocated', Number.parseInt(event.memory, 10), vscode.ConfigurationTarget.Global);
87-
const [tool, ...params] = event.data.split(' ');
88-
startProcess = spawn(tool, params);
89-
channel.append(`\n\n${tool} ${params.join(' ')}\n`);
90-
}
91-
startProcess.stdout.setEncoding('utf8');
92-
startProcess.stderr.setEncoding('utf8');
93-
startProcess.stdout.on('data', (chunk) => {
94-
channel.append(chunk);
95-
});
96-
startProcess.stderr.on('data', (chunk) => {
97-
channel.append(chunk);
98-
});
99-
startProcess.on('close', (code) => {
100-
// eslint-disable-next-line no-console
101-
console.log(`crc start exited with code ${code}`);
102-
if (code !== 0) {
103-
panel.webview.postMessage({action: 'sendcrcstarterror'})
104-
}
105-
const binaryLoc = event.isSetting ? vscode.workspace.getConfiguration('openshiftConnector').get('crcBinaryLocation'): event.crcLoc;
106-
ClusterViewLoader.checkCrcStatus(binaryLoc, 'crcstartstatus', panel);
107-
});
108-
}
109-
if (event.action === 'stop') {
110-
let filePath: string;
111-
channel.show();
112-
if (event.data === '') {
113-
filePath = vscode.workspace.getConfiguration('openshiftConnector').get('crcBinaryLocation');
114-
} else {
115-
filePath = event.data;
116-
}
117-
stopProcess = spawn(`${filePath}`, ['stop']);
118-
channel.append(`\n\n$${filePath} stop\n`);
119-
stopProcess.stdout.setEncoding('utf8');
120-
stopProcess.stderr.setEncoding('utf8');
121-
stopProcess.stdout.on('data', (chunk) => {
122-
channel.append(chunk);
123-
});
124-
stopProcess.stderr.on('data', (chunk) => {
125-
channel.append(chunk);
126-
});
127-
stopProcess.on('close', (code) => {
128-
// eslint-disable-next-line no-console
129-
console.log(`crc stop exited with code ${code}`);
130-
ClusterViewLoader.checkCrcStatus(filePath, 'crcstopstatus', panel);
131-
});
132-
}
133-
if (event.action === 'checksetting') {
134-
const binaryFromSetting:string = vscode.workspace.getConfiguration('openshiftConnector').get('crcBinaryLocation');
135-
if (binaryFromSetting) {
136-
panel.webview.postMessage({action: 'crcsetting'});
137-
ClusterViewLoader.checkCrcStatus(binaryFromSetting, 'crcstatus', panel);
138-
}
139-
}
140-
if (event.action === 'checkcrcstatus') {
141-
ClusterViewLoader.checkCrcStatus(event.data, 'crcstatus', panel);
142-
}
143-
144-
if (event.action === 'crclogin') {
145-
vscode.commands.executeCommand(
146-
'openshift.explorer.login.credentialsLogin',
147-
true,
148-
event.url,
149-
event.data.username,
150-
event.data.password
151-
);
152-
}
153-
})
153+
panel.webview.onDidReceiveMessage(clusterEditorMessageListener);
154154
return panel;
155155
}
156156

157-
private static async checkCrcStatus(filePath: string, postCommand: string, p: vscode.WebviewPanel | undefined = undefined) {
157+
public static async checkCrcStatus(filePath: string, postCommand: string, p: vscode.WebviewPanel | undefined = undefined) {
158158
const crcCredArray = [];
159159
const crcVerInfo = await CliChannel.getInstance().execute(`${filePath} version -ojson`);
160160
channel.append(`\n\n${filePath} version -ojson\n`);
@@ -204,4 +204,4 @@ export default class ClusterViewLoader {
204204
.replace('%BASE_URL%', `${reactAppUri}`)
205205
.replace('<!-- meta http-equiv="Content-Security-Policy" -->', meta);
206206
}
207-
}
207+
}

0 commit comments

Comments
 (0)