@soos-io/api-client
Version:
This is the SOOS API Client for registered clients leveraging the various integrations to the SOOS platform. Register for a free trial today at https://app.soos.io/register
174 lines (173 loc) • 7.33 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArgumentParserBase = void 0;
const enums_1 = require("../enums");
const constants_1 = require("../constants");
const enums_2 = require("../enums");
const utilities_1 = require("../utilities");
const commander_1 = require("commander");
class ArgumentParserBase {
argumentParser;
description;
scanType;
scriptVersion;
integrationName;
integrationType;
constructor(description, scanType, scriptVersion, integrationName, integrationType) {
this.description = description;
this.scanType = scanType;
this.scriptVersion = scriptVersion;
this.integrationName = integrationName;
this.integrationType = integrationType;
this.argumentParser = (0, commander_1.createCommand)()
.description(`${this.description} v${this.scriptVersion}`)
.version(this.scriptVersion);
this.addCommonArguments(this.scriptVersion, this.integrationName, this.integrationType);
}
getIntegrateUrl(scanType) {
return `${constants_1.SOOS_CONSTANTS.Urls.App.Home}integrate/${scanType == enums_2.ScanType.CSA ? "containers" : (scanType ?? enums_2.ScanType.SCA).toLowerCase()}`;
}
parseCommanderOptions(argv) {
return this.argumentParser.parse(argv ?? process.argv).opts();
}
parseCommanderArguments() {
return Object.fromEntries(this.argumentParser.registeredArguments
.map((a) => a.name())
.map((name, index) => [name, this.argumentParser.args[index]]));
}
getCombinedCommanderOptionsAndArguments(argv) {
const options = this.parseCommanderOptions(argv);
const args = this.parseCommanderArguments();
return { ...args, ...options };
}
ensureNoEmptyArgument(name, value) {
if (!value.trim()) {
throw new Error(`${name} cannot be empty`);
}
return value;
}
addCommonArguments(scriptVersion, integrationName, integrationType) {
this.addArgument("apiKey", `SOOS API Key - get yours from ${this.getIntegrateUrl(this.scanType)}`, {
defaultValue: (0, utilities_1.getEnvVariable)(constants_1.SOOS_CONSTANTS.EnvironmentVariables.ApiKey) ?? undefined,
required: true,
});
this.addArgument("apiURL", "SOOS API URL", {
defaultValue: constants_1.SOOS_CONSTANTS.Urls.API.Analysis,
internal: true,
});
this.addArgument("clientId", `SOOS Client ID - get yours from ${this.getIntegrateUrl(this.scanType)}`, {
defaultValue: (0, utilities_1.getEnvVariable)(constants_1.SOOS_CONSTANTS.EnvironmentVariables.ClientId) ?? undefined,
required: true,
});
this.addEnumArgument("integrationName", enums_2.IntegrationName, "Integration Name", {
defaultValue: integrationName,
internal: true,
required: true,
});
this.addEnumArgument("integrationType", enums_1.IntegrationType, "Integration Type", {
defaultValue: integrationType,
internal: true,
required: true,
});
this.argumentParser.addOption(new commander_1.Option("--logLevel <logLevel>", "Minimum log level to display.")
.choices([enums_2.LogLevel.DEBUG, enums_2.LogLevel.INFO, enums_2.LogLevel.WARN, enums_2.LogLevel.FAIL, enums_2.LogLevel.ERROR])
.default(enums_2.LogLevel.INFO));
this.addArgument("scriptVersion", "Script Version", {
defaultValue: scriptVersion,
internal: true,
required: true,
});
}
addArgument(name, description, options) {
if (options?.useNoOptionKey) {
const argument = (0, commander_1.createArgument)(name, description);
if (options?.defaultValue) {
argument.default(options.defaultValue);
}
if (options?.internal) {
throw new Error("internal is not applicable");
}
if (options?.required) {
argument.argRequired();
if (!options?.argParser) {
argument.argParser((value) => this.ensureNoEmptyArgument(name, value));
}
}
if (options?.argParser) {
argument.argParser(options.argParser);
}
if (options?.choices) {
argument.choices(options.choices);
}
if (options?.isFlag) {
throw new Error("isFlag is not applicable");
}
this.argumentParser.addArgument(argument);
return;
}
const flags = options?.isFlag ? `--${name}` : `--${name} <${name}>`;
const option = (0, commander_1.createOption)(flags, description);
if (options?.defaultValue) {
option.default(options.defaultValue);
}
if (options?.internal) {
option.hideHelp();
}
if (options?.required) {
option.makeOptionMandatory(true);
if (!options?.argParser) {
option.argParser((value) => this.ensureNoEmptyArgument(name, value));
}
}
if (options?.argParser) {
if (options?.isFlag) {
throw new Error("argParser is not applicable");
}
option.argParser(options.argParser);
}
if (options?.choices) {
if (options?.isFlag) {
throw new Error("choices is not applicable");
}
option.choices(options.choices);
}
this.argumentParser.addOption(option);
}
addEnumArgument(name, enumObject, description, options) {
const descriptionWithOptions = `${description} Options: ${(0, utilities_1.getEnumOptions)(enumObject, options.excludeDefault)
.map(([, value]) => value)
.join(", ")}`;
const argParser = options?.allowMultipleValues
? (value) => {
return (value
.split(",")
.map((v) => v.trim())
.filter((v) => v !== "")
.map((v) => (0, utilities_1.ensureEnumValue)(enumObject, v, name, options?.excludeDefault))
.filter((v) => v !== undefined) ??
options.defaultValue ??
[]);
}
: (value) => {
return ((0, utilities_1.ensureEnumValue)(enumObject, value, name, options?.excludeDefault) ??
options.defaultValue);
};
this.addArgument(name, descriptionWithOptions, {
defaultValue: options?.defaultValue,
argParser,
internal: options?.internal,
required: options?.required,
});
}
preParseArguments(argv) {
this.argumentParser.allowUnknownOption().allowExcessArguments();
const all = this.getCombinedCommanderOptionsAndArguments(argv);
this.argumentParser.allowUnknownOption(false).allowExcessArguments(false);
return all;
}
parseArguments(argv) {
const all = this.getCombinedCommanderOptionsAndArguments(argv);
return all;
}
}
exports.ArgumentParserBase = ArgumentParserBase;
;