@broadcom/endevor-bridge-for-git-for-zowe-cli
Version:
Endevor Bridge for Git plug-in for Zowe CLI
95 lines (91 loc) • 4.15 kB
JavaScript
;
var lodash = require('lodash');
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 OptionUtils {
/**
* It is used to add the default value to the description of the option
*
* @param defaultValue value
*/
static defaultValueDescription(defaultValue) {
return `\n\nDefault: ${defaultValue}`;
}
constructor(optionValidator) {
this.optionValidator = optionValidator;
}
/**
* Returns the value of a required option. Precedence: arguments, profile
*
* @param isRequired if true, the option is added to missing options if not found
* @param optionDefinition definition of the specific option
* @param args list of arguments that contains the option
* @param profile? profile that contains the option
* @param baseProfile? base profile that contains the option
* @param defaultValue default value if not found
*/
getOption(isRequired, optionDefinition, args, defaultValue, profile, baseProfile) {
let optionValue = args[lodash.camelCase(optionDefinition.name)];
// if there is no argument value and there is service profile get it from there
if (object.isNil(optionValue) && object.isNotNil(profile)) {
// profile value has priority if only if the argument value is undefined
optionValue = profile[this.getProfileFieldName(optionDefinition)];
}
// if there is no argument value and the option is not found in the service profile , get it from base profile
if (object.isNil(optionValue) && object.isNotNil(baseProfile)) {
optionValue = baseProfile[this.getProfileFieldName(optionDefinition)];
}
// otherwise get the default value
if (object.isNil(optionValue) && object.isNotNil(defaultValue)) {
optionValue = defaultValue;
}
if (isRequired && object.isNil(optionValue)) {
this.optionValidator.addMissingOption(optionDefinition);
}
return optionValue;
}
/**
* Returns the value of an option passed as an argument in the command. If it's not specified it returns undefined.
*/
getOptionFromArgument(optionDefinition, args) {
return args[lodash.camelCase(optionDefinition.name)];
}
/**
* Returns the prefix of the options. It is used when using external profiles (e.g. Endevor, z/OSMF)
*/
getOptionPrefix() {
// By default the options do not have prefix
return "";
}
/**
* Returns the key of the profile field in camel case. The options created from profiles must always contain the original field name.
* E.g: 'my-field' in the profile 'my-profile' must be translated to 'my-profile-my-field' so that the field name is 'myField'
*
* @param optionDefinition definition of the specific option
*/
getProfileFieldName(optionDefinition) {
return lodash.camelCase(optionDefinition.name.replace(this.getOptionPrefix(), ""));
}
}
exports.OptionUtils = OptionUtils;