From a292170a63f8f93821362600f166aaaa0ae70d07 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Thu, 2 Dec 2021 16:07:15 +0100 Subject: [PATCH] Fix names + color activation --- generator/main.go | 11 +++++++++-- generator/utils.go | 10 ++++++++++ helm/types.go | 10 +++++++++- main.go | 14 ++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/generator/main.go b/generator/main.go index 1ed39e2..86bef4e 100644 --- a/generator/main.go +++ b/generator/main.go @@ -103,7 +103,9 @@ func parseService(name string, s compose.Service, ret chan interface{}) { store = helm.NewSecret(cf) } if err := store.AddEnvFile(envfile); err != nil { + ActivateColors = true Red(err.Error()) + ActivateColors = false os.Exit(2) } container.EnvFrom = append(container.EnvFrom, map[string]map[string]string{ @@ -177,14 +179,17 @@ func parseService(name string, s compose.Service, ret chan interface{}) { if !isCM && (strings.HasPrefix(volname, ".") || strings.HasPrefix(volname, "/")) { // local volume cannt be mounted + ActivateColors = true Redf("You cannot, at this time, have local volume in %s deployment\n", name) + ActivateColors = false continue } if isCM { + // the volume is a path and it's explicitally asked to be a configmap in labels cm := buildCMFromPath(volname) volname = strings.Replace(volname, "./", "", 1) volname = strings.ReplaceAll(volname, ".", "-") - cm.K8sBase.Metadata.Name = "{{ .Release.Name }}-" + volname + cm.K8sBase.Metadata.Name = "{{ .Release.Name }}-" + volname + "-" + name // build a configmap from the volume path volumes = append(volumes, map[string]interface{}{ "name": volname, @@ -449,8 +454,10 @@ func buildCMFromPath(path string) *helm.ConfigMap { if err != nil { fmt.Fprintf(os.Stderr, "An error occured reading volume path %s\n", err.Error()) } else { - fmt.Printf("Warning, %s is a directory, at this time we only "+ + ActivateColors = true + Yellowf("Warning, %s is a directory, at this time we only "+ "can create configmap for first level file list\n", f) + ActivateColors = false } continue } diff --git a/generator/utils.go b/generator/utils.go index c304bbc..4ed1e39 100644 --- a/generator/utils.go +++ b/generator/utils.go @@ -8,6 +8,8 @@ import ( type Color int +var ActivateColors = false + const ( GREY Color = 30 + iota RED @@ -21,6 +23,10 @@ const ( var waiter = sync.Mutex{} func color(c Color, args ...interface{}) { + if !ActivateColors { + fmt.Println(args...) + return + } waiter.Lock() fmt.Fprintf(os.Stdout, "\x1b[%dm", c) fmt.Fprint(os.Stdout, args...) @@ -29,6 +35,10 @@ func color(c Color, args ...interface{}) { } func colorf(c Color, format string, args ...interface{}) { + if !ActivateColors { + fmt.Printf(format, args...) + return + } waiter.Lock() fmt.Fprintf(os.Stdout, "\x1b[%dm", c) fmt.Fprintf(os.Stdout, format, args...) diff --git a/helm/types.go b/helm/types.go index bd2d717..66b6c7e 100644 --- a/helm/types.go +++ b/helm/types.go @@ -29,6 +29,10 @@ type Signable interface { BuildSHA(filename string) } +type Named interface { + Name() string +} + type Metadata struct { Name string `yaml:"name,omitempty"` Labels map[string]string `yaml:"labels"` @@ -66,10 +70,14 @@ func (k *K8sBase) BuildSHA(filename string) { k.Metadata.Annotations[K+"/docker-compose-sha256"] = fmt.Sprintf("%x", string(sum[:])) } -func (k K8sBase) Get() string { +func (k *K8sBase) Get() string { return k.Kind } +func (k *K8sBase) Name() string { + return k.Metadata.Name +} + func GetProjectName() string { if len(Appname) > 0 { return Appname diff --git a/main.go b/main.go index 915afc9..749616f 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "katenary/helm" "os" "path/filepath" + "regexp" "strings" "gopkg.in/yaml.v3" @@ -20,6 +21,8 @@ var AppVersion = "0.0.1" var Version = "master" var ChartsDir = "chart" +var PrefixRE = regexp.MustCompile(`\{\{.*\}\}-?`) + func main() { flag.StringVar(&ChartsDir, "chart-dir", ChartsDir, "set the chart directory") flag.StringVar(&ComposeFile, "compose", ComposeFile, "set the compose file to parse") @@ -163,6 +166,17 @@ func main() { } fp.Close() + case *helm.ConfigMap, *helm.Secret: + // there could be several files, so let's force the filename + name := c.(helm.Named).Name() + name = PrefixRE.ReplaceAllString(name, "") + fname := filepath.Join(templatesDir, n+"."+name+"."+kind+".yaml") + fp, _ := os.Create(fname) + enc := yaml.NewEncoder(fp) + enc.SetIndent(2) + enc.Encode(c) + fp.Close() + default: fname := filepath.Join(templatesDir, n+"."+kind+".yaml") fp, _ := os.Create(fname)