Skip to content

Commit

Permalink
add handling for 'else' control structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Artyom Suharev committed Apr 30, 2024
1 parent e0af862 commit 77a4e08
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions internal/app/yamltpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,10 @@ import (
)

var controlStructureStart = regexp.MustCompile(`^{{-?\s*(if|range|with|define)\s`)
var controlStructureContinue = regexp.MustCompile(`^{{-?\s*(else)\s`)
var controlStructureEnd = regexp.MustCompile(`^{{-?\s*end\s*-?}}`)
var nonControlStructure = regexp.MustCompile(`^{{-?\s*(include|toYaml|nindent)\s`)

func formatLine(line string, indentLevel int) string {
// Remove leading spaces to reset indentation
trimmedLine := strings.TrimLeft(line, " ")
return strings.Repeat(" ", indentLevel) + trimmedLine
}

func isStartControlStructure(line string) bool {
lineWithoutLeadingSpaces := strings.TrimSpace(line)
return controlStructureStart.MatchString(lineWithoutLeadingSpaces)
}

func isEndControlStructure(line string) bool {
lineWithoutLeadingSpaces := strings.TrimSpace(line)
return controlStructureEnd.MatchString(lineWithoutLeadingSpaces)
}

func isNonControlStructure(line string) bool {
lineWithoutLeadingSpaces := strings.TrimSpace(line)
return nonControlStructure.MatchString(lineWithoutLeadingSpaces)
}

// FormatYamlTpl formats a yaml template string
func FormatYamlTpl(yamlTpl string) (string, error) {
lines := strings.Split(yamlTpl, "\n")
Expand All @@ -44,6 +24,8 @@ func FormatYamlTpl(yamlTpl string) (string, error) {
if isStartControlStructure(trimmed) {
formattedLines = append(formattedLines, formatLine(line, indentLevel))
indentLevel++
} else if isContinueControlStructure(trimmed) {
formattedLines = append(formattedLines, formatLine(line, indentLevel-1))
} else if isNonControlStructure(trimmed) {
// Non-control structures and empty lines are indented according to their current block level
formattedLines = append(formattedLines, formatLine(line, indentLevel))
Expand Down Expand Up @@ -104,3 +86,29 @@ func FormatYamlTplFile(file string, format, output bool) (bool, error) {

return true, nil
}

func formatLine(line string, indentLevel int) string {
// Remove leading spaces to reset indentation
trimmedLine := strings.TrimLeft(line, " ")
return strings.Repeat(" ", indentLevel) + trimmedLine
}

func isStartControlStructure(line string) bool {
lineWithoutLeadingSpaces := strings.TrimSpace(line)
return controlStructureStart.MatchString(lineWithoutLeadingSpaces)
}

func isContinueControlStructure(line string) bool {
lineWithoutLeadingSpaces := strings.TrimSpace(line)
return controlStructureContinue.MatchString(lineWithoutLeadingSpaces)
}

func isEndControlStructure(line string) bool {
lineWithoutLeadingSpaces := strings.TrimSpace(line)
return controlStructureEnd.MatchString(lineWithoutLeadingSpaces)
}

func isNonControlStructure(line string) bool {
lineWithoutLeadingSpaces := strings.TrimSpace(line)
return nonControlStructure.MatchString(lineWithoutLeadingSpaces)
}

0 comments on commit 77a4e08

Please sign in to comment.