Skip to content

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

Merged
bwplotka merged 3 commits intobwplotka:masterfrom
saswatamcode:gen-exec-exit-code
Mar 25, 2021
Merged

Fix mdox-gen-exec issues with --help commands#17
bwplotka merged 3 commits intobwplotka:masterfrom
saswatamcode:gen-exec-exit-code

Conversation

@saswatamcode
Copy link
Copy Markdown
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
Copy Markdown
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? 🤗

infoStringKeyLang = "mdox-gen-lang"
infoStringKeyType = "mdox-gen-type"
infoStringKeyExec = "mdox-gen-exec"
skipExitCodeCheck = "mdox-nonzero-exit-code"
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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>
@saswatamcode saswatamcode requested a review from bwplotka March 21, 2021 13:43
Copy link
Copy Markdown
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
Copy Markdown
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"

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
Copy Markdown
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 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
Copy Markdown
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
Copy Markdown
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