diff --git a/README.md b/README.md index 5d93d95..086e729 100644 --- a/README.md +++ b/README.md @@ -56,9 +56,13 @@ jobs: steps: - uses: actions/checkout@v2 # - uses: lyz-code/yamlfix@v1 - - uses: dsmello/yamlfix@v1.0.2-action + - uses: dsmello/yamlfix@v1.0.3 + with: + args: --validate-only ``` +If you didn't specify any arguments, the action will run with the default, and will return a error if the yaml is not valid or not formatted. + ## License diff --git a/action.yml b/action.yml index 60f3a65..2144549 100644 --- a/action.yml +++ b/action.yml @@ -8,14 +8,20 @@ branding: icon: "check-circle" inputs: - args: + file_path: description: "The file or directory to check" required: false default: "." + args: + description: "Arguments to pass to the command" + required: false + default: "" + runs: using: "docker" image: "Dockerfile" args: - ${{ inputs.args }} + - ${{ inputs.file_path }} diff --git a/src/yamlfix/entrypoints/cli.py b/src/yamlfix/entrypoints/cli.py index 9d68252..2274ebe 100644 --- a/src/yamlfix/entrypoints/cli.py +++ b/src/yamlfix/entrypoints/cli.py @@ -43,6 +43,11 @@ def _find_all_yaml_files( is_flag=True, help="Check if file(s) needs fixing. No files will be written in this case.", ) +@click.option( + "--validate-only", + is_flag=True, + help="Validate the yaml(s) without fixing or formatting. Will enforce --check.", +) @click.option( "--config-file", "-c", @@ -79,6 +84,7 @@ def cli( # pylint: disable=too-many-arguments files: Tuple[str], verbose: bool, check: bool, + validate_only: bool, config_file: Optional[List[str]], include: Optional[List[str]], exclude: Optional[List[str]], @@ -109,6 +115,10 @@ def cli( # pylint: disable=too-many-arguments sys.exit(0) load_logger(verbose) + + if validate_only: + check = True + log.info("YamlFix: %s files", "Checking" if check else "Fixing") config = YamlfixConfig() @@ -116,10 +126,20 @@ def cli( # pylint: disable=too-many-arguments config, config_file, _parse_env_vars_as_yamlfix_config(env_prefix.lower()) ) + + + fixed_code, changed = services.fix_files(files_to_fix, check, config) + for file_to_close in files_to_fix: file_to_close.close() + if fixed_code == "error": + sys.exit(1) + + if validate_only: + sys.exit(0) + if fixed_code is not None: print(fixed_code, end="") diff --git a/src/yamlfix/model.py b/src/yamlfix/model.py index f8c1f87..09fa078 100644 --- a/src/yamlfix/model.py +++ b/src/yamlfix/model.py @@ -34,3 +34,6 @@ class YamlfixConfig(ConfigSchema): preserve_quotes: bool = False quote_representation: str = "'" sequence_style: YamlNodeStyle = YamlNodeStyle.FLOW_STYLE + + +class FailedToFix(Exception): ... \ No newline at end of file diff --git a/src/yamlfix/services.py b/src/yamlfix/services.py index 51e8b97..94150e6 100644 --- a/src/yamlfix/services.py +++ b/src/yamlfix/services.py @@ -11,7 +11,7 @@ from _io import TextIOWrapper from yamlfix.adapters import SourceCodeFixer, Yaml -from yamlfix.model import YamlfixConfig +from yamlfix.model import YamlfixConfig, FailedToFix from ruyaml.scanner import ScannerError @@ -134,7 +134,10 @@ def fix_files( # pylint: disable=too-many-branches len(files) - total_fixed, ) - if dry_run is None and not total_failed: + if total_failed: + return "error", changed + + if dry_run is None: return None return None, changed