UNPKG

1.01 kBJavaScriptView Raw
1const { logs } = require('../sdk');
2const { getAPIKey } = require('./userConf');
3
4/**
5 * Retrieves the logs of a previously deployed Binaris function.
6 *
7 * @param {string} functionName - name of functions whose logs will be retrieved
8 * @param {boolean} follow - read the logs in tail -f fashion
9 * @param {moment} since - get log since date
10 * @param {ReadableStream} logStream - output stream to feed log records
11 */
12const logCLI = async function logCLI(funcName, follow, since, logStream) {
13 const apiKey = await getAPIKey();
14 let token;
15 let startAfter = since && since.toISOString();
16 if (!follow && !startAfter) {
17 const baseDate = new Date();
18 baseDate.setTime(0);
19 startAfter = baseDate.toISOString();
20 }
21 do {
22 // eslint-disable-next-line no-await-in-loop
23 const { nextToken, body } = await logs(funcName, apiKey, follow, startAfter, token);
24 for (const record of body) {
25 logStream.push(record);
26 }
27 token = nextToken;
28 } while (token);
29};
30
31module.exports = logCLI;