From dcb91706fa53c6348a84fd435cf0e8a8da186e46 Mon Sep 17 00:00:00 2001 From: Anish <79566582+AnishKacham@users.noreply.github.com> Date: Thu, 17 Aug 2023 16:47:04 +0530 Subject: [PATCH] feat: add flag to set context when it is created (#752) --- src/commands/config/context/add.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/commands/config/context/add.ts b/src/commands/config/context/add.ts index 26ea6635c60..1ec57606d69 100644 --- a/src/commands/config/context/add.ts +++ b/src/commands/config/context/add.ts @@ -1,6 +1,6 @@ import { Flags } from '@oclif/core'; import Command from '../../../base'; -import { addContext } from '../../../models/Context'; +import { addContext, setCurrentContext } from '../../../models/Context'; import { MissingContextFileError, ContextFileWrongFormatError, @@ -10,6 +10,12 @@ export default class ContextAdd extends Command { static description = 'Add a context to the store'; static flags = { help: Flags.help({ char: 'h' }), + 'set-current': Flags.boolean({ + char: 's', + description: 'Set context being added as the current context', + default: false, + required: false, + }) }; static args = [ @@ -22,15 +28,23 @@ export default class ContextAdd extends Command { ]; async run() { - const { args } = await this.parse(ContextAdd); + const { args, flags } = await this.parse(ContextAdd); const contextName = args['context-name']; const specFilePath = args['spec-file-path']; + const setAsCurrent = flags['set-current']; try { await addContext(contextName, specFilePath); this.log( `Added context "${contextName}".\n\nYou can set it as your current context: asyncapi config context use ${contextName}\nYou can use this context when needed by passing ${contextName} as a parameter: asyncapi validate ${contextName}` ); + + if (setAsCurrent) { + await setCurrentContext(contextName); + this.log( + `The newly added context "${contextName}", is set as your current context!` + ); + } } catch (e) { if ( e instanceof (MissingContextFileError || ContextFileWrongFormatError)