Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pkg/commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}