Skip to content

Commit c444f93

Browse files
authored
Merge pull request #960 from zeeZ/individual-output
Write individual files to output path if it is a directory
2 parents ed146f6 + 9203478 commit c444f93

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

pkg/commands/build/build.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ limitations under the License.
1717
package build
1818

1919
import (
20+
"fmt"
2021
"io"
22+
"path"
23+
"strings"
2124

25+
"github.com/ghodss/yaml"
2226
"github.com/pkg/errors"
2327
"github.com/spf13/cobra"
2428
"sigs.k8s.io/kustomize/pkg/fs"
@@ -127,6 +131,9 @@ func (o *Options) RunBuild(
127131
return err
128132
}
129133
if o.outputPath != "" {
134+
if fSys.IsDir(o.outputPath) {
135+
return writeIndividualFiles(fSys, o.outputPath, allResources)
136+
}
130137
return fSys.WriteFile(o.outputPath, res)
131138
}
132139
_, err = out.Write(res)
@@ -156,6 +163,9 @@ func (o *Options) RunBuildPrune(
156163
return err
157164
}
158165
if o.outputPath != "" {
166+
if fSys.IsDir(o.outputPath) {
167+
return writeIndividualFiles(fSys, o.outputPath, allResources)
168+
}
159169
return fSys.WriteFile(o.outputPath, res)
160170
}
161171
_, err = out.Write(res)
@@ -184,3 +194,25 @@ func NewCmdBuildPrune(
184194
}
185195
return cmd
186196
}
197+
198+
func writeIndividualFiles(fSys fs.FileSystem, folderPath string, resources resmap.ResMap) error {
199+
for _, obj := range resources {
200+
filename := path.Join(
201+
folderPath,
202+
fmt.Sprintf(
203+
"%s_%s.yaml",
204+
strings.ToLower(obj.GetGvk().String()),
205+
strings.ToLower(obj.GetName()),
206+
),
207+
)
208+
out, err := yaml.Marshal(obj.Map())
209+
if err != nil {
210+
return err
211+
}
212+
err = fSys.WriteFile(filename, out)
213+
if err != nil {
214+
return err
215+
}
216+
}
217+
return nil
218+
}

0 commit comments

Comments
 (0)