Skip to content
Open
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
21 changes: 7 additions & 14 deletions src/cfengine_cli/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
update_dependency_tables as _update_dependency_tables,
print_release_dependency_tables,
)
from cfengine_cli.docs import update_docs, check_docs
from cfengine_cli.format import format_paths
from cfengine_cli import docs
from cfengine_cli.syntax_tree import syntax_tree


Expand Down Expand Up @@ -48,21 +47,15 @@ def print_dependency_tables(args) -> int:
return print_release_dependency_tables(args.versions)


def format_docs(files) -> int:
def format_docs() -> int:
_expect_repo("documentation")
ret = update_docs(files)
# Also run the same logic as `cfengine format` so .cf / .json files
# are formatted without having to run that command manually.
ret |= format_paths(files, line_length=80, check=False)
ret = docs.format_docs()
return ret


def lint_docs(files) -> int:
def lint_docs() -> int:
_expect_repo("documentation")
ret = check_docs()
# Also run the same logic as `cfengine format --check` so .cf / .json
# files are checked without having to run that command manually.
ret |= format_paths(files, line_length=80, check=True)
ret = docs.lint_docs()
return ret


Expand All @@ -80,9 +73,9 @@ def dispatch_dev_subcommand(subcommand, args) -> int:
if subcommand == "print-dependency-tables":
return print_dependency_tables(args)
if subcommand == "format-docs":
return format_docs(args.files)
return format_docs()
if subcommand == "lint-docs":
return lint_docs(args.files)
return lint_docs()
if subcommand == "syntax-tree":
return syntax_tree(args.file)
if subcommand == "generate-release-information":
Expand Down
31 changes: 19 additions & 12 deletions src/cfengine_cli/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from cfbs.pretty import pretty_file
from cfbs.utils import find

from cfengine_cli.format import format_policy_file
from cfengine_cli.format import format_policy_file, format_paths
from cfengine_cli.lint import lint_args, lint_policy_file_snippet
from cfengine_cli.utils import UserError

Expand Down Expand Up @@ -420,12 +420,11 @@ def _run_prettier(path) -> bool:
return formatted


def _update_docs_single_arg(path):
def _format_docs_single_arg(path):
if not os.path.exists(path):
raise UserError(f"The specified file/folder '{path}' does not exist")
if not os.path.isfile(path) and not os.path.isdir(path):
raise UserError(f"Argument '{path}' is not a file or a folder")

formatted = False
if os.path.isdir(path) or path.endswith(".py"):
if _run_black(path):
Expand All @@ -443,13 +442,16 @@ def _update_docs_single_arg(path):
replace=True,
cleanup=True,
)

if os.path.isdir(path) or path.endswith(".cf"):
r = format_paths([path], line_length=80, check=False)
if r != 0:
formatted = True
return formatted


def update_docs(paths) -> int:
def format_docs() -> int:
"""
Iterate through entire docs repo, or specified subfolders / files, autoformatting as much as possible:
Iterate through entire docs repo (CWD) autoformatting as much as possible:
- python code with black
- markdown files with prettier
- code blocks inside markdown files are formatted for the formats supported by prettier
Expand All @@ -458,23 +460,27 @@ def update_docs(paths) -> int:
Run by the command:
cfengine dev format-docs
"""
if not paths:
_update_docs_single_arg(".")
return 0
for path in paths:
_update_docs_single_arg(path)
_format_docs_single_arg(".")
return 0


def check_docs() -> int:
def lint_docs() -> int:
"""
Run checks / tests on docs.

Run by the command:
cfengine dev lint-docs"""

# cfengine lint
r = lint_args(["."], strict=False)
if r != 0:
print(f"Failure - {r} errors while running cfengine lint .")
return r

# cfengine format --check
r = format_paths(["."], line_length=80, check=True)

# Extract code blocks from markdown files and run checks on them:
_process_markdown_code_blocks(
path=".",
languages=["json", "cf3"],
Expand All @@ -485,4 +491,5 @@ def check_docs() -> int:
replace=False,
cleanup=True,
)
print("Success - all files look correct")
return 0
Loading