UNPKG

balena-cli

Version:

The official balena Command Line Interface

131 lines (125 loc) 5.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const lazy_1 = require("../../utils/lazy"); const MAX_RETRY = 1000; class DeviceLogsCmd extends core_1.Command { async run() { var _a; const { args: params, flags: options } = await this.parse(DeviceLogsCmd); const balena = (0, lazy_1.getBalenaSdk)(); const { serviceIdToName } = await Promise.resolve().then(() => require('../../utils/cloud')); const { connectAndDisplayDeviceLogs, displayLogObject } = await Promise.resolve().then(() => require('../../utils/device/logs')); const { validateIPAddress, validateDotLocalUrl } = await Promise.resolve().then(() => require('../../utils/validation')); const Logger = await Promise.resolve().then(() => require('../../utils/logger')); const logger = Logger.getLogger(); const displayCloudLog = async (line) => { var _a; if (!line.isSystem) { const serviceName = (_a = (await serviceIdToName(balena, line.serviceId))) !== null && _a !== void 0 ? _a : 'Unknown service'; displayLogObject({ serviceName, ...line }, logger, options.system || false, options.service); } else { displayLogObject(line, logger, options.system || false, options.service); } }; if (validateIPAddress(params.device) || validateDotLocalUrl(params.device)) { const { DeviceAPI } = await Promise.resolve().then(() => require('../../utils/device/api')); const deviceApi = new DeviceAPI(logger, params.device); logger.logDebug('Checking we can access device'); try { await deviceApi.ping(); } catch (e) { const { ExpectedError } = await Promise.resolve().then(() => require('../../errors')); throw new ExpectedError(`Cannot access device at address ${params.device}. Device may not be in local mode.`); } logger.logDebug('Streaming logs'); await connectAndDisplayDeviceLogs({ deviceApi, logger, system: options.system || false, filterServices: options.service, maxAttempts: 1 + ((_a = options['max-retry']) !== null && _a !== void 0 ? _a : MAX_RETRY), }); } else { const { checkLoggedIn } = await Promise.resolve().then(() => require('../../utils/patterns')); await checkLoggedIn(); if (options.tail) { const logStream = await balena.logs.subscribe(params.device, { count: 100, }); await new Promise((_resolve, reject) => { logStream.on('line', displayCloudLog); logStream.on('error', reject); }); } else { const logMessages = await balena.logs.history(params.device); for (const logMessage of logMessages) { await displayCloudLog(logMessage); } } } } } DeviceLogsCmd.aliases = ['logs']; DeviceLogsCmd.deprecateAliases = true; DeviceLogsCmd.description = (0, lazy_1.stripIndent) ` Show device logs. Show logs for a specific device. By default, the command prints all log messages and exits. To continuously stream output, and see new logs in real time, use the \`--tail\` option. If an IP or .local address is passed to this command, logs are displayed from a local mode device with that address. Note that --tail is implied when this command is provided a local mode device. Logs from a single service can be displayed with the --service flag. Just system logs can be shown with the --system flag. Note that these flags can be used together. Note: --service and --system flags must come after the device parameter, as per examples. `; DeviceLogsCmd.examples = [ '$ balena device logs 23c73a1', '$ balena device logs 23c73a1 --tail', '', '$ balena device logs 192.168.0.31', '$ balena device logs 192.168.0.31 --service my-service', '$ balena device logs 192.168.0.31 --service my-service-1 --service my-service-2', '', '$ balena device logs 23c73a1.local --system', '$ balena device logs 23c73a1.local --system --service my-service', ]; DeviceLogsCmd.args = { device: core_1.Args.string({ description: 'device UUID, IP, or .local address', required: true, }), }; DeviceLogsCmd.flags = { 'max-retry': core_1.Flags.integer({ description: (0, lazy_1.stripIndent) ` Maximum number of reconnection attempts on "connection lost" errors (use 0 to disable auto reconnection).`, }), tail: core_1.Flags.boolean({ default: false, description: 'continuously stream output', char: 't', }), service: core_1.Flags.string({ description: (0, lazy_1.stripIndent) ` Reject logs not originating from this service. This can be used in combination with --system or other --service flags.`, char: 's', multiple: true, }), system: core_1.Flags.boolean({ default: false, description: 'Only show system logs. This can be used in combination with --service.', char: 'S', }), }; DeviceLogsCmd.primary = true; exports.default = DeviceLogsCmd; //# sourceMappingURL=logs.js.map