Remove some duped code.

This commit is contained in:
Jeffrey Regan
2019-04-18 11:37:42 -07:00
parent c444f93eb5
commit 8767495b5a

View File

@@ -19,7 +19,7 @@ package build
import ( import (
"fmt" "fmt"
"io" "io"
"path" "path/filepath"
"strings" "strings"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
@@ -121,23 +121,11 @@ func (o *Options) RunBuild(
if err != nil { if err != nil {
return err return err
} }
allResources, err := kt.MakeCustomizedResMap() m, err := kt.MakeCustomizedResMap()
if err != nil { if err != nil {
return err return err
} }
// Output the objects. return o.emitResources(out, fSys, m)
res, err := allResources.EncodeAsYaml()
if err != nil {
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)
return err
} }
func (o *Options) RunBuildPrune( func (o *Options) RunBuildPrune(
@@ -153,19 +141,23 @@ func (o *Options) RunBuildPrune(
if err != nil { if err != nil {
return err return err
} }
allResources, err := kt.MakePruneConfigMap() m, err := kt.MakePruneConfigMap()
if err != nil { if err != nil {
return err return err
} }
// Output the objects. return o.emitResources(out, fSys, m)
res, err := allResources.EncodeAsYaml() }
func (o *Options) emitResources(
out io.Writer, fSys fs.FileSystem, m resmap.ResMap) error {
if o.outputPath != "" && fSys.IsDir(o.outputPath) {
return writeIndividualFiles(fSys, o.outputPath, m)
}
res, err := m.EncodeAsYaml()
if err != nil { if err != nil {
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)
@@ -195,17 +187,18 @@ func NewCmdBuildPrune(
return cmd return cmd
} }
func writeIndividualFiles(fSys fs.FileSystem, folderPath string, resources resmap.ResMap) error { func writeIndividualFiles(
for _, obj := range resources { fSys fs.FileSystem, folderPath string, m resmap.ResMap) error {
filename := path.Join( for _, res := range m {
filename := filepath.Join(
folderPath, folderPath,
fmt.Sprintf( fmt.Sprintf(
"%s_%s.yaml", "%s_%s.yaml",
strings.ToLower(obj.GetGvk().String()), strings.ToLower(res.GetGvk().String()),
strings.ToLower(obj.GetName()), strings.ToLower(res.GetName()),
), ),
) )
out, err := yaml.Marshal(obj.Map()) out, err := yaml.Marshal(res.Map())
if err != nil { if err != nil {
return err return err
} }