Fix names + color activation

This commit is contained in:
2021-12-02 16:07:15 +01:00
parent b894de6232
commit a292170a63
4 changed files with 42 additions and 3 deletions

View File

@@ -103,7 +103,9 @@ func parseService(name string, s compose.Service, ret chan interface{}) {
store = helm.NewSecret(cf) store = helm.NewSecret(cf)
} }
if err := store.AddEnvFile(envfile); err != nil { if err := store.AddEnvFile(envfile); err != nil {
ActivateColors = true
Red(err.Error()) Red(err.Error())
ActivateColors = false
os.Exit(2) os.Exit(2)
} }
container.EnvFrom = append(container.EnvFrom, map[string]map[string]string{ 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, "/")) { if !isCM && (strings.HasPrefix(volname, ".") || strings.HasPrefix(volname, "/")) {
// local volume cannt be mounted // local volume cannt be mounted
ActivateColors = true
Redf("You cannot, at this time, have local volume in %s deployment\n", name) Redf("You cannot, at this time, have local volume in %s deployment\n", name)
ActivateColors = false
continue continue
} }
if isCM { if isCM {
// the volume is a path and it's explicitally asked to be a configmap in labels
cm := buildCMFromPath(volname) cm := buildCMFromPath(volname)
volname = strings.Replace(volname, "./", "", 1) volname = strings.Replace(volname, "./", "", 1)
volname = strings.ReplaceAll(volname, ".", "-") 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 // build a configmap from the volume path
volumes = append(volumes, map[string]interface{}{ volumes = append(volumes, map[string]interface{}{
"name": volname, "name": volname,
@@ -449,8 +454,10 @@ func buildCMFromPath(path string) *helm.ConfigMap {
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "An error occured reading volume path %s\n", err.Error()) fmt.Fprintf(os.Stderr, "An error occured reading volume path %s\n", err.Error())
} else { } 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) "can create configmap for first level file list\n", f)
ActivateColors = false
} }
continue continue
} }

View File

@@ -8,6 +8,8 @@ import (
type Color int type Color int
var ActivateColors = false
const ( const (
GREY Color = 30 + iota GREY Color = 30 + iota
RED RED
@@ -21,6 +23,10 @@ const (
var waiter = sync.Mutex{} var waiter = sync.Mutex{}
func color(c Color, args ...interface{}) { func color(c Color, args ...interface{}) {
if !ActivateColors {
fmt.Println(args...)
return
}
waiter.Lock() waiter.Lock()
fmt.Fprintf(os.Stdout, "\x1b[%dm", c) fmt.Fprintf(os.Stdout, "\x1b[%dm", c)
fmt.Fprint(os.Stdout, args...) fmt.Fprint(os.Stdout, args...)
@@ -29,6 +35,10 @@ func color(c Color, args ...interface{}) {
} }
func colorf(c Color, format string, args ...interface{}) { func colorf(c Color, format string, args ...interface{}) {
if !ActivateColors {
fmt.Printf(format, args...)
return
}
waiter.Lock() waiter.Lock()
fmt.Fprintf(os.Stdout, "\x1b[%dm", c) fmt.Fprintf(os.Stdout, "\x1b[%dm", c)
fmt.Fprintf(os.Stdout, format, args...) fmt.Fprintf(os.Stdout, format, args...)

View File

@@ -29,6 +29,10 @@ type Signable interface {
BuildSHA(filename string) BuildSHA(filename string)
} }
type Named interface {
Name() string
}
type Metadata struct { type Metadata struct {
Name string `yaml:"name,omitempty"` Name string `yaml:"name,omitempty"`
Labels map[string]string `yaml:"labels"` 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[:])) 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 return k.Kind
} }
func (k *K8sBase) Name() string {
return k.Metadata.Name
}
func GetProjectName() string { func GetProjectName() string {
if len(Appname) > 0 { if len(Appname) > 0 {
return Appname return Appname

14
main.go
View File

@@ -9,6 +9,7 @@ import (
"katenary/helm" "katenary/helm"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@@ -20,6 +21,8 @@ var AppVersion = "0.0.1"
var Version = "master" var Version = "master"
var ChartsDir = "chart" var ChartsDir = "chart"
var PrefixRE = regexp.MustCompile(`\{\{.*\}\}-?`)
func main() { func main() {
flag.StringVar(&ChartsDir, "chart-dir", ChartsDir, "set the chart directory") flag.StringVar(&ChartsDir, "chart-dir", ChartsDir, "set the chart directory")
flag.StringVar(&ComposeFile, "compose", ComposeFile, "set the compose file to parse") flag.StringVar(&ComposeFile, "compose", ComposeFile, "set the compose file to parse")
@@ -163,6 +166,17 @@ func main() {
} }
fp.Close() 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: default:
fname := filepath.Join(templatesDir, n+"."+kind+".yaml") fname := filepath.Join(templatesDir, n+"."+kind+".yaml")
fp, _ := os.Create(fname) fp, _ := os.Create(fname)