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
10 changes: 5 additions & 5 deletions src/cfengine_cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ def format(names, line_length, check) -> int:
return format_paths(names, line_length, check)


def _lint(files, strict) -> int:
def _lint(files, strict, syntax_path) -> int:
if not files:
return lint_args(["."], strict)
return lint_args(files, strict)
return lint_args(["."], strict, syntax_path)
return lint_args(files, strict, syntax_path)


def lint(files, strict) -> int:
errors = _lint(files, strict)
def lint(files, strict, syntax_path) -> int:
errors = _lint(files, strict, syntax_path)
if errors == 0:
print("Success, no errors found.")
elif errors == 1:
Expand Down
16 changes: 11 additions & 5 deletions src/cfengine_cli/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ class SyntaxData:
BUILTIN_PROMISE_TYPES = {}
BUILTIN_FUNCTIONS = {}

def __init__(self):
def __init__(self, syntax_path=None):
"""Load the bundled syntax-description.json and derive lookup dicts."""
self._data_dict = self._load_syntax_description()
self._data_dict = self._load_syntax_description(path=syntax_path)
self._derive_syntax_dicts(self._data_dict)

assert self.BUILTIN_BODY_TYPES
Expand Down Expand Up @@ -1100,6 +1100,7 @@ def _lint_main(
state=None,
snippet: Snippet | None = None,
syntax_data=None,
syntax_path: str | None = None,
) -> int:
"""This is the main function used for linting, it does all the steps on all
the arguments (files / folders).
Expand Down Expand Up @@ -1127,7 +1128,10 @@ def _lint_main(
state.mode = Mode.SYNTAX

if syntax_data is None:
syntax_data = SyntaxData()
try:
syntax_data = SyntaxData(syntax_path)
except FileNotFoundError:
raise UserError(f"'{syntax_path}' does not exist")

filenames = _args_to_filenames(args)

Expand Down Expand Up @@ -1379,9 +1383,11 @@ def lint_single_file(file: str, strict: bool = True) -> int:
return _lint_main([file], strict)


def lint_args(args: Iterable[str], strict: bool = True) -> int:
def lint_args(
args: Iterable[str], strict: bool = True, syntax_path: str | None = None
) -> int:
"""Lint a list of args (files / folders)"""
return _lint_main(list(args), strict)
return _lint_main(list(args), strict, syntax_path=syntax_path)


def lint_policy_file_snippet(
Expand Down
11 changes: 10 additions & 1 deletion src/cfengine_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ def _get_arg_parser():
default="yes",
help="Strict mode. Default=yes, checks for undefined promise types, bundles, bodies, functions",
)
lnt.add_argument(
"--syntax-description",
type=str,
help="Lint based on a user given syntax description",
)
lnt.add_argument("files", nargs="*", help="Files to lint")
subp.add_parser(
"report",
Expand Down Expand Up @@ -188,7 +193,11 @@ def run_command_with_args(args) -> int:
if args.command == "format":
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")))
return commands.lint(
args.files,
(args.strict.lower() in ("y", "ye", "yes")),
args.syntax_description,
)
if args.command == "report":
return commands.report()
if args.command == "run":
Expand Down
Loading