How do you generate a YAML file with comments?
See original GitHub issueWe have a tool that generates YAML config files for CI pipelines.
The files contain informative code comments. Here’s a representative example (with a bunch of fields deleted for brevity):
# DO NOT EDIT THIS FILE!!!
# To modify this file, make changes to the associated config files
# and then invoke the "generate-pipelines" tool.
variables:
- name: NodeVersion
value: 12
jobs:
- job: PRBuild
condition: succeeded()
steps:
- checkout: self
fetchDepth: 1
lfs: true
- script: 'node common/scripts/install-run-rush.js install'
displayName: 'Install Rush'
- script: 'node common/scripts/install-run-rush.js rebuild'
displayName: 'AWS Pipelines Check'
# Generated from 'apps/example-app/infra/production/aws-stacks.json'
- task: CloudFormationCreateOrUpdateStack@1
inputs:
templateFile: 'common/config/aws/templates/bucket.cf.json'
# Below parameter(s) generated from 1 file(s)
# apps/example-app/infra/production/bucket-provision.cf-params.json
templateParameters: |
-
ParameterKey: 'BucketName'
ParameterValue: 'example-app.example.com'
-
ParameterKey: 'CostOwner'
ParameterValue: 'example-app'
-
ParameterKey: 'CostService'
ParameterValue: 'example-app.example.com'
capabilityAutoExpand: true
onFailure: 'DELETE'
# Generated from 'apps/example-app/infra/production/aws-stacks.json'
- task: CloudFormationCreateOrUpdateStack@1
inputs:
awsCredentials: 'AWS Production'
regionName: 'us-east-1'
As you can see, the comments aren’t merely pasted at the top of the file, they need to appear inline in the file as well.
We started using a simple string template, but it is tricky to escape parameters correctly. The js-yaml project seems like a better solution.
But does the yaml.dump()
API provide some way to inject comment strings?
Comments are a core feature of YAML, so it seems that there should be some way to do that. Thanks!
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:8 (2 by maintainers)
Top Results From Across the Web
YAML - Comments - Tutorialspoint
The shortcut key combination for commenting YAML blocks is Ctrl+Q. ... Select the block. Use “CTRL + /” on Linux and Windows and...
Read more >Save/dump a YAML file with comments in PyYAML
To build a yaml file with comments, you have to create an event stream that includes comment events. Comments are currently only allowed...
Read more >How to write comments in YAML - Educative.io
Comments in YAML are denoted by using the hash symbol # at the beginning of the ... The use of effective comments in...
Read more >Create and save YAML files with comments · GitHub
Create and save YAML files with comments. GitHub Gist: instantly share code, notes, and snippets.
Read more >YAML - Inline and Block level comments with examples
It is called single-line comments. Each comment line starts with a hashtag(#) symbol and blank space followed by a description. Each line will...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I am an author of a YAML library and I know a list of YAML libraries. Very few support creating comments. It’s hard, I concur with @puzrin . You might try out this newer JS library: https://github.com/eemeli/yaml It was written using the (relatively) new YAML Test Suite, and supports roundtripping comments.
I implemented the following utility which allows using yaml-js to generate output with comments in it:
Example:
Relates to #680