Skip to content

Commit

Permalink
feat: add flag to set context when it is created
Browse files Browse the repository at this point in the history
  • Loading branch information
AnishKacham committed Aug 9, 2023
1 parent 12361a3 commit cec321a
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/commands/config/context/add.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 = [
Expand All @@ -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)
Expand Down

0 comments on commit cec321a

Please sign in to comment.