Skip to content

Commit cf98327

Browse files
authored
Merge pull request #29 from olehermanse/main
Added cfengine lint to cfengine dev lint-docs
2 parents 2847e64 + d1f1671 commit cf98327

File tree

3 files changed

+41
-37
lines changed

3 files changed

+41
-37
lines changed

src/cfengine_cli/commands.py

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import sys
22
import os
33
import re
4-
import itertools
54
import json
65
from cfengine_cli.profile import profile_cfengine, generate_callstack
76
from cfengine_cli.dev import dispatch_dev_subcommand
8-
from cfengine_cli.lint import lint_cfbs_json, lint_json, lint_policy_file
7+
from cfengine_cli.lint import lint_single_arg, lint_folder
98
from cfengine_cli.shell import user_command
109
from cfengine_cli.paths import bin
1110
from cfengine_cli.version import cfengine_cli_version_string
@@ -95,47 +94,15 @@ def format(names, line_length) -> int:
9594
return 0
9695

9796

98-
def _lint_folder(folder):
99-
errors = 0
100-
while folder.endswith(("/.", "/")):
101-
folder = folder[0:-1]
102-
for filename in itertools.chain(
103-
find(folder, extension=".json"), find(folder, extension=".cf")
104-
):
105-
if filename.startswith(("./.", "./out/", folder + "/.", folder + "/out/")):
106-
continue
107-
if filename.startswith(".") and not filename.startswith("./"):
108-
continue
109-
errors += _lint_single_file(filename)
110-
return errors
111-
112-
113-
def _lint_single_file(file):
114-
assert os.path.isfile(file)
115-
if file.endswith("/cfbs.json"):
116-
return lint_cfbs_json(file)
117-
if file.endswith(".json"):
118-
return lint_json(file)
119-
assert file.endswith(".cf")
120-
return lint_policy_file(file)
121-
122-
123-
def _lint_single_arg(arg):
124-
if os.path.isdir(arg):
125-
return _lint_folder(arg)
126-
assert os.path.isfile(arg)
127-
return _lint_single_file(arg)
128-
129-
13097
def _lint(files) -> int:
13198

13299
if not files:
133-
return _lint_folder(".")
100+
return lint_folder(".")
134101

135102
errors = 0
136103

137104
for file in files:
138-
errors += _lint_single_arg(file)
105+
errors += lint_single_arg(file)
139106

140107
return errors
141108

src/cfengine_cli/docs.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from cfbs.pretty import pretty_file
1717
from cfbs.utils import find
1818

19-
from cfengine_cli.lint import lint_policy_file
19+
from cfengine_cli.lint import lint_folder, lint_policy_file
2020
from cfengine_cli.utils import UserError
2121

2222
IGNORED_DIRS = [".git"]
@@ -409,6 +409,9 @@ def check_docs() -> int:
409409
410410
Run by the command:
411411
cfengine dev lint-docs"""
412+
r = lint_folder(".")
413+
if r != 0:
414+
return r
412415
_process_markdown_code_blocks(
413416
path=".",
414417
languages=["json", "cf3"],

src/cfengine_cli/lint.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
import os
1414
import json
15+
import itertools
1516
import tree_sitter_cfengine as tscfengine
1617
from tree_sitter import Language, Parser
1718
from cfbs.validate import validate_config
1819
from cfbs.cfbs_config import CFBSConfig
20+
from cfbs.utils import find
1921

2022
DEPRECATED_PROMISE_TYPES = ["defaults", "guest_environments"]
2123
ALLOWED_BUNDLE_TYPES = ["agent", "common", "monitor", "server", "edit_line", "edit_xml"]
@@ -231,3 +233,35 @@ def lint_policy_file(
231233
else:
232234
print(f"FAIL: {filename} ({errors} error{'s' if errors > 0 else ''})")
233235
return errors
236+
237+
238+
def lint_folder(folder):
239+
errors = 0
240+
while folder.endswith(("/.", "/")):
241+
folder = folder[0:-1]
242+
for filename in itertools.chain(
243+
find(folder, extension=".json"), find(folder, extension=".cf")
244+
):
245+
if filename.startswith(("./.", "./out/", folder + "/.", folder + "/out/")):
246+
continue
247+
if filename.startswith(".") and not filename.startswith("./"):
248+
continue
249+
errors += lint_single_file(filename)
250+
return errors
251+
252+
253+
def lint_single_file(file):
254+
assert os.path.isfile(file)
255+
if file.endswith("/cfbs.json"):
256+
return lint_cfbs_json(file)
257+
if file.endswith(".json"):
258+
return lint_json(file)
259+
assert file.endswith(".cf")
260+
return lint_policy_file(file)
261+
262+
263+
def lint_single_arg(arg):
264+
if os.path.isdir(arg):
265+
return lint_folder(arg)
266+
assert os.path.isfile(arg)
267+
return lint_single_file(arg)

0 commit comments

Comments
 (0)