-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from linkorb/prettify-yaml-output-9235
fix: preserve multiline strings in YAML #9235
- Loading branch information
Showing
4 changed files
with
31 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}" |
5 changes: 4 additions & 1 deletion
5
.../migrations/update-repo-yaml-version.yaml → tasks/migrations/update-version.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters