diff --git a/docs/services-logs-drains.md b/docs/services-logs-drains.md index 6f9aaa32..f4484804 100644 --- a/docs/services-logs-drains.md +++ b/docs/services-logs-drains.md @@ -5,6 +5,7 @@ You can use Clever Tools to control logs drains, through following commands. Eac ``` clever drain clever drain -F json +clever drain --type clever drain create clever drain get clever drain get --format json diff --git a/skills/clever-tools/references/full-documentation.md b/skills/clever-tools/references/full-documentation.md index 7142a7f0..95ebbc37 100644 --- a/skills/clever-tools/references/full-documentation.md +++ b/skills/clever-tools/references/full-documentation.md @@ -1054,6 +1054,7 @@ clever drain [options] -a, --alias Short name for the application --app Application to manage by its ID (or name, if unambiguous) -F, --format Output format (human, json) (default: human) + --type Only list drains of this type (betterstack, datadog, elasticsearch, newrelic, ovh-tcp, raw-http, splunk, syslog-tcp, syslog-udp) ``` ### drain check diff --git a/src/commands/drain/drain.command.js b/src/commands/drain/drain.command.js index 029d2b4c..9743d76a 100644 --- a/src/commands/drain/drain.command.js +++ b/src/commands/drain/drain.command.js @@ -1,7 +1,9 @@ +import { z } from 'zod'; import { getDrains } from '../../clever-client/drains.js'; import { defineCommand } from '../../lib/define-command.js'; +import { defineOption } from '../../lib/define-option.js'; import { Logger } from '../../logger.js'; -import { formatDrain, resolveDrainResource } from '../../models/drain.js'; +import { DRAIN_TYPE_CLI_CODES, DRAIN_TYPES, formatDrain, resolveDrainResource } from '../../models/drain.js'; import { sendToApi } from '../../models/send-to-api.js'; import { addonIdOrRealIdOption, @@ -17,14 +19,25 @@ export const drainCommand = defineCommand({ alias: aliasOption, appIdOrName: appIdOrNameOption, addonIdOrRealId: addonIdOrRealIdOption, + type: defineOption({ + name: 'type', + schema: z.enum(DRAIN_TYPE_CLI_CODES).optional(), + description: 'Only list drains of this type', + placeholder: 'drain-type', + }), format: humanJsonOutputFormatOption, }, args: [], async handler(options) { - const { alias, appIdOrName, addonIdOrRealId, format } = options; + const { alias, appIdOrName, addonIdOrRealId, type, format } = options; const { ownerId, resourceId } = await resolveDrainResource(alias, appIdOrName, addonIdOrRealId); - const drains = await getDrains({ ownerId, resourceId }).then(sendToApi); + const allDrains = await getDrains({ ownerId, resourceId }).then(sendToApi); + + // The resource scoped endpoint only filters on status, so the type filter is applied here. + // The list is bounded by the number of drains of a single resource, it never paginates. + const drainType = Object.values(DRAIN_TYPES).find((drainType) => drainType.cliCode === type); + const drains = drainType == null ? allDrains : allDrains.filter((d) => d.recipient.type === drainType.apiCode); switch (format) { case 'json': { @@ -35,7 +48,8 @@ export const drainCommand = defineCommand({ default: { if (drains.length === 0) { const resourceLabel = addonIdOrRealId ?? appIdOrName ?? resourceId; - Logger.println(`There are no drains for ${resourceLabel}`); + const drainsLabel = drainType == null ? 'drains' : `${drainType.label} drains`; + Logger.println(`There are no ${drainsLabel} for ${resourceLabel}`); return; } diff --git a/src/commands/drain/drain.docs.md b/src/commands/drain/drain.docs.md index 70c6e86b..1dc56929 100644 --- a/src/commands/drain/drain.docs.md +++ b/src/commands/drain/drain.docs.md @@ -16,6 +16,7 @@ clever drain [options] |`-a`, `--alias` ``|Short name for the application| |`--app` ``|Application to manage by its ID (or name, if unambiguous)| |`-F`, `--format` ``|Output format (human, json) (default: human)| +|`--type` ``|Only list drains of this type (betterstack, datadog, elasticsearch, newrelic, ovh-tcp, raw-http, splunk, syslog-tcp, syslog-udp)| ## ➡️ `clever drain check` Since 4.11.0