Skip to content

Commit

Permalink
Merge pull request #103 from ericzbeard/bugfix1-3
Browse files Browse the repository at this point in the history
Bugfixes for 1.3 release
  • Loading branch information
ericzbeard authored Feb 14, 2023
2 parents 76084b6 + 89c7e3f commit 9291592
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ go.work
main
rain
local/
.idea
2 changes: 1 addition & 1 deletion cft/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ waldo: {}
}

expected := `# Comment about the doc
foo: bar # Comment about foo:bar
baz: # Comment about baz
quux: mooz # Comment about quux:mooz
xyzzy: # Comment about xyzzy
- corge # Comment about corge
# Comment about grault
- grault: garply # Comment about grault:garply
Expand Down
27 changes: 25 additions & 2 deletions cft/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,42 @@ func String(t cft.Template, opt Options) string {

lastIndent := 0
indent := 0
lastPartWasComment := false
lastLineWasEmpty := false

for _, part := range parts {
indent = len(part) - len(strings.TrimLeft(part, " "))

// Add spaces between 1st and 2nd level properties
trimmedPart := strings.TrimLeft(part, " ")
indent = len(part) - len(trimmedPart)

isComment := false
if len(part) > 0 && strings.HasPrefix(trimmedPart, "#") {
isComment = true
}

isEmpty := len(part) == 0 // This should never be true

// Add spaces between 1st and 2nd level properties, except for comments
if indent <= lastIndent && (indent == 0 || indent == 2) {
if !lastPartWasComment {
// If the last line was a comment, don't newline here,
// since we want the comment to stick to the thing it was above
result.WriteString("\n")
lastLineWasEmpty = true
}
}

// Put a space above first/only comment lines
if !lastPartWasComment && isComment && !lastLineWasEmpty {
result.WriteString("\n")
}

result.WriteString(part)
result.WriteString("\n")

lastIndent = indent
lastPartWasComment = isComment
lastLineWasEmpty = isEmpty
}

out := strings.TrimSpace(result.String())
Expand Down
6 changes: 6 additions & 0 deletions cft/format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Outputs:
- Bucket2
- Arn # Arn comment
# Multiline comment
# starting at indent 0
Resources:
Bucket2:
Properties:
Expand All @@ -36,6 +38,8 @@ const expectedYaml = `Parameters:
Name:
Type: String
# Multiline comment
# starting at indent 0
Resources:
Bucket2:
Type: AWS::S3::Bucket
Expand All @@ -62,6 +66,8 @@ const expectedYamlUnsorted = `Outputs:
Bucket2: # Bucket comment
Value: !GetAtt Bucket2.Arn # GetAtt comment Arn comment
# Multiline comment
# starting at indent 0
Resources:
Bucket2:
Properties:
Expand Down
4 changes: 4 additions & 0 deletions internal/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func loadConfig(ctx context.Context, sessionName string) *aws.Config {
panic(errors.New("could not establish AWS credentials; please run 'aws configure' or choose a profile"))
}

if cfg.Region == "" {
panic(errors.New("a region was not specified. You can run 'aws configure' or choose a profile with a region"))
}

return &cfg
}

Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/fmt/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/aws-cloudformation/rain/cft/format"
"github.com/aws-cloudformation/rain/internal/config"
"github.com/aws-cloudformation/rain/internal/console"
"github.com/aws-cloudformation/rain/internal/ui"

Expand Down Expand Up @@ -150,4 +151,5 @@ func init() {
Cmd.Flags().BoolVarP(&verifyFlag, "verify", "v", false, "Check if the input is already correctly formatted and exit.\nThe exit status will be 0 if so and 1 if not.")
Cmd.Flags().BoolVarP(&writeFlag, "write", "w", false, "Write the output back to the file rather than to stdout.")
Cmd.Flags().BoolVarP(&unsortedFlag, "unsorted", "u", false, "Do not sort the template's properties.")
Cmd.Flags().BoolVar(&config.Debug, "debug", false, "Output debugging information")
}
29 changes: 29 additions & 0 deletions test/templates/comments.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Comment on the first line
Parameters:
MyParam:
Type: String

# Multi-line comment between sections before Resources
# Starting at indent 0

Resources:

# Comment by itself before MyBucket

MyBucket:
Type: AWS::S3::Bucket # Comment at the end of line

# Multi-line comment between resources
# Starting at indent 0
# Between MyBucket and MyOtherBucket

# Comment at the beginning of MyOtherBucket
MyOtherBucket:
# Comment in the middle of a resource
Type: AWS::S3::Bucket
# Multi line
# comment
Properties:
BucketName: asdf

# Comment at the end
24 changes: 24 additions & 0 deletions test/templates/format-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Outputs:
Bucket1:
Value: !GetAtt Bucket1.Arn # Short GetAtt
Bucket2: # Bucket comment
Value:
Fn::GetAtt: # GetAtt comment
- Bucket2
- Arn # Arn comment

# Multiline comment
# starting at indent 0
Resources:
Bucket2:
Properties:
BucketName: !Ref Name # Ref: comment
Type: "AWS::S3::Bucket"
Bucket1:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub ${Bucket2}-newer
Parameters:
Name:
Type: String

14 changes: 14 additions & 0 deletions test/templates/null.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Conditions:
NeverValid: !Equals [True, False]

# Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
# tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
# quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
# consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
# cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
# proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Resources:
NullResource:
Type: 'Custom::NullResource'
Condition: NeverValid

0 comments on commit 9291592

Please sign in to comment.