Skip to content

Commit b5ba87b

Browse files
committed
Added linting for cfengine policy to docs-check command
Signed-off-by: Ole Herman Schumacher Elgesem <ole.elgesem@northern.tech>
1 parent 36da93b commit b5ba87b

2 files changed

Lines changed: 5 additions & 28 deletions

File tree

src/cfengine_cli/docs.py

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import markdown_it
1717
from cfbs.pretty import pretty_file
1818

19+
from cfengine_cli.lint import lint_policy_file
1920
from cfengine_cli.utils import UserError
2021

2122

@@ -119,25 +120,9 @@ def fn_check_syntax(origin_path, snippet_path, language, first_line, _last_line)
119120

120121
match language:
121122
case "cf":
122-
try:
123-
p = subprocess.run(
124-
["/var/cfengine/bin/cf-promises", snippet_abs_path],
125-
capture_output=True,
126-
text=True,
127-
)
128-
err = p.stderr
129-
130-
if err:
131-
err = err.replace(snippet_abs_path, f"{origin_path}:{first_line}")
132-
print(err)
133-
except OSError:
134-
raise UserError(f"'{snippet_abs_path}' doesn't exist")
135-
except ValueError:
136-
raise UserError("Invalid subprocess arguments")
137-
except subprocess.CalledProcessError:
138-
raise UserError(f"Couldn't run cf-promises on '{snippet_abs_path}'")
139-
except subprocess.TimeoutExpired:
140-
raise UserError("Timed out")
123+
r = lint_policy_file(snippet_abs_path)
124+
if r != 0:
125+
raise UserError(f"Error when checking '{snippet_abs_path}'")
141126
case "json":
142127
try:
143128
with open(snippet_abs_path, "r") as f:
@@ -207,14 +192,6 @@ def _process_markdown_code_blocks(
207192
if not os.path.exists(path):
208193
raise UserError("This path doesn't exist")
209194

210-
# TODO: Switch to tree-sitter parser here:
211-
if (
212-
syntax_check
213-
and "cf3" in languages
214-
and not which("/var/cfengine/bin/cf-promises")
215-
):
216-
raise UserError("cf-promises is not installed")
217-
218195
for language in languages:
219196
if language not in supported_languages:
220197
raise UserError(

src/cfengine_cli/lint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def _walk(filename, lines, node) -> int:
9999

100100
def lint_policy_file(filename):
101101
assert os.path.isfile(filename)
102-
assert filename.endswith(".cf")
102+
assert filename.endswith((".cf", ".cfengine3", ".cf3", ".cf.sub"))
103103
PY_LANGUAGE = Language(tscfengine.language())
104104
parser = Parser(PY_LANGUAGE)
105105

0 commit comments

Comments
 (0)