All files / src/commands/logs logs.command.js

100% Statements 79/79
88.88% Branches 8/9
100% Functions 1/1
100% Lines 79/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 9x 4x 4x 4x 4x 4x 4x 4x 15x 15x 7x 7x 6x 6x 7x 7x 7x 6x 193x 193x  
import { z } from 'zod';
import { defineCommand } from '../../lib/define-command.js';
import { defineOption } from '../../lib/define-option.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import * as Application from '../../models/application.js';
import { resolveAddon } from '../../models/ids-resolver.js';
import * as Log from '../../models/log.js';
import { Deferred } from '../../models/utils.js';
import {
  addonIdOrRealIdOption,
  afterOption,
  aliasOption,
  appIdOrNameOption,
  beforeOption,
  logsFormatOption,
} from '../global.options.js';
 
export const logsCommand = defineCommand({
  description: 'Fetch application logs, continuously',
  since: '0.2.0',
  options: {
    search: defineOption({
      name: 'search',
      schema: z.string().optional(),
      description: 'Fetch logs matching this pattern',
      placeholder: 'search',
    }),
    deploymentId: defineOption({
      name: 'deployment-id',
      schema: z.string().optional(),
      description: 'Fetch logs for a given deployment',
      placeholder: 'deployment-id',
    }),
    alias: aliasOption,
    app: appIdOrNameOption,
    before: beforeOption,
    after: afterOption,
    addon: addonIdOrRealIdOption,
    format: logsFormatOption,
  },
  args: [],
  async handler(options) {
    const {
      alias,
      app: appIdOrName,
      addon: addonIdOrRealId,
      after: since,
      before: until,
      search,
      deploymentId,
      format,
    } = options;
 
    // ignore --search ""
    const filter = search !== '' ? search : null;
    const isForHuman = format === 'human';
 
    if (addonIdOrRealId != null) {
      const { ownerId, realId } = await resolveAddon(addonIdOrRealId);
      if (isForHuman) {
        Logger.println(styleText('blue', 'Waiting for addon logs…'));
      }
      const deferred = new Deferred();
      await Log.displayLogs({ ownerId, addonId: realId, since, until, filter, format, deferred });
      return deferred.promise;
    }
 
    const { ownerId, appId } = await Application.resolveId(appIdOrName, alias);
 
    if (isForHuman) {
      Logger.println(styleText('blue', 'Waiting for application logs…'));
    }
 
    const deferred = new Deferred();
    await Log.displayLogs({ ownerId, appId, since, until, filter, deploymentId, format, deferred });
    return deferred.promise;
  },
});