From 8be84e999f8c47b42d1a7a58fb1f32c901c7df2a Mon Sep 17 00:00:00 2001 From: Bartlomiej Plotka Date: Fri, 11 Jun 2021 21:22:22 +0200 Subject: [PATCH] Fixed mdox gen errors on command with `=` inside (common case). Signed-off-by: Bartlomiej Plotka --- pkg/mdformatter/mdgen/mdgen.go | 36 +++++-------------- .../mdgen/testdata/mdgen_formatted.md | 9 ++--- .../mdgen/testdata/mdgen_not_formatted.md | 7 ++-- 3 files changed, 17 insertions(+), 35 deletions(-) diff --git a/pkg/mdformatter/mdgen/mdgen.go b/pkg/mdformatter/mdgen/mdgen.go index c8a3b62..23af687 100644 --- a/pkg/mdformatter/mdgen/mdgen.go +++ b/pkg/mdformatter/mdgen/mdgen.go @@ -5,7 +5,6 @@ package mdgen import ( "bytes" - "context" "os/exec" "strconv" "strings" @@ -17,8 +16,6 @@ import ( ) const ( - infoStringKeyLang = "mdox-gen-lang" - infoStringKeyType = "mdox-gen-type" infoStringKeyExec = "mdox-exec" infoStringKeyExitCode = "mdox-expect-exit-code" ) @@ -40,19 +37,22 @@ func (t *genCodeBlockTransformer) TransformCodeBlock(ctx mdformatter.SourceConte } infoStringAttr := map[string]string{} for i, field := range infoFiels { - val := strings.Split(field, "=") + val := []string{field} + if i := strings.Index(field, "="); i != -1 { + val = []string{field[:i], field[i+1:]} + } if i == 0 && len(val) == 2 { return nil, errors.Errorf("missing language info in fenced code block. Got info string %q", string(infoString)) } switch val[0] { - case infoStringKeyExec, infoStringKeyExitCode: + case infoStringKeyExec: if len(val) != 2 { - return nil, errors.Errorf("got %q without variable. Expected format is e.g ```yaml %q= %q=. Got info string %q", val[0], infoStringKeyExitCode, infoStringKeyExec, string(infoString)) + return nil, errors.Errorf("got %q without variable. Expected format is e.g ```yaml %s=\"\" but got %s", val[0], infoStringKeyExec, string(infoString)) } infoStringAttr[val[0]] = val[1] - case infoStringKeyLang, infoStringKeyType: + case infoStringKeyExitCode: if len(val) != 2 { - return nil, errors.Errorf("got %q without variable. Expected format is e.g ```yaml %q= %q=. Got info string %q", val[0], infoStringKeyLang, infoStringKeyType, string(infoString)) + return nil, errors.Errorf("got %q without variable. Expected format is e.g ```yaml %s=\"\" but got %s", val[0], infoStringKeyExitCode, string(infoString)) } infoStringAttr[val[0]] = val[1] } @@ -82,30 +82,12 @@ func (t *genCodeBlockTransformer) TransformCodeBlock(ctx mdformatter.SourceConte if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == expectedCode { return b.Bytes(), nil } - return nil, errors.Wrapf(err, "run %v", execCmd) + return nil, errors.Wrapf(err, "run %v, out: %v", execCmd, b.String()) } return b.Bytes(), nil } - lang, langOk := infoStringAttr[infoStringKeyLang] - typePath, typOk := infoStringAttr[infoStringKeyType] - if typOk || langOk { - if typOk != langOk { - return nil, errors.Errorf("got ambiguous attributes: %v. Expected is e.g ```yaml %q= %q=go . Got info string %q", infoStringAttr, infoStringKeyType, infoStringKeyLang, string(infoString)) - } - switch lang { - case "go", "golang": - return genGo(ctx, "", typePath) - default: - return nil, errors.Errorf("expected language a first element of info string got %q; Got info string %q", lang, string(infoString)) - } - } panic("should never get here") } func (t *genCodeBlockTransformer) Close(ctx mdformatter.SourceContext) error { return nil } - -func genGo(ctx context.Context, moduleRoot string, typePath string) ([]byte, error) { - // TODO(bwplotka): To be done. - return nil, nil -} diff --git a/pkg/mdformatter/mdgen/testdata/mdgen_formatted.md b/pkg/mdformatter/mdgen/testdata/mdgen_formatted.md index a423640..90f1581 100755 --- a/pkg/mdformatter/mdgen/testdata/mdgen_formatted.md +++ b/pkg/mdformatter/mdgen/testdata/mdgen_formatted.md @@ -4,10 +4,6 @@ test output ``` -```yaml mdox-gen-lang="go" mdox-gen-type="github.com/bwplotka/mdox/pkg/mdox/testdata.Config" -TO BE DONE -``` - ```yaml abc sad @@ -40,3 +36,8 @@ test output3 echo "test output3" ``` + +```yaml mdox-exec="bash ./testdata/out2.sh --name=queryfrontend.InMemoryResponseCacheConfig" +test output2 +newline +``` diff --git a/pkg/mdformatter/mdgen/testdata/mdgen_not_formatted.md b/pkg/mdformatter/mdgen/testdata/mdgen_not_formatted.md index 3085b70..657f37a 100644 --- a/pkg/mdformatter/mdgen/testdata/mdgen_not_formatted.md +++ b/pkg/mdformatter/mdgen/testdata/mdgen_not_formatted.md @@ -6,10 +6,6 @@ a adf ``` -```yaml mdox-gen-lang="go" mdox-gen-type="github.com/bwplotka/mdox/pkg/mdox/testdata.Config" -TO BE DONE -``` - ```yaml abc sad @@ -41,3 +37,6 @@ abc ```bash mdox-exec="sed -n '1,3p' ./testdata/out3.sh" ``` + +```yaml mdox-exec="bash ./testdata/out2.sh --name=queryfrontend.InMemoryResponseCacheConfig" +```