@broadcom/endevor-bridge-for-git-for-zowe-cli
Version:
Endevor Bridge for Git plug-in for Zowe CLI
75 lines (71 loc) • 2.92 kB
JavaScript
;
var imperative = require('@zowe/imperative');
var object = require('../../utils/object.js');
/*
* Copyright (c) 2019 Broadcom. All Rights Reserved. The term
* "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This software and all information contained therein is
* confidential and proprietary and shall not be duplicated,
* used, disclosed, or disseminated in any way except as
* authorized by the applicable license agreement, without the
* express written permission of Broadcom. All authorized
* reproductions must be marked with this language.
*
* EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO
* THE EXTENT PERMITTED BY APPLICABLE LAW, BROADCOM PROVIDES THIS
* SOFTWARE WITHOUT WARRANTY OF ANY KIND, INCLUDING WITHOUT
* LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL BROADCOM
* BE LIABLE TO THE END USER OR ANY THIRD PARTY FOR ANY LOSS OR
* DAMAGE, DIRECT OR INDIRECT, FROM THE USE OF THIS SOFTWARE,
* INCLUDING WITHOUT LIMITATION, LOST PROFITS, BUSINESS
* INTERRUPTION, GOODWILL, OR LOST DATA, EVEN IF BROADCOM IS
* EXPRESSLY ADVISED OF SUCH LOSS OR DAMAGE.
*/
class OptionValidator {
constructor() {
this.missingOptions = new Map();
}
/**
* Adds the option as a missing option
*
* @param optionDefinition missing option
*/
addMissingOption(optionDefinition) {
const groupId = object.isNil(optionDefinition.group)
? OptionValidator.DEFAULT_OPTIONS_GROUP
: optionDefinition.group;
if (this.missingOptions.has(groupId)) {
this.missingOptions.get(groupId).push(optionDefinition.name);
}
else {
this.missingOptions.set(groupId, [optionDefinition.name]);
}
}
/**
* Checks that there is not missing required options. It must be checked at the beginning of the command handlers.
*/
validateRequiredOptions() {
if (this.missingOptions.size > 0) {
let msg = OptionValidator.MISSING_ARGS_MSG.concat("\n");
const sortedMap = new Map([...this.missingOptions.entries()].sort());
sortedMap.forEach((options, group) => (msg = msg
.concat("- ")
.concat(group)
.concat(": ")
.concat(options.join(", ").concat("\n"))));
throw new imperative.ImperativeError({ msg });
}
}
isMissingOptions(group) {
return this.missingOptions.has(group);
}
isMissingCredentials(session) {
return ((object.isNil(session.tokenValue) || object.isNil(session.tokenType)) &&
(object.isNil(session.user) || object.isNil(session.password)));
}
}
OptionValidator.MISSING_ARGS_MSG = "missing arguments ";
OptionValidator.DEFAULT_OPTIONS_GROUP = "Options";
exports.OptionValidator = OptionValidator;