From 9203478a8ad63500a3b32adfdad1a8737cd16169 Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 9 Apr 2019 19:40:46 +0200 Subject: [PATCH] Write individual files to output path if it is a directory --- pkg/commands/build/build.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pkg/commands/build/build.go b/pkg/commands/build/build.go index a747da0f38..e0d346bdff 100644 --- a/pkg/commands/build/build.go +++ b/pkg/commands/build/build.go @@ -17,8 +17,12 @@ limitations under the License. package build import ( + "fmt" "io" + "path" + "strings" + "github.com/ghodss/yaml" "github.com/pkg/errors" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/pkg/fs" @@ -127,6 +131,9 @@ func (o *Options) RunBuild( return err } if o.outputPath != "" { + if fSys.IsDir(o.outputPath) { + return writeIndividualFiles(fSys, o.outputPath, allResources) + } return fSys.WriteFile(o.outputPath, res) } _, err = out.Write(res) @@ -156,6 +163,9 @@ func (o *Options) RunBuildPrune( return err } if o.outputPath != "" { + if fSys.IsDir(o.outputPath) { + return writeIndividualFiles(fSys, o.outputPath, allResources) + } return fSys.WriteFile(o.outputPath, res) } _, err = out.Write(res) @@ -184,3 +194,25 @@ func NewCmdBuildPrune( } return cmd } + +func writeIndividualFiles(fSys fs.FileSystem, folderPath string, resources resmap.ResMap) error { + for _, obj := range resources { + filename := path.Join( + folderPath, + fmt.Sprintf( + "%s_%s.yaml", + strings.ToLower(obj.GetGvk().String()), + strings.ToLower(obj.GetName()), + ), + ) + out, err := yaml.Marshal(obj.Map()) + if err != nil { + return err + } + err = fSys.WriteFile(filename, out) + if err != nil { + return err + } + } + return nil +}