Fix problems with relative/absolute path

This commit is contained in:
2022-05-08 09:11:31 +02:00
parent 7e861618bb
commit 333d051d49
3 changed files with 36 additions and 22 deletions

22
generator/utils.go Normal file
View File

@@ -0,0 +1,22 @@
package generator
import (
"katenary/compose"
"regexp"
"strings"
)
// replaceChars replaces some chars in a string.
const replaceChars = `[^a-zA-Z0-9._]`
// GetRelPath return the relative path from the root of the project.
func GetRelPath(path string) string {
return strings.Replace(path, compose.GetCurrentDir(), ".", 1)
}
// PathToName transform a path to a yaml name.
func PathToName(path string) string {
path = strings.TrimPrefix(GetRelPath(path), "./")
path = regexp.MustCompile(replaceChars).ReplaceAllString(path, "-")
return path
}