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

Fix mdox-gen-exec issues with --help commands #17

Merged
merged 3 commits into from
Mar 25, 2021

Conversation

saswatamcode
Copy link
Collaborator

This PR fixes the issue where commands with non 0 exit codes (usually --help commands) fail to format. Fixes #10
Before this fix, the markdown below results in an error,

```bash mdox-gen-exec="go --help"
.
.
.
error:  first formatting phase for /Users/saswatamukherjee/web/mdox/test.md: run go --help: exit status 2

A workaround is using something like mdox-gen-exec="sh -c 'go --help || exit 0'" but this is not preferable.
With this fix, a flag can be passed to allow non zero exit codes to format successfully, like below,

```bash mdox-nonzero-exit-code mdox-gen-exec="go --help"
Go is a tool for managing Go source code.

Usage:

	go <command> [arguments]
...

This would also be suitable in case an error code block example needs to be generated.

Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Copy link
Owner

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Epic work, thanks!

Wonder... what would be the best experience here. Some suggestions:

  1. What about opposite: have option expect-exit-code = 2 and by default don't check exit code?
  2. Can we run quick test to ensure this? 🤗

@@ -19,6 +19,7 @@ const (
infoStringKeyLang = "mdox-gen-lang"
infoStringKeyType = "mdox-gen-type"
infoStringKeyExec = "mdox-gen-exec"
skipExitCodeCheck = "mdox-nonzero-exit-code"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder... should we just don't care about exit codes? 🤔

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also variable name is not telling the same as string

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, I did think that not checking for exit codes would be better, but then user wouldn’t be able to tell which command in a md code block had an actual error and that might get formatted without the user noticing. For eg npm —versionn is a typo which would come up as an error if we check the exit code. However, if we skip that then something entirely different than the version number might get formatted.
Would the better option be to have an expect-exit-code flag which will be matched if non 0 exit code occurs?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think tag makes sense more like you did.

So what about by default expecting status code 0, but allow expect-exit-code=X to change that? 🤗

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! That would be best. Implementing that along with a quick test! 😊

Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Copy link
Owner

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing, looks good. Just some suggestions still.

infoStringKeyLang = "mdox-gen-lang"
infoStringKeyType = "mdox-gen-type"
infoStringKeyExec = "mdox-gen-exec"
infoStringKeyExitCode = "expect-exit-code"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
infoStringKeyExitCode = "expect-exit-code"
infoStringKeyExitCode = "mdox-expect-exit-code"

@@ -38,12 +40,12 @@ func (t *genCodeBlockTransformer) TransformCodeBlock(ctx mdformatter.SourceConte
}
infoStringAttr := map[string]string{}
for i, field := range infoFiels {
if val := strings.Split(field, "="); val[0] == infoStringKeyLang || val[0] == infoStringKeyType || val[0] == infoStringKeyExec {
if val := strings.Split(field, "="); val[0] == infoStringKeyLang || val[0] == infoStringKeyType || val[0] == infoStringKeyExec || val[0] == infoStringKeyExitCode {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time for some switch here (:

if i == 0 {
return nil, errors.Errorf("missing language info in fenced code block. Got info string %q", string(infoString))
}
if len(val) != 2 {
return nil, errors.Errorf("got %q without variable. Expected format is e.g ```yaml %q=<value> %q=<value2> . Got info string %q", val[0], infoStringKeyLang, infoStringKeyType, string(infoString))
return nil, errors.Errorf("got %q without variable. Expected format is e.g ```yaml %q=<value> %q=<value2> %q=<value2>. Got info string %q", val[0], infoStringKeyLang, infoStringKeyExitCode, infoStringKeyType, string(infoString))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Copy link
Owner

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks!

LGTM 💪🏽

@bwplotka bwplotka merged commit 1d5f977 into bwplotka:master Mar 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mdox-gen-exec Make it easier to use with --help
2 participants