UNPKG

@facets-cloud/facetsctlv3

Version:
118 lines (117 loc) 5.09 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const config_service_1 = require("../../services/config-service"); const artifact_service_1 = require("../../services/artifact-service"); const facets_api_1 = require("../../services/facets-api"); const axios_1 = __importDefault(require("axios")); class Upload extends core_1.Command { static description = 'Upload any type of application build file to the Facets Control Plane'; static examples = [ `$ facetsctl artifact upload --type GIT_REF --file-path file-to-upload --value my-git-ref Successfully uploaded the file file-to-upload Check out the registered build at: https://example.com/builds/1234 `, ]; static flags = { type: core_1.Flags.string({ char: 't', description: 'Type (GIT_REF, ENVIRONMENT, RELEASE_STREAM)', options: ['GIT_REF', 'ENVIRONMENT', 'RELEASE_STREAM'], required: true, }), 'file-path': core_1.Flags.string({ char: 'f', description: 'path to the application build file (e.g., WAR, JAR, ZIP) on your local system', required: true, }), value: core_1.Flags.string({ char: 'v', description: 'Value of the specified type', required: true, }), runId: core_1.Flags.string({ char: 'r', description: 'Optional run ID to corelate to your CI system, else it will be time', required: false, }), }; async run() { const { flags } = await this.parse(Upload); const { type, 'file-path': filePath, value, runId } = flags; // Generate timestamp if runId is not provided const generateTimestamp = () => { const now = new Date(); const dd = String(now.getDate()).padStart(2, '0'); const mm = String(now.getMonth() + 1).padStart(2, '0'); // January is 0! const yyyy = now.getFullYear(); const hh = String(now.getHours()).padStart(2, '0'); const min = String(now.getMinutes()).padStart(2, '0'); const ss = String(now.getSeconds()).padStart(2, '0'); return `${dd}-${mm}-${yyyy} ${hh}:${min}:${ss}`; }; const finalRunId = runId || generateTimestamp(); // Read the main configuration const configFilePath = config_service_1.ConfigService.findConfigFile(); if (!configFilePath) { this.error('Configuration file not found. Please ensure you are logged in.'); return; } const config = config_service_1.ConfigService.readConfig(configFilePath); this.log('Logged in to: ' + config.ControlPlaneURL + ' with user: ' + config.Username); // Read the artifact configuration let artifactInfo; try { artifactInfo = artifact_service_1.ArtifactService.readArtifactInfo(); } catch (error) { this.error('Artifact configuration file not found. Please run `facetsctl artifact init` first.'); return; } core_1.ux.action.start('uploading file'); let response; const stackName = flags["blueprint-name"] || ""; const gitRef = flags["git-ref"] || ""; const artifactRequest = { applicationName: artifactInfo.serviceName || artifactInfo.ciIntegrationName || '', description: flags.description || "", externalId: runId, artifactUri: "null", stackName: artifactInfo.projectName, }; if (flags.type === 'GIT_REF') { artifactRequest.metadata = { "git-ref": flags.value, BRANCH_NAME: flags.value, }; } else { artifactRequest.registrationType = type; const registrationValueKey = flags.type === 'ENVIRONMENT' ? "clusterId" : "releaseStream"; artifactRequest[registrationValueKey] = flags.value; artifactRequest.metadata = {}; } if (stackName?.length > 0 && gitRef?.length > 0) { artifactRequest.registrationType = flags.type; } try { response = await (0, facets_api_1.uploadArtifactsZip)(config?.ControlPlaneURL, config?.Username, config?.AccessToken, filePath, artifactRequest); } catch (error) { core_1.ux.action.stop('Failed'); if (axios_1.default.isAxiosError(error)) { this.error(`Failed to upload file: ${error.response?.data?.message}`); } return; } core_1.ux.action.stop('Done'); this.log(`Successfully uploaded ${filePath} with runId ${finalRunId}`); this.log(`Check out the uploaded file at: ${config.ControlPlaneURL}`); } } exports.default = Upload;