This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
ValidateProfileHandler.ts
119 lines (108 loc) · 4.99 KB
/
ValidateProfileHandler.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
/*
* 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 { isNullOrUndefined } from "util";
import { ICommandHandler, IHandlerParameters } from "../../../../cmd";
import { IImperativeError, ImperativeError } from "../../../../error";
import { Imperative } from "../../../index";
import {
IProfileValidationPlan,
IProfileValidationReport,
IProfileValidationTask,
ProfilesConstants,
ProfileValidator
} from "../../../../profiles";
import { Logger } from "../../../../logger";
import { ImperativeConfig } from "../../../../utilities";
/**
* Generic handler for validating a profile and printing a report in response
*/
export default class ValidateProfileHandler implements ICommandHandler {
/**
* Get an instance of the imperative logger.
* @private
* @type {Logger}
* @memberof ValidateProfileHandler
*/
private mLogger: Logger = Imperative.api.imperativeLogger;
public async process(params: IHandlerParameters): Promise<void> {
const profileType = params.definition.customize[ProfilesConstants.PROFILES_COMMAND_TYPE_KEY];
const manager = Imperative.api.profileManager(profileType);
let profileName = manager.getDefaultProfileName();
// if the user specified a specific profile, we can determine the name of the profile from that
if (params.arguments.profileName != null) {
profileName = params.arguments.profileName;
}
const profileToValidate = (await manager.load({failNotFound: true, name: profileName})).profile;
let plan: IProfileValidationPlan;
try {
// load the definition of the plan from the specified file path
// this will return the class definition of the plan
const planModule = Imperative.getProfileConfiguration(profileType).validationPlanModule;
plan = require(planModule);
// instantiate the plan object
plan = new (plan as any)();
} catch (e) {
const planLoadErr: IImperativeError = {
msg: "An error was encountered trying to load the plan to validate the Brightside profile.",
additionalDetails: e.message,
causeErrors: e
};
throw new ImperativeError(planLoadErr);
}
// if the user just requested that we print the plan rather than actually validate the profile
if (params.arguments[ProfileValidator.PRINT_PLAN_OPTION.name]) {
Logger.getImperativeLogger().debug("Printed plan for profile validation requested");
params.response.console.log(Buffer.from(ProfileValidator.getTextDisplayForPlan(plan, profileToValidate,
ImperativeConfig.instance.loadedConfig.primaryTextColor)));
const cleanTaskForJSONOutput = (task: IProfileValidationTask) => {
delete task.taskFunction;
if (!isNullOrUndefined(task.dependentTasks)) {
for (const dependent of task.dependentTasks) {
cleanTaskForJSONOutput(dependent);
}
}
};
// add the object version of the plan to the JSON response, without the task functions
// since they can't be printed
for (const task of plan.tasks) {
cleanTaskForJSONOutput(task);
}
params.response.data.setObj(plan);
return;
}
let report: IProfileValidationReport;
try {
const promise = ProfileValidator.validate(profileToValidate,
plan, ImperativeConfig.instance.loadedConfig.productDisplayName);
params.response.progress.startBar({task: promise.progress});
report = await promise;
params.response.data.setObj(report);
const reportText = Buffer.from(ProfileValidator.getTextDisplayForReport(report, plan,
ImperativeConfig.instance.loadedConfig.productDisplayName,
ImperativeConfig.instance.loadedConfig.primaryTextColor,
profileName,
profileType));
params.response.console.log(reportText);
} catch (validateError) {
const unexpectedError: IImperativeError = {
msg: "Encountered an unexpected exception " +
"while validating your profile. ",
additionalDetails: validateError.message,
causeErrors: validateError
};
params.response.console.error("Failed to validate profile due to unexpected exception");
throw new ImperativeError(unexpectedError);
}
if (report.overallResult !== "OK") {
throw new ImperativeError({msg: "The profile validation was not successful"});
}
}
}