Skip to content

Commit

Permalink
Update yamlfix action version and add validate-only option
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmello committed Dec 23, 2023
1 parent 97036fc commit ea31e6e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
20 changes: 20 additions & 0 deletions src/yamlfix/entrypoints/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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]],
Expand Down Expand Up @@ -109,17 +115,31 @@ 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()
configure_yamlfix(
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="")

Expand Down
3 changes: 3 additions & 0 deletions src/yamlfix/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ class YamlfixConfig(ConfigSchema):
preserve_quotes: bool = False
quote_representation: str = "'"
sequence_style: YamlNodeStyle = YamlNodeStyle.FLOW_STYLE


class FailedToFix(Exception): ...
7 changes: 5 additions & 2 deletions src/yamlfix/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ea31e6e

Please sign in to comment.