Output to a dir works.

This commit is contained in:
Martyn 2026-05-05 22:52:37 +02:00
parent 23f83da3d5
commit 3cd043b2b1

View file

@ -108,7 +108,7 @@ type argoApp struct {
} }
var flagInputYaml fileflag.InputFileFlag = "helmfile.yaml" var flagInputYaml fileflag.InputFileFlag = "helmfile.yaml"
var singleFile bool = true var singleFile bool = false
var outputDir string = "-" //TODO: actually handle files var outputDir string = "-" //TODO: actually handle files
var appNamespace string = "argocd" var appNamespace string = "argocd"
var projectName string = "helmfile-imported" 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 a set of .yaml files describing argocd Applications that are built from the
releases section.`, releases section.`,
Run: func(cmd *cobra.Command, args []string) { 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) fmt.Println("converting file:", flagInputYaml)
if (loadEnvironmentValues != "") { if (loadEnvironmentValues != "") {
@ -150,7 +160,7 @@ releases section.`,
outputProject.Spec.SourceRepos = append(outputProject.Spec.SourceRepos, helmfileContents.Repositories[i].Url) outputProject.Spec.SourceRepos = append(outputProject.Spec.SourceRepos, helmfileContents.Repositories[i].Url)
} }
outputProjectYaml, _ := yaml.Marshal(outputProject) outputProjectYaml, _ := yaml.Marshal(outputProject)
outputYamlFile(outputProjectYaml) outputYamlFile(outputProjectYaml, "project-"+projectName+".yaml")
} }
reposFromHelmChart = helmfileContents.Repositories reposFromHelmChart = helmfileContents.Repositories
if len(helmfileContents.Releases) > 0 { if len(helmfileContents.Releases) > 0 {
@ -192,7 +202,7 @@ releases section.`,
outputApp.Spec.Destination.Namespace = thisRelease.Namespace outputApp.Spec.Destination.Namespace = thisRelease.Namespace
outputAppYaml, _ := yaml.Marshal(outputApp) 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 return nv
} }
func parseHelmfile(helmfileFilename string) helmfileStructure { func parseHelmfile(helmfileFilename string, passedValues []map[string]interface{}) helmfileStructure {
var finalPath = helmfileFilename
fmt.Println("parsing file:", flagInputYaml) if helmfileFilename != string(flagInputYaml) {
yamlFile, err := os.ReadFile(helmfileFilename) 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 { if err != nil {
fmt.Printf("yamlFile.Get err #%v ", err) fmt.Printf("yamlFile.Get err #%v ", err)
} }
@ -228,6 +246,11 @@ func parseHelmfile(helmfileFilename string) helmfileStructure {
if err != nil { if err != nil {
fmt.Printf("Unmarshal: %v", err) 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 return *helmfileContents
} }
@ -275,18 +298,25 @@ func repoURLbyName(repoName string) (error, string) {
} }
func outputString(data string, filename string) { func outputString(data string, filename string) {
if filename == "-" { if string(outputDir) == "-" {
fmt.Println(data) fmt.Println(data)
} else { } 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) == "-") { 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 { func baseProject(name string) argoProject {
@ -321,7 +351,7 @@ func init() {
// is called directly, e.g.: // is called directly, e.g.:
// convertCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") // convertCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
convertCmd.Flags().VarP(&flagInputYaml, "input", "i", `helmfile to convert`) 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(&outputDir, "output", "o", "-", `output folder or "-" for stdout`)
convertCmd.Flags().StringVarP(&appNamespace, "appnamespace", "a", "argocd", `namespace for application objects`) convertCmd.Flags().StringVarP(&appNamespace, "appnamespace", "a", "argocd", `namespace for application objects`)
convertCmd.Flags().StringVarP(&projectName, "projectname", "p", "helmfile-imported", `project name for all apps`) convertCmd.Flags().StringVarP(&projectName, "projectname", "p", "helmfile-imported", `project name for all apps`)