Skip to content

Commit 4f4c0d6

Browse files
olehermanseclaude
andcommitted
cfengine dev format-docs: Now also runs cfengine format
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
1 parent 28dd082 commit 4f4c0d6

3 files changed

Lines changed: 75 additions & 70 deletions

File tree

src/cfengine_cli/commands.py

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
import os
32
import re
43
import json
@@ -9,11 +8,7 @@
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
12-
from cfengine_cli.format import (
13-
format_policy_file,
14-
format_json_file,
15-
format_policy_fin_fout,
16-
)
11+
from cfengine_cli.format import format_paths
1712
from cfengine_cli.utils import UserError
1813
from cfengine_cli.up import validate_config
1914
from cfbs.commands import build_command
@@ -51,76 +46,14 @@ def deploy() -> int:
5146
return r
5247

5348

54-
def _format_filename(filename: str, line_length: int, check: bool) -> int:
55-
"""Format a single file.
56-
57-
Raises PolicySyntaxError for .cf files with syntax errors."""
58-
if filename.endswith(".json"):
59-
return format_json_file(filename, check)
60-
if filename.endswith(".cf"):
61-
return format_policy_file(filename, line_length, check)
62-
raise UserError(f"Unrecognized file format: {filename}")
63-
64-
65-
def _format_dirname(directory: str, line_length: int, check: bool) -> int:
66-
ret = 0
67-
for root, dirs, files in os.walk(directory):
68-
# Don't recurse into hidden folders
69-
dirs[:] = [d for d in dirs if not d.startswith(".")]
70-
for name in sorted(files):
71-
if name.startswith("."):
72-
continue # Hidden files are ignored by default
73-
if (
74-
name.endswith(".x.cf")
75-
or name.endswith(".input.cf")
76-
or name.endswith(".output.cf")
77-
or name.endswith(".expected.cf")
78-
):
79-
continue # Test files skipped during directory traversal
80-
if name.endswith(
81-
(".input.json", ".jqinput.json", ".x.json", ".expected.json")
82-
):
83-
continue # Test files skipped during directory traversal
84-
filepath = os.path.join(root, name)
85-
if name.endswith(".json") or name.endswith(".cf"):
86-
ret |= _format_filename(filepath, line_length, check)
87-
return ret
88-
89-
9049
def format(names, line_length, check) -> int:
9150
try:
92-
return _format_inner(names, line_length, check)
51+
return format_paths(names, line_length, check)
9352
except PolicySyntaxError as e:
9453
print(f"Error: {e}")
9554
return 1
9655

9756

98-
def _format_inner(names, line_length, check) -> int:
99-
if not names:
100-
return _format_dirname(".", line_length, check)
101-
if len(names) == 1 and names[0] == "-":
102-
# Special case, format policy file from stdin to stdout
103-
return format_policy_fin_fout(sys.stdin, sys.stdout, line_length, check)
104-
105-
ret = 0
106-
for name in names:
107-
if name == "-":
108-
raise UserError(
109-
"The - argument has a special meaning and cannot be combined with other paths"
110-
)
111-
if not os.path.exists(name):
112-
raise UserError(f"{name} does not exist")
113-
if os.path.isfile(name):
114-
ret |= _format_filename(name, line_length, check)
115-
continue
116-
if os.path.isdir(name):
117-
ret |= _format_dirname(name, line_length, check)
118-
continue
119-
if check:
120-
return ret
121-
return 0
122-
123-
12457
def _lint(files, strict) -> int:
12558
if not files:
12659
return lint_args(["."], strict)

src/cfengine_cli/dev.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
)
1111
from cfengine_cli.docs import update_docs, check_docs
1212
from cfengine_cli.syntax_tree import syntax_tree
13+
from cfengine_cli.format import format_paths
1314

1415

1516
def generate_release_information_command(
@@ -49,7 +50,12 @@ def print_dependency_tables(args) -> int:
4950

5051
def format_docs(files) -> int:
5152
_expect_repo("documentation")
52-
return update_docs(files)
53+
r = update_docs(files)
54+
if r != 0:
55+
return r
56+
# Also run `cfengine format` so .json and .cf files get formatted
57+
# without having to run it manually afterwards.
58+
return format_paths(files, line_length=80, check=False)
5359

5460

5561
def lint_docs() -> int:

src/cfengine_cli/format.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
from typing import IO
44

55
import json
6+
import os
7+
import sys
68

79
import tree_sitter_cfengine as tscfengine
810
from tree_sitter import Language, Parser, Node
911
from cfbs.pretty import pretty_file, pretty_check_file
1012
from cfengine_cli.lint import check_policy_syntax
13+
from cfengine_cli.utils import UserError
1114

1215
# Node types that increase indentation by 2 when entered
1316
INDENTED_TYPES = {
@@ -985,3 +988,66 @@ def format_policy_fin_fout(
985988
new_data = fmt.buffer + "\n"
986989
fout.write(new_data)
987990
return 0
991+
992+
993+
def _format_filename(filename: str, line_length: int, check: bool) -> int:
994+
"""Format a single file.
995+
996+
Raises PolicySyntaxError for .cf files with syntax errors."""
997+
if filename.endswith(".json"):
998+
return format_json_file(filename, check)
999+
if filename.endswith(".cf"):
1000+
return format_policy_file(filename, line_length, check)
1001+
raise UserError(f"Unrecognized file format: {filename}")
1002+
1003+
1004+
def _format_dirname(directory: str, line_length: int, check: bool) -> int:
1005+
ret = 0
1006+
for root, dirs, files in os.walk(directory):
1007+
# Don't recurse into hidden folders
1008+
dirs[:] = [d for d in dirs if not d.startswith(".")]
1009+
for name in sorted(files):
1010+
if name.startswith("."):
1011+
continue # Hidden files are ignored by default
1012+
if (
1013+
name.endswith(".x.cf")
1014+
or name.endswith(".input.cf")
1015+
or name.endswith(".output.cf")
1016+
or name.endswith(".expected.cf")
1017+
):
1018+
continue # Test files skipped during directory traversal
1019+
if name.endswith(
1020+
(".input.json", ".jqinput.json", ".x.json", ".expected.json")
1021+
):
1022+
continue # Test files skipped during directory traversal
1023+
filepath = os.path.join(root, name)
1024+
if name.endswith(".json") or name.endswith(".cf"):
1025+
ret |= _format_filename(filepath, line_length, check)
1026+
return ret
1027+
1028+
1029+
def format_paths(names, line_length, check) -> int:
1030+
"""Format the given files / directories (the `cfengine format` logic)."""
1031+
if not names:
1032+
return _format_dirname(".", line_length, check)
1033+
if len(names) == 1 and names[0] == "-":
1034+
# Special case, format policy file from stdin to stdout
1035+
return format_policy_fin_fout(sys.stdin, sys.stdout, line_length, check)
1036+
1037+
ret = 0
1038+
for name in names:
1039+
if name == "-":
1040+
raise UserError(
1041+
"The - argument has a special meaning and cannot be combined with other paths"
1042+
)
1043+
if not os.path.exists(name):
1044+
raise UserError(f"{name} does not exist")
1045+
if os.path.isfile(name):
1046+
ret |= _format_filename(name, line_length, check)
1047+
continue
1048+
if os.path.isdir(name):
1049+
ret |= _format_dirname(name, line_length, check)
1050+
continue
1051+
if check:
1052+
return ret
1053+
return 0

0 commit comments

Comments
 (0)