UNPKG

balena-cli

Version:

The official balena Command Line Interface

100 lines (98 loc) 4.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const lazy_1 = require("../../utils/lazy"); const release_1 = require("../release"); const fs = require("fs"); const path = require("path"); const promises_1 = require("stream/promises"); const stream_1 = require("stream"); class ReleaseAssetDownloadCmd extends core_1.Command { async run() { const { args, flags } = await this.parse(ReleaseAssetDownloadCmd); const balena = (0, lazy_1.getBalenaSdk)(); const { getRelease } = await Promise.resolve().then(() => require('../../utils/sdk')); const release = await getRelease(args.commitOrId, { $select: ['id'], }); const releaseAsset = await balena.models.release.asset.get({ release: release.id, asset_key: flags.key, }, { $select: ['id', 'asset'], }); if (releaseAsset.asset == null) { throw new Error(`Release asset ${releaseAsset.id} is empty`); } const stream = await balena.models.release.asset.download({ release: release.id, asset_key: flags.key, }); let outputPath; if (flags.output) { outputPath = path.resolve(flags.output); } else { outputPath = path.resolve(flags.key); } if (fs.existsSync(outputPath) && !flags.overwrite) { const patterns = await Promise.resolve().then(() => require('../../utils/patterns')); await patterns.confirm(false, `File ${outputPath} already exists. Do you want to overwrite it?`); } const dir = path.dirname(outputPath); await fs.promises.mkdir(dir, { recursive: true }); const visuals = (0, lazy_1.getVisuals)(); const bar = new visuals.Progress(`Downloading release asset '${flags.key}'`); const totalSize = releaseAsset.asset.size; let downloadedBytes = 0; const progressStream = new stream_1.Transform({ transform(chunk, _encoding, callback) { downloadedBytes += chunk.length; if (totalSize != null && totalSize > 0) { const percentage = (downloadedBytes / totalSize) * 100; bar.update({ percentage, eta: null }); } callback(null, chunk); }, }); const writeStream = fs.createWriteStream(outputPath); await (0, promises_1.pipeline)(stream, progressStream, writeStream); console.log(`Release asset '${flags.key}' downloaded successfully to ${outputPath}`); } } ReleaseAssetDownloadCmd.description = (0, lazy_1.stripIndent) ` Download a release asset. Download a release asset with the specified key. By default, the file will be saved with the original filename. Use the --output flag to specify a different output path. If the output file already exists, the command will prompt for confirmation before overwriting, unless the --overwrite flag is specified. `; ReleaseAssetDownloadCmd.examples = [ '$ balena release-asset download 1234567 --key config.json', '$ balena release-asset download a777f7345fe3d655c1c981aa642e5555 --key app.tar.gz --output ./downloads/app.tar.gz', '$ balena release-asset download 1234567 --key large-file.bin -o output.bin', '$ balena release-asset download 1234567 --key config.json --overwrite', ]; ReleaseAssetDownloadCmd.args = { commitOrId: (0, release_1.commitOrIdArg)({ description: 'the commit or ID of the release', required: true, }), }; ReleaseAssetDownloadCmd.flags = { key: core_1.Flags.string({ description: 'the key of the release asset to download', required: true, }), output: core_1.Flags.string({ char: 'o', description: 'output path for the downloaded file', }), overwrite: core_1.Flags.boolean({ description: 'overwrite existing file without confirmation', default: false, }), }; ReleaseAssetDownloadCmd.authenticated = true; exports.default = ReleaseAssetDownloadCmd; //# sourceMappingURL=download.js.map