From 0d1a6f8c82a3c048eb967c8059ad58d317bcd24f Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Wed, 16 Feb 2022 10:56:21 +0100 Subject: [PATCH] Fix some indentation behavio + add indent flag --- generator/writers/configmap.go | 2 +- generator/writers/deployment.go | 13 ++++++++----- generator/writers/ingress.go | 2 +- generator/writers/service.go | 2 +- generator/writers/storage.go | 2 +- generator/writers/utils.go | 5 ++++- main.go | 13 +++++++++++-- 7 files changed, 27 insertions(+), 12 deletions(-) diff --git a/generator/writers/configmap.go b/generator/writers/configmap.go index d3ec879..d17a889 100644 --- a/generator/writers/configmap.go +++ b/generator/writers/configmap.go @@ -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() } diff --git a/generator/writers/deployment.go b/generator/writers/deployment.go index 1cbe810..ca09249 100644 --- a/generator/writers/deployment.go +++ b/generator/writers/deployment.go @@ -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") } diff --git a/generator/writers/ingress.go b/generator/writers/ingress.go index 311e80d..b11a552 100644 --- a/generator/writers/ingress.go +++ b/generator/writers/ingress.go @@ -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 -}}") diff --git a/generator/writers/service.go b/generator/writers/service.go index 2dae7d8..2ba9b82 100644 --- a/generator/writers/service.go +++ b/generator/writers/service.go @@ -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() } diff --git a/generator/writers/storage.go b/generator/writers/storage.go index 69343ab..2075068 100644 --- a/generator/writers/storage.go +++ b/generator/writers/storage.go @@ -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 -}}") } diff --git a/generator/writers/utils.go b/generator/writers/utils.go index acf333f..b057945 100644 --- a/generator/writers/utils.go +++ b/generator/writers/utils.go @@ -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 { diff --git a/main.go b/main.go index ba128ac..5dbf58a 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,9 @@ package main import ( "katenary/cmd" + "katenary/generator/writers" "katenary/helm" + "strconv" "github.com/spf13/cobra" ) @@ -50,16 +52,21 @@ func main() { Short: "Convert docker-compose to helm chart", Long: "Convert docker-compose to helm chart. The resulting helm chart will be in the current directory/" + cmd.ChartsDir + "/" + cmd.AppName + - ".\nThe appversion will be produced that waty:\n" + - "- from git version or tag\n" + + ".\nThe appversion will be generated that way:\n" + + "- if it's in a git project, it takes git version or tag\n" + "- if it's not defined, so the version will be get from the --apVersion flag \n" + "- if it's not defined, so the 0.0.1 version is used", Run: func(c *cobra.Command, args []string) { force := c.Flag("force").Changed + // TODO: is there a way to get typed values from cobra? appversion := c.Flag("app-version").Value.String() composeFile := c.Flag("compose-file").Value.String() appName := c.Flag("app-name").Value.String() chartDir := c.Flag("output-dir").Value.String() + indentation, err := strconv.Atoi(c.Flag("indent-size").Value.String()) + if err != nil { + writers.IndentSize = indentation + } cmd.Convert(composeFile, appversion, appName, chartDir, force) }, } @@ -73,6 +80,8 @@ func main() { "app-name", "n", cmd.AppName, "Application name") convertCmd.Flags().StringP( "output-dir", "o", cmd.ChartsDir, "Chart directory") + convertCmd.Flags().IntP( + "indent-size", "i", 2, "Set the indent size of the YAML files") // show possible labels to set in docker-compose file showLabelsCmd := &cobra.Command{