Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write individual files to output path if it is a directory #960

Merged
merged 1 commit into from
Apr 18, 2019
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
}