mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Write individual files to output path if it is a directory
This commit is contained in:
@@ -17,8 +17,12 @@ limitations under the License.
|
|||||||
package build
|
package build
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/ghodss/yaml"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
@@ -127,6 +131,9 @@ func (o *Options) RunBuild(
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if o.outputPath != "" {
|
if o.outputPath != "" {
|
||||||
|
if fSys.IsDir(o.outputPath) {
|
||||||
|
return writeIndividualFiles(fSys, o.outputPath, allResources)
|
||||||
|
}
|
||||||
return fSys.WriteFile(o.outputPath, res)
|
return fSys.WriteFile(o.outputPath, res)
|
||||||
}
|
}
|
||||||
_, err = out.Write(res)
|
_, err = out.Write(res)
|
||||||
@@ -156,6 +163,9 @@ func (o *Options) RunBuildPrune(
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if o.outputPath != "" {
|
if o.outputPath != "" {
|
||||||
|
if fSys.IsDir(o.outputPath) {
|
||||||
|
return writeIndividualFiles(fSys, o.outputPath, allResources)
|
||||||
|
}
|
||||||
return fSys.WriteFile(o.outputPath, res)
|
return fSys.WriteFile(o.outputPath, res)
|
||||||
}
|
}
|
||||||
_, err = out.Write(res)
|
_, err = out.Write(res)
|
||||||
@@ -184,3 +194,25 @@ func NewCmdBuildPrune(
|
|||||||
}
|
}
|
||||||
return cmd
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user