Skip to content

Commit

Permalink
Merge pull request #114 from linkorb/prettify-yaml-output-9235
Browse files Browse the repository at this point in the history
fix: preserve multiline strings in YAML #9235
  • Loading branch information
mhitza authored Dec 27, 2024
2 parents 9d5891c + 65ab867 commit 4131be0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
25 changes: 25 additions & 0 deletions library/yaml_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python3

import yaml
import sys

# https://stackoverflow.com/questions/45004464/yaml-dump-adding-unwanted-newlines-in-multiline-strings
yaml.SafeDumper.org_represent_str = yaml.SafeDumper.represent_str

# Represent strings as multiline within YAML file, if the string contains the newline character
def repr_str(dumper, data):
if '\n' in data:
return dumper.represent_scalar(u'tag:yaml.org,2002:str', data, style='|')
return dumper.org_represent_str(data)


yaml.add_representer(str, repr_str, Dumper=yaml.SafeDumper)


file_path = sys.argv[1]
with open(file_path, 'r') as file:
document = file.read()

structure = yaml.safe_load(document)
with open(file_path, 'w') as file:
yaml.safe_dump(structure, file)
2 changes: 1 addition & 1 deletion tasks/migrations/run-step.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- name: "{{ migration_to_version }} applying migration"
ansible.builtin.include_tasks: "tasks/migrations/{{ migration_filename }}"

- ansible.builtin.include_tasks: "tasks/migrations/update-repo-yaml-version.yaml"
- ansible.builtin.include_tasks: tasks/migrations/update-version.yaml
vars:
to_version: "{{ migration_to_version }}"
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
- name: "updating version in repo.yaml"
copy:
dest: "{{ repo_path }}/repo.yaml"
content: "{{ repo_yaml | combine({'version': to_version}) | to_nice_yaml(sort_keys=False, indent=2) }}"
content: "{{ repo_yaml | combine({'version': to_version}) | to_nice_yaml }}"

- name: reformat repo.yaml file
ansible.builtin.command: "./library/yaml_format.py '{{ repo_path }}/repo.yaml'"
2 changes: 1 addition & 1 deletion tasks/version-migrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@
# XXX renamed to avoid warnings if included tasks include loops of their own
loop_var: item_migration

- ansible.builtin.include_tasks: "tasks/migrations/update-repo-yaml-version.yaml"
- ansible.builtin.include_tasks: "tasks/migrations/update-version.yaml"
vars:
to_version: "{{ target_repo_ansible_version }}"

0 comments on commit 4131be0

Please sign in to comment.