Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions src/cfengine_cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,33 @@ def deploy() -> int:
return r


def _format_filename(filename, line_length):
def _format_filename(filename, line_length, check):
if filename.startswith("./."):
return
return 0
if filename.endswith(".json"):
format_json_file(filename)
return
return format_json_file(filename, check)
if filename.endswith(".cf"):
format_policy_file(filename, line_length)
return
return format_policy_file(filename, line_length, check)
raise UserError(f"Unrecognized file format: {filename}")


def _format_dirname(directory, line_length):
def _format_dirname(directory, line_length, check):
ret = 0
for filename in find(directory, extension=".json"):
_format_filename(filename, line_length)
ret |= _format_filename(filename, line_length, check)
for filename in find(directory, extension=".cf"):
_format_filename(filename, line_length)
ret |= _format_filename(filename, line_length, check)
return ret


def format(names, line_length) -> int:
def format(names, line_length, check) -> int:
if not names:
_format_dirname(".", line_length)
return 0
return _format_dirname(".", line_length, check)
if len(names) == 1 and names[0] == "-":
# Special case, format policy file from stdin to stdout
format_policy_fin_fout(sys.stdin, sys.stdout, line_length)
return 0
return format_policy_fin_fout(sys.stdin, sys.stdout, line_length, check)

ret = 0
for name in names:
if name == "-":
raise UserError(
Expand All @@ -86,12 +85,12 @@ def format(names, line_length) -> int:
if not os.path.exists(name):
raise UserError(f"{name} does not exist")
if os.path.isfile(name):
_format_filename(name, line_length)
ret |= _format_filename(name, line_length, check)
continue
if os.path.isdir(name):
_format_dirname(name, line_length)
ret |= _format_dirname(name, line_length, check)
continue
return 0
return ret
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@victormlg I think we still want to return 0 if there is no --check and we successfully reformatted the file(s).



def _lint(files, strict) -> int:
Expand Down
2 changes: 1 addition & 1 deletion src/cfengine_cli/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def fn_autoformat(_origin_path, snippet_path, language, _first_line, _last_line)
raise UserError(f"Invalid json in '{snippet_path}'")
case "cf":
# Note: Dead code - Not used for CFEngine policy yet
format_policy_file(snippet_path, 80)
format_policy_file(snippet_path, 80, False)


def _translate_language(x):
Expand Down
23 changes: 19 additions & 4 deletions src/cfengine_cli/format.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import tree_sitter_cfengine as tscfengine
from tree_sitter import Language, Parser, Node
from cfbs.pretty import pretty_file
from cfbs.pretty import pretty_file, pretty_check_file


def format_json_file(filename):
def format_json_file(filename, check):
assert filename.endswith(".json")

if check:
r = not pretty_check_file(filename)
if r:
print(f"JSON file '{filename}' needs reformatting")
return r

r = pretty_file(filename)
if r:
print(f"JSON file '{filename}' was reformatted")
return r


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


def format_policy_file(filename, line_length):
def format_policy_file(filename, line_length, check):
assert filename.endswith(".cf")

PY_LANGUAGE = Language(tscfengine.language())
parser = Parser(PY_LANGUAGE)

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

new_data = fmt.buffer + "\n"
if new_data != original_data.decode("utf-8"):
if check:
print(f"Policy file '{filename}' needs reformatting")
return 1

with open(filename, "w") as f:
f.write(new_data)
print(f"Policy file '{filename}' was reformatted")
return 0


def format_policy_fin_fout(fin, fout, line_length):
def format_policy_fin_fout(fin, fout, line_length, check):
PY_LANGUAGE = Language(tscfengine.language())
parser = Parser(PY_LANGUAGE)

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

new_data = fmt.buffer + "\n"
fout.write(new_data)
return 0
3 changes: 2 additions & 1 deletion src/cfengine_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def _get_arg_parser():
fmt = subp.add_parser("format", help="Autoformat .json and .cf files")
fmt.add_argument("files", nargs="*", help="Files to format")
fmt.add_argument("--line-length", default=80, type=int, help="Maximum line length")
fmt.add_argument("--check", action="store_true")
lnt = subp.add_parser(
"lint",
help="Look for syntax errors and other simple mistakes",
Expand Down Expand Up @@ -136,7 +137,7 @@ def run_command_with_args(args) -> int:
if args.command == "deploy":
return commands.deploy()
if args.command == "format":
return commands.format(args.files, args.line_length)
return commands.format(args.files, args.line_length, args.check)
if args.command == "lint":
return commands.lint(args.files, (args.strict.lower() in ("y", "ye", "yes")))
if args.command == "report":
Expand Down
Loading