From 10d904268286ff67e987728832f39fdbfb63c535 Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem Date: Wed, 18 Mar 2026 17:31:39 +0100 Subject: [PATCH 1/2] Moved some linting wrapper functions into lint.py to more easily reuse them Signed-off-by: Ole Herman Schumacher Elgesem --- src/cfengine_cli/commands.py | 39 +++--------------------------------- src/cfengine_cli/lint.py | 34 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/cfengine_cli/commands.py b/src/cfengine_cli/commands.py index a4cbb1c..1f9f3f5 100644 --- a/src/cfengine_cli/commands.py +++ b/src/cfengine_cli/commands.py @@ -1,11 +1,10 @@ import sys import os import re -import itertools import json from cfengine_cli.profile import profile_cfengine, generate_callstack from cfengine_cli.dev import dispatch_dev_subcommand -from cfengine_cli.lint import lint_cfbs_json, lint_json, lint_policy_file +from cfengine_cli.lint import lint_single_arg, lint_folder from cfengine_cli.shell import user_command from cfengine_cli.paths import bin from cfengine_cli.version import cfengine_cli_version_string @@ -95,47 +94,15 @@ def format(names, line_length) -> int: return 0 -def _lint_folder(folder): - errors = 0 - while folder.endswith(("/.", "/")): - folder = folder[0:-1] - for filename in itertools.chain( - find(folder, extension=".json"), find(folder, extension=".cf") - ): - if filename.startswith(("./.", "./out/", folder + "/.", folder + "/out/")): - continue - if filename.startswith(".") and not filename.startswith("./"): - continue - errors += _lint_single_file(filename) - return errors - - -def _lint_single_file(file): - assert os.path.isfile(file) - if file.endswith("/cfbs.json"): - return lint_cfbs_json(file) - if file.endswith(".json"): - return lint_json(file) - assert file.endswith(".cf") - return lint_policy_file(file) - - -def _lint_single_arg(arg): - if os.path.isdir(arg): - return _lint_folder(arg) - assert os.path.isfile(arg) - return _lint_single_file(arg) - - def _lint(files) -> int: if not files: - return _lint_folder(".") + return lint_folder(".") errors = 0 for file in files: - errors += _lint_single_arg(file) + errors += lint_single_arg(file) return errors diff --git a/src/cfengine_cli/lint.py b/src/cfengine_cli/lint.py index 3884291..9349f8f 100644 --- a/src/cfengine_cli/lint.py +++ b/src/cfengine_cli/lint.py @@ -12,10 +12,12 @@ import os import json +import itertools import tree_sitter_cfengine as tscfengine from tree_sitter import Language, Parser from cfbs.validate import validate_config from cfbs.cfbs_config import CFBSConfig +from cfbs.utils import find DEPRECATED_PROMISE_TYPES = ["defaults", "guest_environments"] ALLOWED_BUNDLE_TYPES = ["agent", "common", "monitor", "server", "edit_line", "edit_xml"] @@ -231,3 +233,35 @@ def lint_policy_file( else: print(f"FAIL: {filename} ({errors} error{'s' if errors > 0 else ''})") return errors + + +def lint_folder(folder): + errors = 0 + while folder.endswith(("/.", "/")): + folder = folder[0:-1] + for filename in itertools.chain( + find(folder, extension=".json"), find(folder, extension=".cf") + ): + if filename.startswith(("./.", "./out/", folder + "/.", folder + "/out/")): + continue + if filename.startswith(".") and not filename.startswith("./"): + continue + errors += lint_single_file(filename) + return errors + + +def lint_single_file(file): + assert os.path.isfile(file) + if file.endswith("/cfbs.json"): + return lint_cfbs_json(file) + if file.endswith(".json"): + return lint_json(file) + assert file.endswith(".cf") + return lint_policy_file(file) + + +def lint_single_arg(arg): + if os.path.isdir(arg): + return lint_folder(arg) + assert os.path.isfile(arg) + return lint_single_file(arg) From d1f16719ea1e02a0cc67089db5b929c172b60f28 Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem Date: Wed, 18 Mar 2026 17:36:53 +0100 Subject: [PATCH 2/2] Added cfengine lint to cfengine dev lint-docs Signed-off-by: Ole Herman Schumacher Elgesem --- src/cfengine_cli/docs.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cfengine_cli/docs.py b/src/cfengine_cli/docs.py index 14dea09..d90cebd 100644 --- a/src/cfengine_cli/docs.py +++ b/src/cfengine_cli/docs.py @@ -16,7 +16,7 @@ from cfbs.pretty import pretty_file from cfbs.utils import find -from cfengine_cli.lint import lint_policy_file +from cfengine_cli.lint import lint_folder, lint_policy_file from cfengine_cli.utils import UserError IGNORED_DIRS = [".git"] @@ -409,6 +409,9 @@ def check_docs() -> int: Run by the command: cfengine dev lint-docs""" + r = lint_folder(".") + if r != 0: + return r _process_markdown_code_blocks( path=".", languages=["json", "cf3"],