aws-cloudformation / rain

A development workflow tool for working with AWS CloudFormation.
Apache License 2.0
807 stars 72 forks source link

Allow formatting with long-format version of intrinsic functions #12

Open cicdguy opened 4 years ago

cicdguy commented 4 years ago

Hi!

First of all - this project, along with aws-cloudformation-template-formatter, is an awesome tool for formatting YAML-formatted CloudFormation templates. So thank you for creating this, as it is a huge time saver.

Second - is it possible to add the ability to format the CloudFormation templates with the long-format version of intrinsic functions?

For instance, suppose there's a template that looks like this:

AWSTemplateFormatVersion: '2010-09-09'
Description: Defines a certificate for DomainName and a SAN of *.DomainName
Parameters:
  DomainName: {Type: String}
  ValidationDomain: {Type: String}
Resources:
  Certificate:
    Type: AWS::CertificateManager::Certificate
    Properties:
      DomainName: {Ref: DomainName}
      DomainValidationOptions:
      - DomainName: {Ref: DomainName}
        ValidationDomain:  {Ref: ValidationDomain}
      - DomainName:
          Fn::Join: ['.', ['*', {Ref: DomainName}]]
        ValidationDomain:  {Ref: ValidationDomain}
      SubjectAlternativeNames:
        - Fn::Join: ['.', ['*',{Ref: DomainName}]]

Outputs:
  CertificateArn:
    Description: ARN of certificate
    Value: {Ref: Certificate}

rain fmt -c <template> results in a template that looks like this:

AWSTemplateFormatVersion: "2010-09-09"
Description: Defines a certificate for DomainName and a SAN of *.DomainName
Parameters:
  DomainName:
    Type: String
  ValidationDomain:
    Type: String
Resources:
  Certificate:
    Type: "AWS::CertificateManager::Certificate"
    Properties:
      DomainName: !Ref DomainName
      DomainValidationOptions:
        - DomainName: !Ref DomainName
          ValidationDomain: !Ref ValidationDomain
        - DomainName: !Join
            - .
            - - "*"
              - Ref: DomainName
          ValidationDomain: !Ref ValidationDomain
      SubjectAlternativeNames:
        - "Fn::Join":
            - .
            - - "*"
              - Ref: DomainName
Outputs:
  CertificateArn:
    Description: ARN of certificate
    Value: !Ref Certificate

Would it be possible to add an additional option to rain fmt so that the long-format (Ref: instead of !Ref | Fn::Join instead of !Join etc) of the intrinsic functions is retained?

stilvoid commented 4 years ago

Hi @dinakar29. Apologies for the long wait. Can you explain your use case for this feature?

cicdguy commented 4 years ago

Hi @stilvoid, no worries, and thanks for getting back to me.

My use case is that I have several (100s) of legacy CloudFormation templates that utilize the long-format version of the intrinsic functions with varying formatting styles such as compact YAML vs non-compact YAML. I wanted to have all templates conform to a standard formatting style so there isn’t any drift, and that’s where rain would be very beneficial where it could potentially run as a pre-commit hook.

stilvoid commented 4 years ago

I'm currently working on overhauling the parsing and formatting of YAML in rain so I haven't forgotten this ticket but it might take a while.

What's the reason to prefer long format over short? Once I'm done with this refactor, it will be easy enough to add a switch to provide it but I'm curious :)

cicdguy commented 4 years ago

No worries. There is no real reason to prefer the long format over short - just that I have inherited a legacy codebase with 100s of templates which utilize the long format and wanted to minimize the risk of disrupting existing stacks that utilize those templates. I've definitely resorted to using the short form for all new templates going forward.

ericzbeard commented 2 years ago

Hi @cicdguy, sorry for the long delay here, I've been added as a maintainer and I'd like to check with you to see if this is something you still need.

cicdguy commented 2 years ago

Hi there, I personally no longer need this but I'm sure someone else might benefit from it. Totally up to you on how you want to proceed with this request.

pheck commented 1 year ago

Hi, just want to check if it's worth creating another issue: aws-cloudformation-template-formatter has a -c option to generate compact templates without adding extra blank lines.

Seeing the message from @cicdguy, it looks like a 2019 release of rain still had that flag as well:

rain fmt -c <template> results in a template that looks like this:

Any chance to re-introduce the compact mode in rain again - if so, let me know and I can raise a separate issue for that.

ericzbeard commented 1 year ago

Go ahead and create a separate issue.

pheck commented 1 year ago

@ericzbeard Thanks, here it is: #105