-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: inspect command to show number of components,servers and channels
- Loading branch information
Showing
6 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 description = 'Show the number of servers, channels, and components in AsyncAPI files'; | ||
|
||
static flags = { | ||
...inspectFlags(), | ||
...proxyFlags(), // Merge proxyFlags with validateFlags | ||
}; | ||
|
||
static 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}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |