Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: inspect command to show number of components,servers and channels #1628

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions src/commands/inspect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Args } from '@oclif/core';
import Command from '../core/base';
import { parse } from '../core/parser';
import { load } from '../core/models/SpecificationFile';
import { inspectFlags } from '../core/flags/inspect.flags';
import { proxyFlags } from '../core/flags/proxy.flags';
import { numberOfChannels } from '../core/utils/numberOfChannels';
import { numberOfServers } from '../core/utils/numberOfServers';
import { ValidationError } from '../core/errors/validation-error';
import { numberOfComponents } from '../core/utils/numberOfComponents';

export default class Inspect extends Command {
static readonly description = 'Show the number of servers, channels, and components in AsyncAPI files';

static readonly flags = {
...inspectFlags(),
...proxyFlags(), // Merge proxyFlags with validateFlags
};

static readonly args = {
'spec-file': Args.string({ description: 'spec path, url, or context-name', required: false }),
proxyHost: Args.string({ description: 'Name of the Proxy Host', required: false }),
proxyPort: Args.string({ description: 'Name of the Port of the ProxyHost', required: false }),
};

async run() {
const { args, flags } = await this.parse(Inspect);
let filePath = args['spec-file'];
const proxyHost = flags['proxyHost'];
const proxyPort = flags['proxyPort'];
if (proxyHost && proxyPort) {
const proxyUrl = `http://${proxyHost}:${proxyPort}`;
filePath = `${filePath}+${proxyUrl}`; // Update filePath with proxyUrl
}
try {
this.specFile = await load(filePath);
} catch (err: any) {
if (err.message.includes('Failed to download')) {
throw new Error('Proxy Connection Error: Unable to establish a connection to the proxy check hostName or PortNumber.');
} else {
this.error(
new ValidationError({
type: 'invalid-file',
filepath: filePath,
})
);
}
}
let { document } = await parse(this, this.specFile);
const channels = await numberOfChannels(document);
const servers = await numberOfServers(document);
const components = await numberOfComponents(document);
this.log(`The total number of Servers in asyncapi document is ${servers}`);
this.log(`The total number of Channels in asyncapi document is ${channels}`);
this.log(`The total number of Components in asyncapi document is ${components}`);
}
}
7 changes: 7 additions & 0 deletions src/core/flags/inspect.flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Flags } from "@oclif/core";

export const inspectFlags = () => {
return {
help: Flags.help({ char: 'h' }),
};
};
9 changes: 9 additions & 0 deletions src/core/utils/numberOfChannels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AsyncAPIDocumentInterface } from '@asyncapi/parser/cjs/models';

export async function numberOfChannels(document: AsyncAPIDocumentInterface | undefined) {
let countChannels = 0;
if (document?.channels().length) {
countChannels = document?.channels().length;
}
return countChannels;
}
9 changes: 9 additions & 0 deletions src/core/utils/numberOfComponents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AsyncAPIDocumentInterface } from '@asyncapi/parser/cjs/models';

export async function numberOfComponents(document: AsyncAPIDocumentInterface | undefined) {
let countComponents = 0;
if (document?.components().json()) {
countComponents = document?.components().json.length;
}
return countComponents;
}
9 changes: 9 additions & 0 deletions src/core/utils/numberOfServers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AsyncAPIDocumentInterface } from '@asyncapi/parser/cjs/models';

export async function numberOfServers(document: AsyncAPIDocumentInterface | undefined) {
let countServers = 0;
if (document?.servers().length) {
countServers = document?.servers().length;
}
return countServers;
}
Loading