Merge branch 'feat-multiple-composefile' into feat-cronjob

This commit is contained in:
2022-06-10 11:33:29 +02:00
4 changed files with 30 additions and 21 deletions

View File

@@ -50,6 +50,7 @@ func main() {
}
// convert command, need some flags
var composeFiles *[]string
convertCmd := &cobra.Command{
Use: "convert",
Short: "Convert docker-compose to helm chart",
@@ -61,9 +62,7 @@ func main() {
"- 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()
chartVersion := c.Flag("chart-version").Value.String()
chartDir := c.Flag("output-dir").Value.String()
@@ -71,17 +70,17 @@ func main() {
if err != nil {
writers.IndentSize = indentation
}
Convert(composeFile, appversion, appName, chartDir, chartVersion, force)
Convert(*composeFiles, appversion, appName, chartDir, chartVersion, force)
},
}
composeFiles = convertCmd.Flags().StringArrayP(
"compose-file", "c", []string{ComposeFile}, "compose file to convert, can be use several times to override previous file. Order is important!")
convertCmd.Flags().BoolP(
"force", "f", false, "force overwrite of existing output files")
convertCmd.Flags().StringP(
"app-version", "a", AppVersion, "app version")
convertCmd.Flags().StringP(
"chart-version", "v", ChartVersion, "chart version")
convertCmd.Flags().StringP(
"compose-file", "c", ComposeFile, "docker compose file")
convertCmd.Flags().StringP(
"app-name", "n", AppName, "application name")
convertCmd.Flags().StringP(

View File

@@ -93,19 +93,24 @@ func detectGitVersion() (string, error) {
return defaulVersion, errors.New("git log failed")
}
func Convert(composeFile, appVersion, appName, chartDir, chartVersion string, force bool) {
func Convert(composeFile []string, appVersion, appName, chartDir, chartVersion string, force bool) {
if len(composeFile) == 0 {
fmt.Println("No compose file given")
return
}
_, err := os.Stat(composeFile)
if err != nil {
fmt.Println("No compose file found")
os.Exit(1)
composeFiles := composeFile
ComposeFile = composeFiles[0]
for _, cf := range composeFiles {
if _, err := os.Stat(cf); err != nil {
fmt.Printf("Compose file %s not found\n", cf)
return
}
}
// Parse the compose file now
p := compose.NewParser(composeFile)
p := compose.NewParser(composeFiles)
p.Parse(appName)
dirname := filepath.Join(chartDir, appName)

View File

@@ -26,32 +26,37 @@ var (
)
// NewParser create a Parser and parse the file given in filename. If filename is empty, we try to parse the content[0] argument that should be a valid YAML content.
func NewParser(filename string, content ...string) *Parser {
func NewParser(filename []string, content ...string) *Parser {
p := &Parser{}
if len(content) > 0 { // mainly for the tests...
dir := filepath.Dir(filename)
dir := filepath.Dir(filename[0])
err := os.MkdirAll(dir, 0755)
if err != nil {
log.Fatal(err)
}
p.temporary = &dir
ioutil.WriteFile(filename, []byte(content[0]), 0644)
cli.DefaultFileNames = []string{filename}
ioutil.WriteFile(filename[0], []byte(content[0]), 0644)
cli.DefaultFileNames = filename
}
// if filename is not in cli Default files, add it
if len(filename) > 0 {
found := false
for _, f := range cli.DefaultFileNames {
if f == filename {
for _, defaultFileName := range cli.DefaultFileNames {
for _, givenFileName := range filename {
if defaultFileName == givenFileName {
found = true
break
}
}
}
// add the file at first position
if !found {
cli.DefaultFileNames = append([]string{filename}, cli.DefaultFileNames...)
cli.DefaultFileNames = append([]string{filename[0]}, cli.DefaultFileNames...)
}
if len(filename) > 1 {
cli.DefaultOverrideFileNames = append(filename[1:], cli.DefaultOverrideFileNames...)
}
}

View File

@@ -127,7 +127,7 @@ func setUp(t *testing.T) (string, *compose.Parser) {
}
composefile := filepath.Join(tmpwork, "docker-compose.yaml")
p := compose.NewParser(composefile, DOCKER_COMPOSE_YML)
p := compose.NewParser([]string{composefile}, DOCKER_COMPOSE_YML)
// create envfile for "useenvfile" service
err = os.Mkdir(filepath.Join(tmpwork, "config"), 0777)