From 4cf470f2228197af9747e0b2b307dfae54d77746 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 3 Jun 2022 16:17:53 +0200 Subject: [PATCH 1/5] Allow several compose file in -c arg --- cmd/katenary/utils.go | 18 +++++++++++++----- compose/parser.go | 20 +++++++++++--------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/cmd/katenary/utils.go b/cmd/katenary/utils.go index bc0d8b4..9e179e5 100644 --- a/cmd/katenary/utils.go +++ b/cmd/katenary/utils.go @@ -98,14 +98,22 @@ func Convert(composeFile, appVersion, appName, chartDir, chartVersion string, fo fmt.Println("No compose file given") return } - _, err := os.Stat(composeFile) - if err != nil { - fmt.Println("No compose file found") - os.Exit(1) + + composeFiles := strings.Split(composeFile, ",") + for i, c := range composeFiles { + composeFiles[i] = strings.TrimSpace(c) + } + + for _, composeFile := range composeFiles { + _, err := os.Stat(composeFile) + if err != nil { + fmt.Println("Error reading file:", composeFile) + os.Exit(1) + } } // Parse the compose file now - p := compose.NewParser(composeFile) + p := compose.NewParser(composeFiles) p.Parse(appName) dirname := filepath.Join(chartDir, appName) diff --git a/compose/parser.go b/compose/parser.go index 86b0cb5..2e8a1b0 100644 --- a/compose/parser.go +++ b/compose/parser.go @@ -26,32 +26,34 @@ 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 { - found = true - break + 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(filename, cli.DefaultFileNames...) } } From 918baa2ce05a81f4d2f525f1581899be984da97e Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 3 Jun 2022 16:44:08 +0200 Subject: [PATCH 2/5] Set extended files as overrides config --- compose/parser.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compose/parser.go b/compose/parser.go index 2e8a1b0..2653b5b 100644 --- a/compose/parser.go +++ b/compose/parser.go @@ -53,7 +53,10 @@ func NewParser(filename []string, content ...string) *Parser { } // add the file at first position if !found { - cli.DefaultFileNames = append(filename, cli.DefaultFileNames...) + cli.DefaultFileNames = append([]string{filename[0]}, cli.DefaultFileNames...) + } + if len(filename) > 1 { + cli.DefaultOverrideFileNames = append(filename[1:], cli.DefaultOverrideFileNames...) } } From af05e41225349d0dc6d07f8ad6db1ba73d687369 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Thu, 9 Jun 2022 09:20:12 +0200 Subject: [PATCH 3/5] Make "-c" (compose-file) argument repeated --- cmd/katenary/main.go | 4 ++-- cmd/katenary/utils.go | 7 +++---- generator/main_test.go | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/cmd/katenary/main.go b/cmd/katenary/main.go index 712f057..de1ce08 100644 --- a/cmd/katenary/main.go +++ b/cmd/katenary/main.go @@ -61,9 +61,9 @@ 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 + composeFile := c.Flags().StringArrayP("compose-file", "c", []string{}, "compose file to convert, can be use several times to override previous file (default: docker-compose.yml)") // 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,7 +71,7 @@ func main() { if err != nil { writers.IndentSize = indentation } - Convert(composeFile, appversion, appName, chartDir, chartVersion, force) + Convert(*composeFile, appversion, appName, chartDir, chartVersion, force) }, } convertCmd.Flags().BoolP( diff --git a/cmd/katenary/utils.go b/cmd/katenary/utils.go index 9e179e5..2f6d071 100644 --- a/cmd/katenary/utils.go +++ b/cmd/katenary/utils.go @@ -93,15 +93,14 @@ 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 } - composeFiles := strings.Split(composeFile, ",") - for i, c := range composeFiles { - composeFiles[i] = strings.TrimSpace(c) + for i, c := range composeFile { + composeFile[i] = strings.TrimSpace(c) } for _, composeFile := range composeFiles { diff --git a/generator/main_test.go b/generator/main_test.go index 7a57006..c90ca39 100644 --- a/generator/main_test.go +++ b/generator/main_test.go @@ -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) From e5d4ed1df594190149fdb8cda114c2a4f2ce8863 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Thu, 9 Jun 2022 09:31:27 +0200 Subject: [PATCH 4/5] Fix compose-file argument usage --- cmd/katenary/main.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cmd/katenary/main.go b/cmd/katenary/main.go index de1ce08..de1ff9b 100644 --- a/cmd/katenary/main.go +++ b/cmd/katenary/main.go @@ -5,6 +5,7 @@ import ( "katenary/generator/writers" "katenary/helm" "katenary/update" + "log" "strconv" "github.com/spf13/cobra" @@ -50,6 +51,7 @@ func main() { } // convert command, need some flags + var composeFiles *[]string convertCmd := &cobra.Command{ Use: "convert", Short: "Convert docker-compose to helm chart", @@ -61,8 +63,6 @@ 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 - composeFile := c.Flags().StringArrayP("compose-file", "c", []string{}, "compose file to convert, can be use several times to override previous file (default: docker-compose.yml)") - // TODO: is there a way to get typed values from cobra? appversion := c.Flag("app-version").Value.String() appName := c.Flag("app-name").Value.String() chartVersion := c.Flag("chart-version").Value.String() @@ -71,17 +71,18 @@ func main() { if err != nil { writers.IndentSize = indentation } - Convert(*composeFile, appversion, appName, chartDir, chartVersion, force) + log.Println(composeFiles) + 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( From 5a8a1b135ed7249b6008977724cc1373bfffd215 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 10 Jun 2022 09:47:15 +0200 Subject: [PATCH 5/5] Fixup the command line argument to get multiple compose files --- cmd/katenary/main.go | 2 -- cmd/katenary/utils.go | 14 ++++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/cmd/katenary/main.go b/cmd/katenary/main.go index de1ff9b..ceaea8e 100644 --- a/cmd/katenary/main.go +++ b/cmd/katenary/main.go @@ -5,7 +5,6 @@ import ( "katenary/generator/writers" "katenary/helm" "katenary/update" - "log" "strconv" "github.com/spf13/cobra" @@ -71,7 +70,6 @@ func main() { if err != nil { writers.IndentSize = indentation } - log.Println(composeFiles) Convert(*composeFiles, appversion, appName, chartDir, chartVersion, force) }, } diff --git a/cmd/katenary/utils.go b/cmd/katenary/utils.go index 2f6d071..118932e 100644 --- a/cmd/katenary/utils.go +++ b/cmd/katenary/utils.go @@ -99,15 +99,13 @@ func Convert(composeFile []string, appVersion, appName, chartDir, chartVersion s return } - for i, c := range composeFile { - composeFile[i] = strings.TrimSpace(c) - } + composeFiles := composeFile + ComposeFile = composeFiles[0] - for _, composeFile := range composeFiles { - _, err := os.Stat(composeFile) - if err != nil { - fmt.Println("Error reading file:", composeFile) - os.Exit(1) + for _, cf := range composeFiles { + if _, err := os.Stat(cf); err != nil { + fmt.Printf("Compose file %s not found\n", cf) + return } }