UNPKG

@facets-cloud/facetsctlv3

Version:
98 lines (97 loc) 4.38 kB
"use strict"; 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"); class Register extends core_1.Command { static description = 'Register a Docker image with a specified type and value'; static examples = [ `$ facetsctl artifact register --type GIT_REF --docker-image my-docker-image:latest --value my-git-ref Successfully registered my-docker-image:latest 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, }), 'docker-image': core_1.Flags.string({ char: 'i', description: 'Docker image to register', 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(Register); const { type, 'docker-image': dockerImage, value, runId } = flags; if (/\s/.test(dockerImage)) { this.error('Invalid Docker image reference. Image names and tags must not contain spaces.'); } // 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; } // Construct the URI to register let uriToRegister = dockerImage; if (artifactInfo.repositoryUrl) { const tag = dockerImage.split(':')[1] || 'latest'; uriToRegister = `${artifactInfo.repositoryUrl}:${tag}`; } // Register the image via the API core_1.ux.action.start('Registering image'); let response; try { response = await (0, facets_api_1.registerImage)(config.ControlPlaneURL, config.Username, config.AccessToken, type, uriToRegister, value, artifactInfo.projectName, artifactInfo.serviceName, artifactInfo.artifactoryName, finalRunId, artifactInfo.ciIntegrationName); } catch (error) { core_1.ux.action.stop('Failed'); if (error instanceof Error) { this.error(`Failed to register Docker image: ${error.message}`); } return; } core_1.ux.action.stop('Done'); this.log(`Successfully registered ${dockerImage} with runId ${finalRunId}`); this.log(`Check out the registered build at: ${config.ControlPlaneURL}`); } } exports.default = Register;