Better path to name + more comments

This commit is contained in:
2022-05-07 17:39:52 +02:00
parent 2b1dc68303
commit e7e4746774
2 changed files with 13 additions and 8 deletions

View File

@@ -10,6 +10,7 @@ import (
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@@ -889,9 +890,12 @@ func addVolumeFrom(deployment *helm.Deployment, container *helm.Container, s *ty
} }
} }
// replaceChars replaces some chars in a string.
const replaceChars = `[^a-zA-Z0-9._-]`
// PathToName transform a path to a yaml name.
func PathToName(path string) string { func PathToName(path string) string {
path = strings.TrimPrefix(path, "./") path = strings.TrimPrefix(path, "./")
path = strings.ReplaceAll(path, ".", "-") path = regexp.MustCompile(replaceChars).ReplaceAllString(path, "-")
path = strings.ReplaceAll(path, "/", "-")
return path return path
} }

View File

@@ -16,10 +16,14 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
// HelmFile represents a helm file from helm package that has got some necessary methods
// to generate a helm file.
type HelmFile interface { type HelmFile interface {
GetType() string GetType() string
GetPathRessource() string GetPathRessource() string
} }
// HelmFileGenerator is a chanel of HelmFile.
type HelmFileGenerator chan HelmFile type HelmFileGenerator chan HelmFile
var PrefixRE = regexp.MustCompile(`\{\{.*\}\}-?`) var PrefixRE = regexp.MustCompile(`\{\{.*\}\}-?`)
@@ -34,6 +38,7 @@ func portExists(port int, ports []types.ServicePortConfig) bool {
return false return false
} }
// Generate get a parsed compose file, and generate the helm files.
func Generate(p *compose.Parser, katernayVersion, appName, appVersion, chartVersion, composeFile, dirName string) { func Generate(p *compose.Parser, katernayVersion, appName, appVersion, chartVersion, composeFile, dirName string) {
// make the appname global (yes... ugly but easy) // make the appname global (yes... ugly but easy)
@@ -55,6 +60,7 @@ func Generate(p *compose.Parser, katernayVersion, appName, appVersion, chartVers
// Manage services to not export // Manage services to not export
skips := make(map[string]bool) skips := make(map[string]bool)
// remove ignored services
for _, s := range p.Data.Services { for _, s := range p.Data.Services {
if s.Labels[helm.LABEL_IGNORE] == "true" { if s.Labels[helm.LABEL_IGNORE] == "true" {
skips[s.Name] = true skips[s.Name] = true
@@ -185,12 +191,7 @@ func Generate(p *compose.Parser, katernayVersion, appName, appVersion, chartVers
// there could be several files, so let's force the filename // there could be several files, so let's force the filename
name := c.(helm.Named).Name() + "-" + c.GetType() name := c.(helm.Named).Name() + "-" + c.GetType()
suffix := c.GetPathRessource() suffix := c.GetPathRessource()
if suffix != "" { suffix = PathToName(suffix)
charToRemove := []string{"/", ".", " "}
for _, char := range charToRemove {
suffix = strings.Replace(suffix, char, "-", -1)
}
}
name += suffix name += suffix
name = PrefixRE.ReplaceAllString(name, "") name = PrefixRE.ReplaceAllString(name, "")
writers.BuildConfigMap(c, kind, n, name, templatesDir) writers.BuildConfigMap(c, kind, n, name, templatesDir)