Skip to content

Commit 13dae18

Browse files
committed
Added args to cfengine format
Signed-off-by: Ole Herman Schumacher Elgesem <[email protected]>
1 parent 0f45f41 commit 13dae18

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ __pycache__
1010
/out/
1111
/tmp/
1212
*.log
13+
*.output.cf

src/cfengine_cli/commands.py

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import sys
12
import os
23
from cfengine_cli.dev import dispatch_dev_subcommand
34
from cfengine_cli.lint import lint_cfbs_json, lint_json, lint_policy_file
45
from cfengine_cli.shell import user_command
56
from cfengine_cli.paths import bin
67
from cfengine_cli.version import cfengine_cli_version_string
7-
from cfengine_cli.format import format_policy_file, format_json_file
8+
from cfengine_cli.format import (
9+
format_policy_file,
10+
format_json_file,
11+
format_policy_fin_fout,
12+
)
813
from cfengine_cli.utils import UserError
914
from cfbs.utils import find
1015
from cfbs.commands import build_command
@@ -42,15 +47,47 @@ def deploy() -> int:
4247
return r
4348

4449

45-
def format() -> int:
46-
for filename in find(".", extension=".json"):
47-
if filename.startswith("./."):
48-
continue
50+
def _format_filename(filename):
51+
if filename.startswith("./."):
52+
return
53+
if filename.endswith(".json"):
4954
format_json_file(filename)
50-
for policy_file in find(".", extension=".cf"):
51-
if policy_file.startswith("./."):
55+
return
56+
if filename.endswith(".cf"):
57+
format_policy_file(filename)
58+
return
59+
raise UserError(f"Unrecognized file format: {filename}")
60+
61+
62+
def _format_dirname(directory):
63+
for filename in find(directory, extension=".json"):
64+
_format_filename(filename)
65+
for filename in find(directory, extension=".cf"):
66+
_format_filename(filename)
67+
68+
69+
def format(args) -> int:
70+
if not args:
71+
_format_dirname(".")
72+
return 0
73+
if len(args) == 1 and args[0] == "-":
74+
# Special case, format policy file from stdin to stdout
75+
format_policy_fin_fout(sys.stdin, sys.stdout)
76+
return 0
77+
78+
for arg in args:
79+
if arg == "-":
80+
raise UserError(
81+
"The - argument has a special meaning and cannot be combined with other paths"
82+
)
83+
if not os.path.exists(arg):
84+
raise UserError(f"{arg} does not exist")
85+
if os.path.isfile(arg):
86+
_format_filename(arg)
87+
continue
88+
if os.path.isdir(arg):
89+
_format_dirname(arg)
5290
continue
53-
format_policy_file(policy_file)
5491
return 0
5592

5693

src/cfengine_cli/format.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,20 @@ def format_policy_file(filename):
223223
with open(filename, "w") as f:
224224
f.write(new_data)
225225
print(f"Policy file '{filename}' was reformatted")
226+
227+
228+
def format_policy_fin_fout(fin, fout):
229+
PY_LANGUAGE = Language(tscfengine.language())
230+
parser = Parser(PY_LANGUAGE)
231+
232+
macro_indent = 0
233+
fmt = Formatter()
234+
original_data = fin.read().encode("utf-8")
235+
tree = parser.parse(original_data)
236+
237+
root_node = tree.root_node
238+
assert root_node.type == "source_file"
239+
autoformat(root_node, fmt, macro_indent)
240+
241+
new_data = fmt.buffer + "\n"
242+
fout.write(new_data)

src/cfengine_cli/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def _get_arg_parser():
4141
)
4242
subp.add_parser("build", help="Build a policy set from a CFEngine Build project")
4343
subp.add_parser("deploy", help="Deploy a built policy set")
44-
subp.add_parser("format", help="Autoformat .json and .cf files")
44+
fmt = subp.add_parser("format", help="Autoformat .json and .cf files")
45+
fmt.add_argument("files", nargs="*", help="Files to format")
4546
subp.add_parser(
4647
"lint",
4748
help="Look for syntax errors and other simple mistakes",
@@ -85,7 +86,7 @@ def run_command_with_args(args) -> int:
8586
if args.command == "deploy":
8687
return commands.deploy()
8788
if args.command == "format":
88-
return commands.format()
89+
return commands.format(args.files)
8990
if args.command == "lint":
9091
return commands.lint()
9192
if args.command == "report":

0 commit comments

Comments
 (0)