-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b562ecb
commit 61f6c1b
Showing
7 changed files
with
71 additions
and
14 deletions.
There are no files selected for viewing
40 changes: 37 additions & 3 deletions
40
cortex-js/src/infrastructure/commanders/engines.command.ts
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 |
---|---|---|
@@ -1,22 +1,56 @@ | ||
import { invert } from 'lodash'; | ||
import { CommandRunner, SubCommand } from 'nest-commander'; | ||
import { SetCommandContext } from './decorators/CommandContext'; | ||
import { ContextService } from '@/infrastructure/services/context/context.service'; | ||
import { EnginesListCommand } from './engines/engines-list.command'; | ||
import { EnginesGetCommand } from './engines/engines-get.command'; | ||
import { EnginesInitCommand } from './engines/engines-init.command'; | ||
import { ModuleRef } from '@nestjs/core'; | ||
import { EngineNamesMap } from './types/engine.interface'; | ||
|
||
@SubCommand({ | ||
name: 'engines', | ||
subCommands: [EnginesListCommand, EnginesGetCommand, EnginesInitCommand], | ||
description: 'Get cortex engines', | ||
arguments: '<command|parameter> [subcommand]', | ||
}) | ||
@SetCommandContext() | ||
export class EnginesCommand extends CommandRunner { | ||
constructor(readonly contextService: ContextService) { | ||
commandMap: { [key: string]: any } = { | ||
list: EnginesListCommand, | ||
get: EnginesGetCommand, | ||
init: EnginesInitCommand, | ||
}; | ||
|
||
constructor( | ||
readonly contextService: ContextService, | ||
private readonly moduleRef: ModuleRef, | ||
) { | ||
super(); | ||
} | ||
async run(passedParam: string[]): Promise<void> { | ||
const [parameter, command] = passedParam; | ||
if (command !== 'list' && !parameter) { | ||
console.error('Engine name is required.'); | ||
return; | ||
} | ||
|
||
// Handle the commands accordingly | ||
const commandClass = this.commandMap[command as string]; | ||
if (!commandClass) { | ||
this.command?.help(); | ||
return; | ||
} | ||
const engine = invert(EngineNamesMap)[parameter] || parameter; | ||
await this.runCommand(commandClass, [engine]); | ||
} | ||
|
||
async run(): Promise<void> { | ||
this.command?.help(); | ||
private async runCommand(commandClass: any, params: string[] = []) { | ||
const commandInstance = this.moduleRef.get(commandClass, { strict: false }); | ||
if (commandInstance) { | ||
await commandInstance.run(params); | ||
} else { | ||
console.error('Command not found.'); | ||
} | ||
} | ||
} |
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
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
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
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
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
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