Skip to content

Commit e426651

Browse files
committed
Added check_syntax for cf3
Ticket: ENT-12737 Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent 0aa5b27 commit e426651

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

scripts/markdown-code-checker.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from cfbs.pretty import pretty_file
2+
from shutil import which
23
import markdown_it
34
import os
45
import argparse
56
import sys
7+
import subprocess
68

79

810
def extract_inline_code(file_path, languages):
@@ -73,8 +75,28 @@ def extract(path, i, language, first_line, last_line):
7375
f.write(code_snippet)
7476

7577

76-
def check_syntax():
77-
pass
78+
def check_syntax(path, i, language, first_line, last_line):
79+
file_name = f"{path}.snippet-{i}.{language}"
80+
abs_file_name = os.path.abspath(file_name)
81+
82+
if not os.path.exists(file_name):
83+
print(
84+
f"[error] Couldn't find the file '{file_name}'. Run --extract to extract the inline code."
85+
)
86+
return
87+
88+
match language:
89+
case "cf":
90+
p = subprocess.run(
91+
["/var/cfengine/bin/cf-promises", abs_file_name],
92+
capture_output=True,
93+
text=True,
94+
)
95+
err = p.stderr
96+
97+
if err:
98+
err = err.replace(abs_file_name, f"{path}:{first_line}")
99+
print(err)
78100

79101

80102
def check_output():
@@ -181,6 +203,14 @@ def parse_args():
181203
print("[error] This path doesn't exist")
182204
sys.exit(-1)
183205

206+
if (
207+
args.syntax_check
208+
and "cf3" in args.languages
209+
and not which("/var/cfengine/bin/cf-promises")
210+
):
211+
print("[error] cf-promises is not installed")
212+
sys.exit(-1)
213+
184214
for language in args.languages:
185215
if language not in supported_languages:
186216
print(
@@ -209,7 +239,13 @@ def parse_args():
209239
)
210240

211241
if args.syntax_check and "novalidate" not in code_block["flags"]:
212-
check_syntax()
242+
check_syntax(
243+
path,
244+
i + 1,
245+
supported_languages[code_block["language"]],
246+
code_block["first_line"],
247+
code_block["last_line"],
248+
)
213249

214250
if args.autoformat and "noautoformat" not in code_block["flags"]:
215251
autoformat(

0 commit comments

Comments
 (0)