Skip to content

Commit 25fa2b1

Browse files
committed
cfengine dev lint-docs: Improved logging and error messages
Signed-off-by: Ole Herman Schumacher Elgesem <[email protected]>
1 parent 5879e84 commit 25fa2b1

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/cfengine_cli/docs.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,16 @@ def fn_extract(origin_path, snippet_path, _language, first_line, last_line):
109109
raise UserError(f"Couldn't open '{origin_path}' or '{snippet_path}'")
110110

111111

112+
def lint_json_file(snippet_abs_path, origin_path, snippet_number, first_line, prefix=None):
113+
with open(snippet_abs_path, "r") as f:
114+
json.loads(f.read())
115+
if prefix:
116+
print(prefix, end="")
117+
print(f"PASS: Snippet {snippet_number} at '{origin_path}:{first_line}' (json)")
118+
return 0
119+
112120
def fn_check_syntax(
113-
origin_path, snippet_path, language, first_line, _last_line, snippet_number
121+
origin_path, snippet_path, language, first_line, _last_line, snippet_number, prefix=None
114122
):
115123
snippet_abs_path = os.path.abspath(snippet_path)
116124

@@ -122,14 +130,13 @@ def fn_check_syntax(
122130
match language:
123131
case "cf":
124132
r = lint_policy_file(
125-
snippet_abs_path, origin_path, first_line + 1, snippet_number
133+
snippet_abs_path, origin_path, first_line + 1, snippet_number, prefix
126134
)
127135
if r != 0:
128136
raise UserError(f"Error when checking '{origin_path}'")
129137
case "json":
130138
try:
131-
with open(snippet_abs_path, "r") as f:
132-
json.loads(f.read())
139+
lint_json_file(snippet_abs_path, origin_path, snippet_number, first_line, prefix)
133140
except json.decoder.JSONDecodeError as e:
134141
raise UserError(f"Error when checking '{snippet_abs_path}': {str(e)}")
135142
except Exception as e:
@@ -220,7 +227,12 @@ def _process_markdown_code_blocks(
220227

221228
parsed_markdowns = get_markdown_files(path, languages)
222229

223-
for origin_path in parsed_markdowns["files"].keys():
230+
origin_paths = sorted(parsed_markdowns["files"].keys())
231+
origin_paths_len = len(origin_paths)
232+
233+
for origin_paths_i, origin_path in enumerate(origin_paths):
234+
percentage = int(100 * (origin_paths_i + 1)/origin_paths_len)
235+
prefix = f"[{origin_paths_i + 1}/{origin_paths_len} ({percentage}%)] "
224236
offset = 0
225237
for i, code_block in enumerate(
226238
parsed_markdowns["files"][origin_path]["code-blocks"]
@@ -257,6 +269,7 @@ def _process_markdown_code_blocks(
257269
code_block["first_line"],
258270
code_block["last_line"],
259271
snippet_number,
272+
prefix
260273
)
261274
except Exception as e:
262275
if cleanup:

src/cfengine_cli/lint.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def _walk(filename, lines, node) -> int:
9898

9999

100100
def lint_policy_file(
101-
filename, original_filename=None, original_line=None, snippet=None
101+
filename, original_filename=None, original_line=None, snippet=None, prefix=None
102102
):
103103
assert original_filename is None or type(original_filename) is str
104104
assert original_line is None or type(original_line) is int
@@ -123,7 +123,14 @@ def lint_policy_file(
123123

124124
root_node = tree.root_node
125125
if root_node.type != "source_file":
126-
print(f"Error: Failed to parse snippet ('{filename}') - Is this valid CFEngine policy?")
126+
if snippet:
127+
assert original_filename and original_line
128+
print(
129+
f"Error: Failed to parse policy snippet {snippet} at '{original_filename}:{original_line}'"
130+
)
131+
else:
132+
print(f" Empty policy file '{filename}'")
133+
print(" Is this valid CFEngine policy?")
127134
print("")
128135
lines = original_data.decode().split("\n")
129136
if not len(lines) <= 5:
@@ -144,17 +151,19 @@ def lint_policy_file(
144151
print(f"Error: Empty policy file '{filename}'")
145152
errors += 1
146153
errors += _walk(filename, lines, root_node)
154+
if prefix:
155+
print(prefix, end="")
147156
if errors == 0:
148157
if snippet:
149158
assert original_filename and original_line
150-
print(f"PASS: Snippet {snippet} at '{original_filename}:{original_line}'")
159+
print(f"PASS: Snippet {snippet} at '{original_filename}:{original_line}' (cf3)")
151160
else:
152161
print(f"PASS: {filename}")
153162
return 0
154163

155164
if snippet:
156165
assert original_filename and original_line
157-
print(f"FAIL: Snippet {snippet} at '{original_filename}:{original_line}'")
166+
print(f"FAIL: Snippet {snippet} at '{original_filename}:{original_line}' (cf3)")
158167
else:
159168
print(f"FAIL: {filename} ({errors} error{'s' if errors > 0 else ''})")
160169
return errors

0 commit comments

Comments
 (0)