-
Notifications
You must be signed in to change notification settings - Fork 90
/
actions.ts
358 lines (343 loc) · 13.3 KB
/
actions.ts
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* This program and the accompanying materials are made available under the terms of the *
* Eclipse Public License v2.0 which accompanies this distribution, and is available at *
* https://www.eclipse.org/legal/epl-v20.html *
* *
* SPDX-License-Identifier: EPL-2.0 *
* *
* Copyright Contributors to the Zowe Project. *
* *
*/
import * as vscode from "vscode";
import * as zowe from "@zowe/cli";
import { errorHandling } from "../utils/ProfilesUtils";
import { Profiles } from "../Profiles";
import { ZoweExplorerApiRegister } from "../ZoweExplorerApiRegister";
import { ValidProfileEnum, IZoweTree, IZoweJobTreeNode } from "@zowe/zowe-explorer-api";
import { Job } from "./ZoweJobNode";
import * as nls from "vscode-nls";
import { toUniqueJobFileUri } from "../SpoolProvider";
import { IProfileLoaded } from "@zowe/imperative";
import * as globals from "../globals";
import { refreshAll as refreshAllJobs } from "../shared/refresh";
// Set up localization
nls.config({
messageFormat: nls.MessageFormat.bundle,
bundleFormat: nls.BundleFormat.standalone,
})();
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
/**
* Download all the spool content for the specified job.
*
* @param job The job to download the spool content from
*/
export async function downloadSpool(job: IZoweJobTreeNode) {
try {
const dirUri = await vscode.window.showOpenDialog({
openLabel: localize("downloadSpool.select", "Select"),
canSelectFolders: true,
canSelectFiles: false,
canSelectMany: false,
});
if (dirUri !== undefined) {
ZoweExplorerApiRegister.getJesApi(job.getProfile()).downloadSpoolContent({
jobid: job.job.jobid,
jobname: job.job.jobname,
outDir: dirUri[0].fsPath,
});
}
} catch (error) {
await errorHandling(error, null, error.message);
}
}
/**
* Download the spool content for the specified job
*
* @param session The session to which the job belongs
* @param spool The IJobFile to get the spool content for
* @param refreshTimestamp The timestamp of the last job node refresh
*/
export async function getSpoolContent(session: string, spool: zowe.IJobFile, refreshTimestamp: number) {
const profiles = Profiles.getInstance();
let zosmfProfile: IProfileLoaded;
try {
zosmfProfile = profiles.loadNamedProfile(session);
} catch (error) {
await errorHandling(error, session, error.message);
return;
}
await profiles.checkCurrentProfile(zosmfProfile);
if (profiles.validProfile === ValidProfileEnum.VALID || profiles.validProfile === ValidProfileEnum.UNVERIFIED) {
const uri = toUniqueJobFileUri(session, spool)(refreshTimestamp.toString());
try {
await vscode.window.showTextDocument(uri);
} catch (error) {
await errorHandling(error, session, error.message);
return;
}
}
}
/**
* Refresh a node in the job tree
*
* @param node The node to refresh
* @param jobsProvider The tree to which the refreshed node belongs
*/
export async function refreshJobsServer(node: IZoweJobTreeNode, jobsProvider: IZoweTree<IZoweJobTreeNode>) {
jobsProvider.checkCurrentProfile(node);
if (
Profiles.getInstance().validProfile === ValidProfileEnum.VALID ||
Profiles.getInstance().validProfile === ValidProfileEnum.UNVERIFIED
) {
await jobsProvider.refreshElement(node);
}
}
/**
* Refresh a job node information and spool files in the job tree
*
* @param job The job node to refresh
* @param jobsProvider The tree to which the refreshed node belongs
*/
export function refreshJob(job: Job, jobsProvider: IZoweTree<IZoweJobTreeNode>) {
jobsProvider.refreshElement(job);
}
/**
* Download the JCL content for the specified job.
*
* @param job The job to download the JCL content from
*/
export async function downloadJcl(job: Job) {
try {
const jobJcl = await ZoweExplorerApiRegister.getJesApi(job.getProfile()).getJclForJob(job.job);
const jclDoc = await vscode.workspace.openTextDocument({ language: "jcl", content: jobJcl });
await vscode.window.showTextDocument(jclDoc);
} catch (error) {
await errorHandling(error, null, error.message);
}
}
/**
* Focus of the specified job in the tree
* @param jobsProvider is a jobs tree
* @param sessionName is a profile name to use in the jobs tree
* @param jobId is a job to focus on
*/
export const focusOnJob = async (jobsProvider: IZoweTree<IZoweJobTreeNode>, sessionName: string, jobId: string) => {
let sessionNode: IZoweJobTreeNode | undefined = jobsProvider.mSessionNodes.find(
(jobNode) => jobNode.label.trim() === sessionName.trim()
);
if (!sessionNode) {
try {
await jobsProvider.addSession(sessionName);
} catch (error) {
await errorHandling(error, null, error.message);
return;
}
sessionNode = jobsProvider.mSessionNodes.find((jobNode) => jobNode.label === sessionName);
}
try {
jobsProvider.refreshElement(sessionNode);
} catch (error) {
await errorHandling(error, null, error.message);
return;
}
sessionNode.searchId = jobId;
const jobs: IZoweJobTreeNode[] = await sessionNode.getChildren();
const job = jobs.find((jobNode) => jobNode.job.jobid === jobId);
if (job) {
jobsProvider.setItem(jobsProvider.getTreeView(), job);
}
};
/**
* Modify a job command
*
* @param job The job on which to modify a command
*/
export async function modifyCommand(job: Job) {
try {
const command = await vscode.window.showInputBox({
prompt: localize("jobActions.modifyCommand.command.prompt", "Modify Command"),
});
if (command !== undefined) {
const commandApi = ZoweExplorerApiRegister.getInstance().getCommandApi(job.getProfile());
if (commandApi) {
const response = await ZoweExplorerApiRegister.getCommandApi(job.getProfile()).issueMvsCommand(
`f ${job.job.jobname},${command}`
);
vscode.window.showInformationMessage(
localize("jobActions.modifyCommand.response", "Command response: ") + response.commandResponse
);
}
}
} catch (error) {
if (error.toString().includes("non-existing")) {
vscode.window.showErrorMessage(
localize("jobActions.modifyCommand.apiNonExisting", "Not implemented yet for profile of type: ") +
job.getProfile().type
);
} else {
await errorHandling(error.toString(), job.getProfile().name, error.message.toString());
}
}
}
/**
* Stop a job command
*
* @param job The job on which to stop a command
*/
export async function stopCommand(job: Job) {
try {
const commandApi = ZoweExplorerApiRegister.getInstance().getCommandApi(job.getProfile());
if (commandApi) {
const response = await ZoweExplorerApiRegister.getCommandApi(job.getProfile()).issueMvsCommand(
`p ${job.job.jobname}`
);
vscode.window.showInformationMessage(
localize("jobActions.stopCommand.response", "Command response: ") + response.commandResponse
);
}
} catch (error) {
if (error.toString().includes("non-existing")) {
vscode.window.showErrorMessage(
localize("jobActions.stopCommand.apiNonExisting", "Not implemented yet for profile of type: ") +
job.getProfile().type
);
} else {
await errorHandling(error.toString(), job.getProfile().name, error.message.toString());
}
}
}
/**
* Set the owner of a job
*
* @param job The job to set the owner of
* @param jobsProvider The tree to which the updated node belongs
*/
// Is this redundant with the setter in the Job class (ZoweJobNode.ts)?
export async function setOwner(job: IZoweJobTreeNode, jobsProvider: IZoweTree<IZoweJobTreeNode>) {
const newOwner = await vscode.window.showInputBox({ prompt: localize("setOwner.newOwner.prompt.owner", "Owner") });
job.owner = newOwner;
jobsProvider.refreshElement(job);
}
/**
* Set the prefix of a job
*
* @param job The job to set the prefix of
* @param jobsProvider The tree to which the updated node belongs
*/
export async function setPrefix(job: IZoweJobTreeNode, jobsProvider: IZoweTree<IZoweJobTreeNode>) {
const newPrefix = await vscode.window.showInputBox({
prompt: localize("setOwner.newOwner.prompt.prefix", "Prefix"),
});
job.prefix = newPrefix;
jobsProvider.refreshElement(job);
}
/**
* Delete the selected jobs command
*
* @param jobsProvider The tree to which the node belongs
*/
export async function deleteCommand(
jobsProvider: IZoweTree<IZoweJobTreeNode>,
job?: IZoweJobTreeNode,
jobs?: IZoweJobTreeNode[]
) {
if (jobs && jobs.length) {
await deleteMultipleJobs(
jobs.filter((jobNode) => jobNode.job !== undefined && jobNode.job !== null),
jobsProvider
);
return;
}
if (job) {
await deleteSingleJob(job, jobsProvider);
return;
}
}
async function deleteSingleJob(job: IZoweJobTreeNode, jobsProvider: IZoweTree<IZoweJobTreeNode>): Promise<void> {
const jobName = `${job.job.jobname}(${job.job.jobid})`;
const message = localize(
"deleteJobPrompt.confirmation.message",
"Are you sure you want to delete the following item?\nThis will permanently remove the following job from your system.\n\n{0}",
jobName.replace(/(,)/g, "\n")
);
const deleteButton = localize("deleteJobPrompt.confirmation.delete", "Delete");
const result = await vscode.window.showWarningMessage(message, { modal: true }, deleteButton);
if (!result || result === "Cancel") {
globals.LOG.debug(localize("deleteJobPrompt.confirmation.cancel.log.debug", "Delete action was canceled."));
vscode.window.showInformationMessage(
localize("deleteJobPrompt.deleteCancelled", "Delete action was cancelled.")
);
return;
}
try {
await jobsProvider.delete(job);
} catch (error) {
await errorHandling(error.toString(), job.getProfile().name, error.message.toString());
return;
}
await refreshAllJobs(jobsProvider);
vscode.window.showInformationMessage(localize("deleteCommand.job", "Job {0} deleted.", jobName));
}
async function deleteMultipleJobs(
jobs: ReadonlyArray<IZoweJobTreeNode>,
jobsProvider: IZoweTree<IZoweJobTreeNode>
): Promise<void> {
const deleteButton = localize("deleteJobPrompt.confirmation.delete", "Delete");
const toJobname = (jobNode: IZoweJobTreeNode) => `${jobNode.job.jobname}(${jobNode.job.jobid})`;
const message = localize(
"deleteJobPrompt.confirmation.message",
"Are you sure you want to delete the following {0} items?\nThis will permanently remove the following jobs from your system.\n\n{1}",
jobs.length,
jobs.map(toJobname).toString().replace(/(,)/g, "\n")
);
const deleteChoice = await vscode.window.showWarningMessage(message, { modal: true }, deleteButton);
if (!deleteChoice || deleteChoice === "Cancel") {
globals.LOG.debug(localize("deleteJobPrompt.confirmation.cancel.log.debug", "Delete action was canceled."));
vscode.window.showInformationMessage(
localize("deleteJobPrompt.deleteCancelled", "Delete action was cancelled.")
);
return;
}
const deletionResult: ReadonlyArray<IZoweJobTreeNode | Error> = await Promise.all(
jobs.map(async (job) => {
try {
await jobsProvider.delete(job);
return job;
} catch (error) {
return error;
}
})
);
const deletedJobs: ReadonlyArray<IZoweJobTreeNode> = deletionResult
.map((result) => {
if (result instanceof Error) {
return undefined;
}
return result;
})
.filter((result) => result !== undefined);
if (deletedJobs.length) {
await refreshAllJobs(jobsProvider);
vscode.window.showInformationMessage(
localize(
"deleteCommand.multipleJobs",
"The following jobs were deleted: {0}",
deletedJobs.map(toJobname).toString().replace(/(,)/g, ", ")
)
);
}
const deletionErrors: ReadonlyArray<Error> = deletionResult
.map((result) => {
if (result instanceof Error) {
const error = result;
return error;
}
return undefined;
})
.filter((result) => result !== undefined);
if (deletionErrors.length) {
const errorMessages = deletionErrors.map((error) => error.message).join(", ");
const userMessage = `There were errors during jobs deletion: ${errorMessages}`;
await errorHandling(userMessage);
}
}