diff --git a/.copier-answers.yml b/.copier-answers.yml index 4ce9d2e..d0b81f2 100644 --- a/.copier-answers.yml +++ b/.copier-answers.yml @@ -1,6 +1,6 @@ # Autogenerated. Do not edit this by hand, use `copier update`. --- -_commit: 0.4.4 +_commit: 0.4.5 _src_path: https://github.com/lkubb/salt-extension-copier author: EITR Technologies, LLC author_email: devops@eitr.tech @@ -16,7 +16,7 @@ loaders: - pillar - sdb - state -max_salt_version: 3007 +max_salt_version: '3007' no_saltext_namespace: false package_name: consul project_name: consul diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5a8536d..66ce305 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,7 +35,7 @@ repos: language: python pass_filenames: false - - repo: https://github.com/s0undt3ch/salt-rewrite + - repo: https://github.com/saltstack/salt-rewrite # Automatically rewrite code with known rules rev: 2.5.2 hooks: @@ -45,7 +45,7 @@ repos: files: ^src/saltext/consul/.*\.py$ args: [--silent] - - repo: https://github.com/s0undt3ch/salt-rewrite + - repo: https://github.com/saltstack/salt-rewrite # Automatically rewrite code with known rules rev: 2.5.2 hooks: diff --git a/tools/helpers/copier.py b/tools/helpers/copier.py index 91cf291..87ceba6 100644 --- a/tools/helpers/copier.py +++ b/tools/helpers/copier.py @@ -1,3 +1,4 @@ +import os import sys from functools import wraps from pathlib import Path @@ -13,7 +14,33 @@ yaml = None -COPIER_ANSWERS = Path(".copier-answers.yml").resolve() +if os.environ.get("STAGE"): + # If we're running inside a Copier task/migration, cwd is the target dir. + # We cannot use __file__ because this file is imported from the template clone. + COPIER_ANSWERS = Path(".copier-answers.yml").resolve() +else: + COPIER_ANSWERS = (Path(__file__).parent.parent.parent / ".copier-answers.yml").resolve() + + +if yaml is not None: + + def represent_str(dumper, data): + """ + Represent multiline strings using "|" + """ + if len(data.splitlines()) > 1: + return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|") + return dumper.represent_scalar("tag:yaml.org,2002:str", data) + + class OpinionatedYamlDumper(yaml.SafeDumper): + """ + Indent lists by two spaces + """ + + def increase_indent(self, flow=False, indentless=False): + return super().increase_indent(flow=flow, indentless=False) + + OpinionatedYamlDumper.add_representer(str, represent_str) def _needs_answers(func): @@ -33,10 +60,29 @@ def load_answers(): """ if not yaml: raise RuntimeError("Missing pyyaml in environment") - with open(COPIER_ANSWERS) as f: + with open(COPIER_ANSWERS, encoding="utf-8") as f: return yaml.safe_load(f) +@_needs_answers +def dump_answers(answers): + """ + Write the complete answers file. Depends on PyYAML. + Intended for answers migrations. + """ + if not yaml: + raise RuntimeError("Missing pyyaml in environment") + with open(COPIER_ANSWERS, "w", encoding="utf-8") as f: + yaml.dump( + answers, + f, + Dumper=OpinionatedYamlDumper, + indent=0, + default_flow_style=False, + canonical=False, + ) + + @_needs_answers def discover_project_name(): """ diff --git a/tools/helpers/pre_commit.py b/tools/helpers/pre_commit.py index bcab18a..95cb474 100644 --- a/tools/helpers/pre_commit.py +++ b/tools/helpers/pre_commit.py @@ -102,6 +102,14 @@ def _run_pre_commit_loop(retries_left): for i, failing_hook in enumerate(failing): prompt.warn(f"✗ Failing hook ({i + 1}): {failing_hook}", failing[failing_hook]) finally: - # Undo git add --intent-to-add to allow RenovateBot to detect new files correctly - git("restore", "--staged", *new_files) + if new_files: + try: + # Check if there is at least one commit in the repo, + # otherwise git restore --staged fails. + git("rev-parse", "HEAD") + except ProcessExecutionError: + pass + else: + # Undo git add --intent-to-add to allow RenovateBot to detect new files correctly + git("restore", "--staged", *new_files) return False