Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add flag to set context when it is created #752

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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