From 438023017f84ecde3134172e34aaeeac0886be27 Mon Sep 17 00:00:00 2001 From: AnishKacham Date: Wed, 9 Aug 2023 23:21:59 +0530 Subject: [PATCH] feat: add flag to set context when it is created --- 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)