Skip to content

Commit 9329cb0

Browse files
authored
Merge pull request #56 from victormlg/check
ENT-13836: Added --check to cfengine format
2 parents 3a7bea3 + e5e2bd9 commit 9329cb0

File tree

4 files changed

+38
-23
lines changed

4 files changed

+38
-23
lines changed

src/cfengine_cli/commands.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,34 +50,33 @@ def deploy() -> int:
5050
return r
5151

5252

53-
def _format_filename(filename, line_length):
53+
def _format_filename(filename, line_length, check):
5454
if filename.startswith("./."):
55-
return
55+
return 0
5656
if filename.endswith(".json"):
57-
format_json_file(filename)
58-
return
57+
return format_json_file(filename, check)
5958
if filename.endswith(".cf"):
60-
format_policy_file(filename, line_length)
61-
return
59+
return format_policy_file(filename, line_length, check)
6260
raise UserError(f"Unrecognized file format: {filename}")
6361

6462

65-
def _format_dirname(directory, line_length):
63+
def _format_dirname(directory, line_length, check):
64+
ret = 0
6665
for filename in find(directory, extension=".json"):
67-
_format_filename(filename, line_length)
66+
ret |= _format_filename(filename, line_length, check)
6867
for filename in find(directory, extension=".cf"):
69-
_format_filename(filename, line_length)
68+
ret |= _format_filename(filename, line_length, check)
69+
return ret
7070

7171

72-
def format(names, line_length) -> int:
72+
def format(names, line_length, check) -> int:
7373
if not names:
74-
_format_dirname(".", line_length)
75-
return 0
74+
return _format_dirname(".", line_length, check)
7675
if len(names) == 1 and names[0] == "-":
7776
# Special case, format policy file from stdin to stdout
78-
format_policy_fin_fout(sys.stdin, sys.stdout, line_length)
79-
return 0
77+
return format_policy_fin_fout(sys.stdin, sys.stdout, line_length, check)
8078

79+
ret = 0
8180
for name in names:
8281
if name == "-":
8382
raise UserError(
@@ -86,12 +85,12 @@ def format(names, line_length) -> int:
8685
if not os.path.exists(name):
8786
raise UserError(f"{name} does not exist")
8887
if os.path.isfile(name):
89-
_format_filename(name, line_length)
88+
ret |= _format_filename(name, line_length, check)
9089
continue
9190
if os.path.isdir(name):
92-
_format_dirname(name, line_length)
91+
ret |= _format_dirname(name, line_length, check)
9392
continue
94-
return 0
93+
return ret
9594

9695

9796
def _lint(files, strict) -> int:

src/cfengine_cli/docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def fn_autoformat(_origin_path, snippet_path, language, _first_line, _last_line)
212212
raise UserError(f"Invalid json in '{snippet_path}'")
213213
case "cf":
214214
# Note: Dead code - Not used for CFEngine policy yet
215-
format_policy_file(snippet_path, 80)
215+
format_policy_file(snippet_path, 80, False)
216216

217217

218218
def _translate_language(x):

src/cfengine_cli/format.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import tree_sitter_cfengine as tscfengine
22
from tree_sitter import Language, Parser, Node
3-
from cfbs.pretty import pretty_file
3+
from cfbs.pretty import pretty_file, pretty_check_file
44

55

6-
def format_json_file(filename):
6+
def format_json_file(filename, check):
77
assert filename.endswith(".json")
8+
9+
if check:
10+
r = not pretty_check_file(filename)
11+
if r:
12+
print(f"JSON file '{filename}' needs reformatting")
13+
return r
14+
815
r = pretty_file(filename)
916
if r:
1017
print(f"JSON file '{filename}' was reformatted")
18+
return r
1119

1220

1321
def text(node: Node):
@@ -534,8 +542,9 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
534542
fmt.print(node, indent)
535543

536544

537-
def format_policy_file(filename, line_length):
545+
def format_policy_file(filename, line_length, check):
538546
assert filename.endswith(".cf")
547+
539548
PY_LANGUAGE = Language(tscfengine.language())
540549
parser = Parser(PY_LANGUAGE)
541550

@@ -551,12 +560,17 @@ def format_policy_file(filename, line_length):
551560

552561
new_data = fmt.buffer + "\n"
553562
if new_data != original_data.decode("utf-8"):
563+
if check:
564+
print(f"Policy file '{filename}' needs reformatting")
565+
return 1
566+
554567
with open(filename, "w") as f:
555568
f.write(new_data)
556569
print(f"Policy file '{filename}' was reformatted")
570+
return 0
557571

558572

559-
def format_policy_fin_fout(fin, fout, line_length):
573+
def format_policy_fin_fout(fin, fout, line_length, check):
560574
PY_LANGUAGE = Language(tscfengine.language())
561575
parser = Parser(PY_LANGUAGE)
562576

@@ -571,3 +585,4 @@ def format_policy_fin_fout(fin, fout, line_length):
571585

572586
new_data = fmt.buffer + "\n"
573587
fout.write(new_data)
588+
return 0

src/cfengine_cli/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def _get_arg_parser():
4848
fmt = subp.add_parser("format", help="Autoformat .json and .cf files")
4949
fmt.add_argument("files", nargs="*", help="Files to format")
5050
fmt.add_argument("--line-length", default=80, type=int, help="Maximum line length")
51+
fmt.add_argument("--check", action="store_true")
5152
lnt = subp.add_parser(
5253
"lint",
5354
help="Look for syntax errors and other simple mistakes",
@@ -136,7 +137,7 @@ def run_command_with_args(args) -> int:
136137
if args.command == "deploy":
137138
return commands.deploy()
138139
if args.command == "format":
139-
return commands.format(args.files, args.line_length)
140+
return commands.format(args.files, args.line_length, args.check)
140141
if args.command == "lint":
141142
return commands.lint(args.files, (args.strict.lower() in ("y", "ye", "yes")))
142143
if args.command == "report":

0 commit comments

Comments
 (0)