diff --git a/cmd/convert.go b/cmd/convert.go index af7c9fd..b36c86a 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -108,7 +108,7 @@ type argoApp struct { } var flagInputYaml fileflag.InputFileFlag = "helmfile.yaml" -var singleFile bool = true +var singleFile bool = false var outputDir string = "-" //TODO: actually handle files var appNamespace string = "argocd" var projectName string = "helmfile-imported" @@ -128,7 +128,17 @@ var convertCmd = &cobra.Command{ a set of .yaml files describing argocd Applications that are built from the releases section.`, Run: func(cmd *cobra.Command, args []string) { - helmfileContents := parseHelmfile(string(flagInputYaml)) + helmfileContents := parseHelmfile(string(flagInputYaml), nil) + if recursiveConvert && len(helmfileContents.HelmFiles) > 0 { + for i := 0; i <= len(helmfileContents.HelmFiles)-1; i++ { + fmt.Print("Merging in ") + fmt.Println(helmfileContents.HelmFiles[i].Path) + mergeIn := parseHelmfile(string(helmfileContents.HelmFiles[i].Path), helmfileContents.HelmFiles[i].Values) + helmfileContents.Repositories = append(helmfileContents.Repositories, mergeIn.Repositories[:]...) + helmfileContents.Releases = append(helmfileContents.Releases, mergeIn.Releases[:]...) + maps.Copy(helmfileContents.Environments,mergeIn.Environments) + } + } fmt.Println("converting file:", flagInputYaml) if (loadEnvironmentValues != "") { @@ -150,7 +160,7 @@ releases section.`, outputProject.Spec.SourceRepos = append(outputProject.Spec.SourceRepos, helmfileContents.Repositories[i].Url) } outputProjectYaml, _ := yaml.Marshal(outputProject) - outputYamlFile(outputProjectYaml) + outputYamlFile(outputProjectYaml, "project-"+projectName+".yaml") } reposFromHelmChart = helmfileContents.Repositories if len(helmfileContents.Releases) > 0 { @@ -192,7 +202,7 @@ releases section.`, outputApp.Spec.Destination.Namespace = thisRelease.Namespace outputAppYaml, _ := yaml.Marshal(outputApp) - outputYamlFile(outputAppYaml) + outputYamlFile(outputAppYaml,"app-"+thisRelease.Name+".yaml") } } }, @@ -210,10 +220,18 @@ func flattenHelmfileWeirdValues(hfv []map[string]interface{}) map[string]interfa return nv } -func parseHelmfile(helmfileFilename string) helmfileStructure { - - fmt.Println("parsing file:", flagInputYaml) - yamlFile, err := os.ReadFile(helmfileFilename) +func parseHelmfile(helmfileFilename string, passedValues []map[string]interface{}) helmfileStructure { + var finalPath = helmfileFilename + if helmfileFilename != string(flagInputYaml) { + absolutePath, err := realpath.Realpath(string(flagInputYaml)) + basePath := filepath.Dir(absolutePath) + finalPath, err = realpath.Realpath(basePath + string(os.PathSeparator) + helmfileFilename) + if err != nil { + panic(err) + } + } + fmt.Println("parsing file:", finalPath) + yamlFile, err := os.ReadFile(finalPath) if err != nil { fmt.Printf("yamlFile.Get err #%v ", err) } @@ -228,6 +246,11 @@ func parseHelmfile(helmfileFilename string) helmfileStructure { if err != nil { fmt.Printf("Unmarshal: %v", err) } + if passedValues != nil { + for i := 0; i <= len(helmfileContents.Releases)-1; i++ { + helmfileContents.Releases[i].Values = append(helmfileContents.Releases[i].Values, passedValues...) + } + } return *helmfileContents } @@ -275,18 +298,25 @@ func repoURLbyName(repoName string) (error, string) { } func outputString(data string, filename string) { - if filename == "-" { + if string(outputDir) == "-" { fmt.Println(data) } else { - fmt.Println(filename + " is not -") + f, err := os.OpenFile(outputDir+string(os.PathSeparator)+filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + panic(err) + } + defer f.Close() + if _, err := f.WriteString(data); err != nil { + panic(err) + } } } -func outputYamlFile(bytes []byte) { +func outputYamlFile(bytes []byte, filename string) { if singleFile || (string(outputDir) == "-") { - outputString("---", string(outputDir)) + outputString("---\n", filename) } - outputString(string(bytes), string(outputDir)) + outputString(string(bytes)+"\n", filename) } func baseProject(name string) argoProject { @@ -321,7 +351,7 @@ func init() { // is called directly, e.g.: // convertCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") convertCmd.Flags().VarP(&flagInputYaml, "input", "i", `helmfile to convert`) - convertCmd.Flags().BoolVarP(&singleFile, "single-file", "1", true, `single output file (always true if output is "-")`) + convertCmd.Flags().BoolVarP(&singleFile, "single-file", "1", false, `single output file (always true if output is "-")`) convertCmd.Flags().StringVarP(&outputDir, "output", "o", "-", `output folder or "-" for stdout`) convertCmd.Flags().StringVarP(&appNamespace, "appnamespace", "a", "argocd", `namespace for application objects`) convertCmd.Flags().StringVarP(&projectName, "projectname", "p", "helmfile-imported", `project name for all apps`)