All files / src/commands/k8s k8s.activity.command.js

49.05% Statements 26/53
100% Branches 1/1
0% Functions 0/2
49.05% Lines 26/53

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 54193x 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          
import { z } from 'zod';
import { defineCommand } from '../../lib/define-command.js';
import { defineOption } from '../../lib/define-option.js';
import { k8sListActivity } from '../../lib/k8s.js';
import { Logger } from '../../logger.js';
import { humanJsonOutputFormatOption, orgaIdOrNameOption } from '../global.options.js';
import { k8sIdOrNameArg } from './k8s.args.js';
 
export const k8sActivityCommand = defineCommand({
  description: 'Show recent deployment events of a Kubernetes cluster',
  since: '4.9.0',
  options: {
    org: orgaIdOrNameOption,
    format: humanJsonOutputFormatOption,
    limit: defineOption({
      name: 'limit',
      schema: z.coerce.number().int().min(1).max(1000).default(50),
      description: 'Number of events to fetch (1 to 1000)',
      placeholder: 'limit',
    }),
  },
  args: [k8sIdOrNameArg],
  async handler(options, clusterIdOrName) {
    const { format, org: orgIdOrName, limit } = options;
    const events = await k8sListActivity(orgIdOrName, clusterIdOrName, limit);

    switch (format) {
      case 'json':
        Logger.printJson(events);
        break;
      case 'human':
      default:
        if (events.length === 0) {
          Logger.println('🔎 No deployment event found');
          return;
        }
        console.table(
          events.map((e) => ({
            Date: formatDate(e.createdAt),
            Operation: e.operation,
            Step: e.stepName ?? '-',
            Status: e.status,
          })),
        );
        break;
    }
  },
});
 
function formatDate(iso) {
  if (iso == null) return '-';
  return `${iso.slice(0, 19).replace('T', ' ')}Z`;
}