Skip to content

Commit

Permalink
feat: support context file location in repository
Browse files Browse the repository at this point in the history
  • Loading branch information
aeworxet committed Jul 4, 2023
1 parent 93dd030 commit 8368907
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
37 changes: 32 additions & 5 deletions src/commands/config/context/current.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
import { Flags } from '@oclif/core';
import Command from '../../../base';
import { getCurrentContext } from '../../../models/Context';
import {
MissingContextFileError,
ContextFileWrongFormatError,
ContextNotFoundError,
} from '../../../errors/context-error';

export default class ContextCurrent extends Command {
static description='Shows the current context that is being used';
static flags={
help: Flags.help({char: 'h'})
static description = 'Shows the current context that is being used';
static flags = {
help: Flags.help({ char: 'h' }),
};

async run() {
const { current, context } = await getCurrentContext();
this.log(`${current}: ${context}`);
let fileContent;

try {
fileContent = await getCurrentContext();
} catch (e) {
if (
e instanceof MissingContextFileError ||
ContextFileWrongFormatError ||
ContextNotFoundError ||
(fileContent && !fileContent.current) ||
!fileContent
) {
this.log(
'No context is set as current. Run "asyncapi config context" to see all available options.'
);
return;
} else {
throw e;
}
}

if (fileContent) {
this.log(`${fileContent.current}: ${fileContent.context}`);
}
}
}
25 changes: 22 additions & 3 deletions src/commands/config/context/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Flags } from '@oclif/core';
import Command from '../../../base';
import { loadContextFile, CONTEXT_FILE_PATH } from '../../../models/Context';
import {
MissingContextFileError,
ContextFileWrongFormatError,
} from '../../../errors/context-error';

export default class ContextList extends Command {
static description = 'List all the stored contexts in the store';
Expand All @@ -9,18 +13,33 @@ export default class ContextList extends Command {
};

async run() {
const fileContent = await loadContextFile();
let fileContent;

try {
fileContent = await loadContextFile();
} catch (e) {
if (e instanceof MissingContextFileError || ContextFileWrongFormatError) {
this.log(
'You have no context configured. Run "asyncapi config context" to see all available options.'
);
}
}

// If context file contains only one empty property `store` then the whole
// context file is considered empty.
if (
fileContent &&
Object.keys(fileContent).length === 1 &&
Object.keys(fileContent.store).length === 0
) {
this.log(`Context file "${CONTEXT_FILE_PATH}" is empty.`);
return;
}
for (const [contextName, filePath] of Object.entries(fileContent.store)) {
this.log(`${contextName}: ${filePath}`);

if (fileContent) {
for (const [contextName, filePath] of Object.entries(fileContent.store)) {
this.log(`${contextName}: ${filePath}`);
}
}
}
}

0 comments on commit 8368907

Please sign in to comment.