|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2 |
| 4 | + * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +import {Args, Flags} from '@oclif/core'; |
| 7 | +import {InstanceCommand} from '@salesforce/b2c-tooling-sdk/cli'; |
| 8 | +import { |
| 9 | + searchJobExecutions, |
| 10 | + getJobExecution, |
| 11 | + getJobLog, |
| 12 | + type JobExecution, |
| 13 | +} from '@salesforce/b2c-tooling-sdk/operations/jobs'; |
| 14 | +import {t, withDocs} from '../../i18n/index.js'; |
| 15 | +import {highlightLogText} from '../../utils/logs/index.js'; |
| 16 | + |
| 17 | +interface JobLogResult { |
| 18 | + execution: JobExecution; |
| 19 | + log: string; |
| 20 | +} |
| 21 | + |
| 22 | +export default class JobLog extends InstanceCommand<typeof JobLog> { |
| 23 | + static args = { |
| 24 | + jobId: Args.string({ |
| 25 | + description: 'Job ID', |
| 26 | + required: true, |
| 27 | + }), |
| 28 | + executionId: Args.string({ |
| 29 | + description: 'Execution ID (if omitted, finds the most recent execution with a log)', |
| 30 | + required: false, |
| 31 | + }), |
| 32 | + }; |
| 33 | + |
| 34 | + static description = withDocs( |
| 35 | + t('commands.job.log.description', 'Retrieve the log for a job execution'), |
| 36 | + '/cli/jobs.html#b2c-job-log', |
| 37 | + ); |
| 38 | + |
| 39 | + static enableJsonFlag = true; |
| 40 | + |
| 41 | + static examples = [ |
| 42 | + '<%= config.bin %> <%= command.id %> my-job', |
| 43 | + '<%= config.bin %> <%= command.id %> my-job --failed', |
| 44 | + '<%= config.bin %> <%= command.id %> my-job abc123-def456', |
| 45 | + '<%= config.bin %> <%= command.id %> my-job --json', |
| 46 | + ]; |
| 47 | + |
| 48 | + static flags = { |
| 49 | + ...InstanceCommand.baseFlags, |
| 50 | + failed: Flags.boolean({ |
| 51 | + description: 'Find the most recent failed execution with a log', |
| 52 | + default: false, |
| 53 | + }), |
| 54 | + 'no-color': Flags.boolean({ |
| 55 | + description: 'Disable colored output', |
| 56 | + default: false, |
| 57 | + }), |
| 58 | + }; |
| 59 | + |
| 60 | + protected operations = { |
| 61 | + searchJobExecutions, |
| 62 | + getJobExecution, |
| 63 | + getJobLog, |
| 64 | + }; |
| 65 | + |
| 66 | + async run(): Promise<JobLogResult> { |
| 67 | + this.requireOAuthCredentials(); |
| 68 | + |
| 69 | + const {jobId, executionId} = this.args; |
| 70 | + const {failed} = this.flags; |
| 71 | + |
| 72 | + let execution: JobExecution; |
| 73 | + |
| 74 | + if (executionId) { |
| 75 | + this.log( |
| 76 | + t('commands.job.log.fetchingSpecific', 'Fetching log for job {{jobId}} execution {{executionId}}...', { |
| 77 | + jobId, |
| 78 | + executionId, |
| 79 | + }), |
| 80 | + ); |
| 81 | + execution = await this.operations.getJobExecution(this.instance, jobId, executionId); |
| 82 | + } else { |
| 83 | + this.log( |
| 84 | + failed |
| 85 | + ? t( |
| 86 | + 'commands.job.log.searchingFailed', |
| 87 | + 'Searching for most recent failed execution with log for job {{jobId}}...', |
| 88 | + {jobId}, |
| 89 | + ) |
| 90 | + : t('commands.job.log.searching', 'Searching for most recent execution with log for job {{jobId}}...', { |
| 91 | + jobId, |
| 92 | + }), |
| 93 | + ); |
| 94 | + |
| 95 | + const results = await this.operations.searchJobExecutions(this.instance, { |
| 96 | + jobId, |
| 97 | + status: failed ? ['ERROR'] : undefined, |
| 98 | + count: 10, |
| 99 | + sortBy: 'start_time', |
| 100 | + sortOrder: 'desc', |
| 101 | + }); |
| 102 | + |
| 103 | + const match = results.hits.find((hit) => hit.is_log_file_existing); |
| 104 | + if (!match) { |
| 105 | + const msg = failed |
| 106 | + ? t( |
| 107 | + 'commands.job.log.noFailedExecutionFound', |
| 108 | + 'No failed execution with a log file found for job {{jobId}}', |
| 109 | + {jobId}, |
| 110 | + ) |
| 111 | + : t('commands.job.log.noExecutionFound', 'No execution with a log file found for job {{jobId}}', { |
| 112 | + jobId, |
| 113 | + }); |
| 114 | + this.error(msg); |
| 115 | + } |
| 116 | + |
| 117 | + execution = match; |
| 118 | + } |
| 119 | + |
| 120 | + if (!execution.is_log_file_existing) { |
| 121 | + this.error(t('commands.job.log.noLogFile', 'No log file exists for this execution')); |
| 122 | + } |
| 123 | + |
| 124 | + this.log( |
| 125 | + t('commands.job.log.foundExecution', 'Found execution {{executionId}} ({{status}})', { |
| 126 | + executionId: execution.id ?? 'unknown', |
| 127 | + status: execution.exit_status?.code || execution.execution_status || 'unknown', |
| 128 | + }), |
| 129 | + ); |
| 130 | + |
| 131 | + const log = await this.operations.getJobLog(this.instance, execution); |
| 132 | + |
| 133 | + if (!this.jsonEnabled()) { |
| 134 | + const useColor = !this.flags['no-color'] && process.stdout.isTTY; |
| 135 | + process.stdout.write(useColor ? highlightLogText(log) : log); |
| 136 | + } |
| 137 | + |
| 138 | + return {execution, log}; |
| 139 | + } |
| 140 | +} |
0 commit comments