Skip to content

Commit

Permalink
Serverless Tags - adjust indentation calculation. (#452)
Browse files Browse the repository at this point in the history
* adjust indentation

* adjust indentation just for sls (noy cfn)
  • Loading branch information
ChanochShayner committed Dec 14, 2023
1 parent 3e30ff3 commit 2dac6f8
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/common/yaml/yaml_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,16 @@ func WriteYAMLFile(readFilePath string, blocks []structure.IBlock, writeFilePath
if isCfn {
tagIndent += SingleIndent
}
resourcesLines = append(resourcesLines, IndentLines(newResourceLines[newResourceTagLineRange.Start+1:newResourceTagLineRange.End+1], tagIndent)...)
resourcesLines = append(resourcesLines, IndentLines(newResourceLines[newResourceTagLineRange.Start+1:newResourceTagLineRange.End+1], tagIndent, 0)...)
resourcesLines = append(resourcesLines, oldResourceLines[lastIndex+1:]...)
continue
}

oldTagsIndent := ExtractIndentationOfLine(oldResourceLines[oldResourceTagLines.Start-oldResourceLinesRange.Start])
oldTagsValueIndent := len(ExtractIndentationOfLine(oldResourceLines[oldResourceTagLines.Start-oldResourceLinesRange.Start+1])) - len(oldTagsIndent)
if isCfn {
oldTagsValueIndent = 0
}
if isCfn {
oldTagsIndent += SingleIndent
}
Expand All @@ -87,7 +91,7 @@ func WriteYAMLFile(readFilePath string, blocks []structure.IBlock, writeFilePath
} else {
UpdateExistingSLSTags(tagLines, diff.Updated)
}
allNewResourceTagLines := IndentLines(newResourceLines[newResourceTagLineRange.Start+1:newResourceTagLineRange.End+1], oldTagsIndent)
allNewResourceTagLines := IndentLines(newResourceLines[newResourceTagLineRange.Start+1:newResourceTagLineRange.End+1], oldTagsIndent, oldTagsValueIndent)
var netNewResourceLines []string
for i := 0; i < len(allNewResourceTagLines); i += linesPerTag {
l := allNewResourceTagLines[i]
Expand Down Expand Up @@ -337,13 +341,19 @@ func findLastNonEmptyLine(fileLines []string, maxIndex int) int {
return 0
}

func IndentLines(textLines []string, indent string) []string {
func IndentLines(textLines []string, indent string, valueIndent int) []string {
for i, originLine := range textLines {
var blankSpaces string
if valueIndent == 0 {
blankSpaces = SingleIndent
} else {
blankSpaces = strings.Repeat(" ", valueIndent)
}
noLeadingWhitespace := strings.TrimLeft(originLine, "\t \n")
if strings.Contains(originLine, "- Key") {
textLines[i] = indent + noLeadingWhitespace
} else {
textLines[i] = indent + SingleIndent + noLeadingWhitespace
textLines[i] = indent + blankSpaces + noLeadingWhitespace
}
}

Expand Down

0 comments on commit 2dac6f8

Please sign in to comment.