Fix some indentation behavio + add indent flag

This commit is contained in:
2022-02-16 10:56:21 +01:00
parent a4834a0661
commit 0d1a6f8c82
7 changed files with 27 additions and 12 deletions

View File

@@ -11,7 +11,7 @@ func BuildConfigMap(c interface{}, kind, servicename, name, templatesDir string)
fname := filepath.Join(templatesDir, servicename+"."+name+"."+kind+".yaml")
fp, _ := os.Create(fname)
enc := yaml.NewEncoder(fp)
enc.SetIndent(2)
enc.SetIndent(IndentSize)
enc.Encode(c)
fp.Close()
}

View File

@@ -16,22 +16,25 @@ func BuildDeployment(deployment *helm.Deployment, name, templatesDir string) {
fp, _ := os.Create(fname)
buffer := bytes.NewBuffer(nil)
enc := yaml.NewEncoder(buffer)
enc.SetIndent(2)
enc.SetIndent(IndentSize)
enc.Encode(deployment)
_content := string(buffer.Bytes())
content := strings.Split(string(_content), "\n")
dataname := ""
component := deployment.Spec.Selector["matchLabels"].(map[string]string)[helm.K+"/component"]
n := 0 // will be count of lines only on "persistentVolumeClaim" line, to indent "else" and "end" at the right place
for _, line := range content {
if strings.Contains(line, "name:") {
dataname = strings.Split(line, ":")[1]
dataname = strings.TrimSpace(dataname)
} else if strings.Contains(line, "persistentVolumeClaim") {
line = " {{- if .Values." + component + ".persistence." + dataname + ".enabled }}\n" + line
n = CountSpaces(line)
line = strings.Repeat(" ", n) + "{{- if .Values." + component + ".persistence." + dataname + ".enabled }}\n" + line
} else if strings.Contains(line, "claimName") {
line += "\n {{ else }}"
line += "\n emptyDir: {}"
line += "\n {{- end }}"
spaces := strings.Repeat(" ", n)
line += "\n" + spaces + "{{ else }}"
line += "\n" + spaces + "emptyDir: {}"
line += "\n" + spaces + "{{- end }}"
}
fp.WriteString(line + "\n")
}

View File

@@ -27,7 +27,7 @@ func BuildIngress(ingress *helm.Ingress, name, templatesDir string) {
buffer := bytes.NewBuffer(nil)
fname := filepath.Join(templatesDir, name+"."+kind+".yaml")
enc := yaml.NewEncoder(buffer)
enc.SetIndent(2)
enc.SetIndent(IndentSize)
buffer.WriteString("{{- if .Values." + name + ".ingress.enabled -}}\n")
enc.Encode(ingress)
buffer.WriteString("{{- end -}}")

View File

@@ -17,7 +17,7 @@ func BuildService(service *helm.Service, name, templatesDir string) {
fname := filepath.Join(templatesDir, name+suffix+"."+kind+".yaml")
fp, _ := os.Create(fname)
enc := yaml.NewEncoder(fp)
enc.SetIndent(2)
enc.SetIndent(IndentSize)
enc.Encode(service)
fp.Close()
}

View File

@@ -15,7 +15,7 @@ func BuildStorage(storage *helm.Storage, name, templatesDir string) {
volname := storage.K8sBase.Metadata.Labels[helm.K+"/pvc-name"]
fp.WriteString("{{ if .Values." + name + ".persistence." + volname + ".enabled }}\n")
enc := yaml.NewEncoder(fp)
enc.SetIndent(2)
enc.SetIndent(IndentSize)
enc.Encode(storage)
fp.WriteString("{{- end -}}")
}

View File

@@ -1,6 +1,9 @@
package writers
// CountSpaces returns the number of spaces from the begining of the line
// IndentSize set the indentation size for yaml output.
var IndentSize = 2
// CountSpaces returns the number of spaces from the begining of the line.
func CountSpaces(line string) int {
var spaces int
for _, char := range line {