diff --git a/package.json b/package.json index 7f7a5c8..0adbf88 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "substreams-sink", - "version": "0.15.0", + "version": "0.15.1", "description": "Substreams Sink", "type": "module", "exports": "./dist/index.js", diff --git a/src/commander.ts b/src/commander.ts index 256b216..1af5a1a 100644 --- a/src/commander.ts +++ b/src/commander.ts @@ -2,9 +2,6 @@ import "dotenv/config"; import { Command, Option } from "commander"; import { DEFAULT_INACTIVITY_SECONDS, DEFAULT_PARAMS, DEFAULT_VERBOSE, DEFAULT_HOSTNAME, DEFAULT_PORT, DEFAULT_METRICS_LABELS, DEFAULT_COLLECT_DEFAULT_METRICS, DEFAULT_START_BLOCK, DEFAULT_DELAY_BEFORE_START, DEFAULT_HEADERS, DEFAULT_PRODUCTION_MODE, DEFAULT_FINAL_BLOCKS_ONLY } from "./config.js"; -import { list } from "./list.js"; -import { logger } from "./logger.js"; - export interface Package { name: string; version: string; @@ -38,17 +35,7 @@ export function program(pkg: Package) { program.name(pkg.name).version(pkg.version, "-v, --version", `version for ${pkg.name}`); program.command("completion").description("Generate the autocompletion script for the specified shell"); program.command("help").description("Display help for command"); - program.command("list") - .showHelpAfterError() - .description("List all compatible output modules for a given Substreams package") - .argument("", "URL or IPFS hash of Substreams package") - .action(async spkg => { - const modules = await list(spkg) - logger.info('list', { modules }) - process.stdout.write(JSON.stringify(modules) + "\n") - }); program.showHelpAfterError(); - return program; } @@ -73,8 +60,30 @@ function handleHeaders(value: string, previous: Headers) { return headers; } -export function run(program: Command, pkg: Package) { - return program.command("run") +export function run(program: Command, pkg: Package, options: AddRunOptions = {}) { + return addRunOptions(program.command("run"), pkg, options); +} + +export function list(program: Command) { + program.command("list") + .showHelpAfterError() + .description("List all compatible output modules for a given Substreams package") + .argument("", "URL or IPFS hash of Substreams package") + .action(async spkg => { + const modules = await list(spkg) + process.stdout.write(JSON.stringify(modules) + "\n"); + }); + + return program; +} + +interface AddRunOptions { + http?: boolean; + metrics?: boolean; +} + +export function addRunOptions(program: Command, pkg: Package, options: AddRunOptions = {}) { + const command = program .showHelpAfterError() .description(pkg.description) .addOption(new Option("-e --substreams-endpoint ", "Substreams gRPC endpoint to stream data from").makeOptionMandatory().env("SUBSTREAMS_ENDPOINT")) @@ -89,11 +98,20 @@ export function run(program: Command, pkg: Package) { .addOption(new Option("--cursor ", "Cursor to stream from. Leave blank for no cursor")) .addOption(new Option("--production-mode ", "Enable production mode, allows cached Substreams data if available").default(DEFAULT_PRODUCTION_MODE).env("PRODUCTION_MODE")) .addOption(new Option("--inactivity-seconds ", "If set, the sink will stop when inactive for over a certain amount of seconds").default(DEFAULT_INACTIVITY_SECONDS).env("INACTIVITY_SECONDS")) - .addOption(new Option("--hostname ", "The process will listen on this hostname for any HTTP and Prometheus metrics requests").default(DEFAULT_HOSTNAME).env("HOSTNAME")) - .addOption(new Option("--port ", "The process will listen on this port for any HTTP and Prometheus metrics requests").default(DEFAULT_PORT).env("PORT")) - .addOption(new Option("--metrics-labels [string...]", "To apply generic labels to all default metrics (ex: --labels foo=bar)").default(DEFAULT_METRICS_LABELS).env("METRICS_LABELS").argParser(handleMetricsLabels)) - .addOption(new Option("--collect-default-metrics ", "Collect default metrics").default(DEFAULT_COLLECT_DEFAULT_METRICS).env("COLLECT_DEFAULT_METRICS")) .addOption(new Option("--headers [string...]", "Set headers that will be sent on every requests (ex: --headers X-HEADER=headerA)").default(DEFAULT_HEADERS).env("HEADERS").argParser(handleHeaders)) .addOption(new Option("--final-blocks-only ", "Only process blocks that have pass finality, to prevent any reorg and undo signal by staying further away from the chain HEAD").default(DEFAULT_FINAL_BLOCKS_ONLY).env("FINAL_BLOCKS_ONLY")) - .addOption(new Option("--verbose ", "Enable verbose logging").default(DEFAULT_VERBOSE).env("VERBOSE")); + .addOption(new Option("--verbose ", "Enable verbose logging").default(DEFAULT_VERBOSE).env("VERBOSE")) + + // HTTP and Prometheus metrics options + if ( options.http !== false ) { + command + .addOption(new Option("--hostname ", "The process will listen on this hostname for any HTTP and Prometheus metrics requests").default(DEFAULT_HOSTNAME).env("HOSTNAME")) + .addOption(new Option("--port ", "The process will listen on this port for any HTTP and Prometheus metrics requests").default(DEFAULT_PORT).env("PORT")) + } + if ( options.metrics !== false ) { + command + .addOption(new Option("--metrics-labels [string...]", "To apply generic labels to all default metrics (ex: --labels foo=bar)").default(DEFAULT_METRICS_LABELS).env("METRICS_LABELS").argParser(handleMetricsLabels)) + .addOption(new Option("--collect-default-metrics ", "Collect default metrics").default(DEFAULT_COLLECT_DEFAULT_METRICS).env("COLLECT_DEFAULT_METRICS")) + } + return command; }