@facets-cloud/facetsctl
Version:
150 lines (149 loc) • 6.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const configuration_1 = require("../utils/configuration");
const FacetsAPI = require("../services/facets-api");
const Listr = require("listr");
const errors_1 = require("@oclif/core/lib/errors");
var RegistrationTypes;
(function (RegistrationTypes) {
RegistrationTypes["ENVIRONMENT"] = "ENVIRONMENT";
RegistrationTypes["RELEASE_STREAM"] = "RELEASE_STREAM";
})(RegistrationTypes || (RegistrationTypes = {}));
class UploadFile extends core_1.Command {
constructor() {
super(...arguments);
this.showOriginalError = false;
this.checkUserCredentials = (ctx) => {
try {
const config = (0, configuration_1.readConfig)();
ctx.config = config;
}
catch (error) {
if (!error.message) {
error.message =
"Error while fetching user credentials! Make sure you are logged in before pushing";
}
throw new errors_1.CLIError(error, { exit: 2 });
}
};
this.uploadNewFile = async (ctx) => {
try {
const { flags } = await this.parse(UploadFile);
const config = ctx.config;
const filePath = flags["file-path"] || "";
const stackName = flags["blueprint-name"] || "";
const gitRef = flags["git-ref"] || "";
const artifactRequest = {
applicationName: flags["artifact-name"],
description: flags.description || "",
externalId: flags["external-id"] || "",
artifactUri: "null",
stackName: stackName,
};
if (flags["git-ref"]) {
artifactRequest.metadata = {
"git-ref": flags["git-ref"],
BRANCH_NAME: flags["git-ref"],
};
}
else {
artifactRequest.registrationType = flags["registration-type"];
const registrationValueKey = flags["registration-type"] === RegistrationTypes.ENVIRONMENT
? "clusterId"
: "releaseStream";
artifactRequest[registrationValueKey] = flags["registration-value"];
artifactRequest.metadata = {};
}
if ((stackName === null || stackName === void 0 ? void 0 : stackName.length) > 0 && (gitRef === null || gitRef === void 0 ? void 0 : gitRef.length) > 0) {
artifactRequest.registrationType = flags["registration-type"];
}
await FacetsAPI.uploadArtifactsZip(config === null || config === void 0 ? void 0 : config.ControlPlaneURL, config === null || config === void 0 ? void 0 : config.Username, config === null || config === void 0 ? void 0 : config.AccessToken, filePath, artifactRequest);
}
catch (error) {
if (this.showOriginalError) {
throw new errors_1.CLIError(error, { exit: 2 });
}
if (error.code === "ENOTFOUND") {
error.message = "Incorrect URL of Control Plane!";
}
throw new errors_1.CLIError(error, { exit: 2 });
}
};
}
async run() {
const { flags } = await this.parse(UploadFile);
this.showOriginalError = flags.debug === "true";
if (!((flags["registration-type"] && flags["registration-value"]) ||
flags["git-ref"])) {
throw new errors_1.CLIError("Either (registration-type and registration-value) or git-ref are required", { exit: 2 });
}
const tasks = new Listr([
{
title: "Checking user credentials",
task: this.checkUserCredentials,
},
{
title: "Uploading file",
task: this.uploadNewFile,
},
]);
try {
await tasks.run();
}
catch (error) {
// Throwing the error prints a duplicate message from bin/run as well, however that is fine to go with
throw error;
}
}
}
exports.default = UploadFile;
UploadFile.description = "Upload any type of application build file to the Facets Control Plane";
UploadFile.examples = ["<%= config.bin %> <%= command.id %>"];
UploadFile.flags = {
"file-path": core_1.Flags.string({
char: "p",
description: "path to the application build file (e.g., WAR, JAR, ZIP) on your local system",
required: true,
}),
"artifact-name": core_1.Flags.string({
char: "a",
description: "name of the artifact-ci as mentioned in resource spec. If the CI integration is already created, provide its name. If not, additionally provide blueprint name and the registration type using respective flags.'",
required: true,
}),
"registration-type": core_1.Flags.string({
description: "mode of registration",
options: [
RegistrationTypes.ENVIRONMENT,
RegistrationTypes.RELEASE_STREAM,
],
required: false,
}),
"registration-value": core_1.Flags.string({
description: "value for release stream or cluster id, based on mode of registration",
required: false,
}),
"git-ref": core_1.Flags.string({
description: "Git ref provided in CI Rule",
required: false,
}),
"blueprint-name": core_1.Flags.string({
description: "name of the blueprint where the CI integration will be created.",
}),
description: core_1.Flags.string({
char: "d",
description: "description of the build if any",
required: false,
}),
"external-id": core_1.Flags.string({
char: "e",
description: "external identifier for CI Integration",
required: true,
}),
debug: core_1.Flags.string({
description: "See original error message thrown",
required: false,
default: "false",
}),
};
UploadFile.args = [];