|
1 | 1 | import { ApplicationLogStream } from '@clevercloud/client/esm/streams/application-logs.js'; |
| 2 | +import { ResourceLogStream } from '@clevercloud/client/esm/streams/resource-logs.js'; |
2 | 3 | import { styleText } from '../lib/style-text.js'; |
3 | 4 | import { Logger } from '../logger.js'; |
4 | 5 | import { conf } from './configuration.js'; |
@@ -91,6 +92,77 @@ export async function displayLogs(params) { |
91 | 92 | return logStream; |
92 | 93 | } |
93 | 94 |
|
| 95 | +export async function displayAddonLogs(params) { |
| 96 | + const deferred = params.deferred || new Deferred(); |
| 97 | + const { apiHost, tokens } = await getHostAndTokens(); |
| 98 | + const { ownerId, addonId, filter, since, until, deploymentId, format } = params; |
| 99 | + |
| 100 | + if (format === 'json' && until == null) { |
| 101 | + throw new Error('"json" format is only applicable with a limiting parameter such as `--until`'); |
| 102 | + } |
| 103 | + |
| 104 | + const logStream = new ResourceLogStream({ |
| 105 | + apiHost, |
| 106 | + tokens, |
| 107 | + ownerId, |
| 108 | + addonId, |
| 109 | + connectionTimeout: 10_000, |
| 110 | + retryConfiguration, |
| 111 | + since, |
| 112 | + until, |
| 113 | + deploymentId, |
| 114 | + filter, |
| 115 | + service: 'all', |
| 116 | + throttleElements: THROTTLE_ELEMENTS, |
| 117 | + throttlePerInMilliseconds: THROTTLE_PER_IN_MILLISECONDS, |
| 118 | + }); |
| 119 | + |
| 120 | + // Properly close the stream |
| 121 | + process.once('SIGINT', (signal) => logStream.close(signal)); |
| 122 | + const jsonArray = new JsonArray(); |
| 123 | + |
| 124 | + logStream |
| 125 | + .on('open', () => { |
| 126 | + Logger.debug(styleText('blue', `Logs stream (open) ${JSON.stringify({ addonId, filter, deploymentId })}`)); |
| 127 | + if (format === 'json') { |
| 128 | + jsonArray.open(); |
| 129 | + } |
| 130 | + }) |
| 131 | + .on('error', (event) => { |
| 132 | + Logger.debug(styleText('red', `Logs stream (error) ${event.error.message}`)); |
| 133 | + }) |
| 134 | + .onLog((log) => { |
| 135 | + switch (format) { |
| 136 | + case 'json': |
| 137 | + jsonArray.push(log); |
| 138 | + return; |
| 139 | + case 'json-stream': |
| 140 | + Logger.printJson(log); |
| 141 | + return; |
| 142 | + case 'human': |
| 143 | + default: |
| 144 | + if (log.message === RESET_COLOR) { |
| 145 | + return; |
| 146 | + } |
| 147 | + Logger.println(formatLogLine(log)); |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | + // start() is blocking until end of stream |
| 152 | + logStream |
| 153 | + .start() |
| 154 | + .then(() => { |
| 155 | + if (format === 'json') { |
| 156 | + jsonArray.close(); |
| 157 | + } |
| 158 | + return deferred.resolve(); |
| 159 | + }) |
| 160 | + .catch(processError) |
| 161 | + .catch((error) => deferred.reject(error)); |
| 162 | + |
| 163 | + return logStream; |
| 164 | +} |
| 165 | + |
94 | 166 | export async function watchDeploymentAndDisplayLogs(options) { |
95 | 167 | const { ownerId, appId, deploymentId, commitId, knownDeployments, quiet, redeployDate, exitStrategy } = options; |
96 | 168 |
|
|
0 commit comments