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

Validation of default files in CWD does not work when I have no files in the context #35

Closed
derberg opened this issue Jul 26, 2021 · 5 comments · Fixed by #48
Closed
Labels
bug Something isn't working released

Comments

@derberg
Copy link
Member

derberg commented Jul 26, 2021

This is how you can reproduce it:

$ asyncapi context list
myapp1 : /Users/wookiee/sources/cli/test/spec.yaml
$ asyncapi context current
No context is set as current, please set a current context.
$ asyncapi validate
File: /Users/wookiee/sources/cli/asyncapi.yaml successfully validated!
$ asyncapi context remove myapp1
context deleted successfully
$ asyncapi context list
No contexts saved yet.
$ asyncapi validate
No contexts saved yet, run asyncapi --help to know more.

in case I do asyncapi validate and, flags like --context or --files are not passed, and there is no default context + no contexts at all, the validation of default files from CWD should work

@Souvikns
Copy link
Member

Souvikns commented Aug 11, 2021

@derberg shall I start with this issue next?

@derberg
Copy link
Member Author

derberg commented Aug 11, 2021

yeap

@Souvikns
Copy link
Member

Souvikns commented Aug 11, 2021

Error

When the global context file is absent, so no default context is present, and also when flags are missing but there is an asyncapi.yml file in the working directory, It should load that file automatically, but it is throwing No contexts saved yet, run asyncapi --help to know more. error.

Cause of Error

This is happening because of

const ctx: Context = contextService.loadContextFile();
loading the context file before checking for auto-detection. It is needed because to read the --context flag we need the global context config.

Two Possible Solution

  1. If we give auto-detection first priority then in this scenario the CLI will auto-detect files first and not load the global config. But doing this will change the default behavior as it will not look for global current context first, but only if there is no asyncapi.yml file in the working directory. This could be beneficial in some scenarios.

  2. After we catch the ContextFileNotFoundError we check for the asyncapi.yml file in the working directory and throw an error if it is not found. This way the default behavior will not change but fix the issue with this scenario.

@derberg what do you think, which approach should I follow?


export const useSpecfile = (flags: useSpecFileInput): useSpecFileOutput => {
const contextService: ContextService = container.resolve(ContextService);
try {
if (flags.file) {
const specFile: SpecificationFile = new SpecificationFile(flags.file);
if (specFile.isNotValid()) { throw new Error('Invalid spec path'); }
return { specFile };
}
const ctx: Context = contextService.loadContextFile();
if (flags.context) {
const ctxFile = ctx.store[flags.context];
if (!ctxFile) { throw new ContextNotFoundError(flags.context); }
const specFile = new SpecificationFile(ctxFile);
return { specFile };
}
if (ctx.current) {
const currentFile = ctx.store[ctx.current];
if (!currentFile) { throw new MissingCurrentContextError(); }
const specFile = new SpecificationFile(currentFile);
return { specFile };
}
const autoDetectedSpecPath = contextService.autoDetectSpecFile();
if (typeof autoDetectedSpecPath === 'undefined') { throw new Error('No spec path found in your working directory, please use flags or store a context'); }
const specFile = new SpecificationFile(autoDetectedSpecPath);
return { specFile };
} catch (error) {
return { error };
}
};

@derberg
Copy link
Member Author

derberg commented Aug 11, 2021

I'm definitely with 2, just make sure message thrown to the user clearly explains cause and next steps

@asyncapi-bot
Copy link
Contributor

🎉 This issue has been resolved in version 0.3.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants