Use ManifestLoader to read and write the kubemanifest file

This commit is contained in:
Jingfang Liu 2018-02-21 14:46:14 -08:00
parent af31960388
commit ec449fc3d6
1 changed files with 4 additions and 24 deletions

View File

@ -19,13 +19,11 @@ package commands
import ( import (
"io" "io"
"github.com/ghodss/yaml"
"errors" "errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
"k8s.io/kubectl/pkg/kinflate/constants" "k8s.io/kubectl/pkg/kinflate/constants"
"k8s.io/kubectl/pkg/kinflate/tree"
"k8s.io/kubectl/pkg/kinflate/util/fs" "k8s.io/kubectl/pkg/kinflate/util/fs"
) )
@ -80,29 +78,11 @@ func (o *setNamePrefixOptions) Complete(cmd *cobra.Command, args []string) error
// RunSetNamePrefix runs setNamePrefix command (does real work). // RunSetNamePrefix runs setNamePrefix command (does real work).
func (o *setNamePrefixOptions) RunSetNamePrefix(out, errOut io.Writer, fsys fs.FileSystem) error { func (o *setNamePrefixOptions) RunSetNamePrefix(out, errOut io.Writer, fsys fs.FileSystem) error {
content, err := fsys.ReadFile(constants.KubeManifestFileName) loader := tree.ManifestLoader{FS: fsys}
m, err := loader.Read(constants.KubeManifestFileName)
if err != nil { if err != nil {
return err return err
} }
// TODO: Refactor manifest reading to a common location.
// See pkg/kinflate/util.go:loadManifestPkg
var m manifest.Manifest
err = yaml.Unmarshal(content, &m)
if err != nil {
return err
}
m.NamePrefix = o.prefix m.NamePrefix = o.prefix
return loader.Write(constants.KubeManifestFileName, m)
bytes, err := yaml.Marshal(m)
if err != nil {
return err
}
err = fsys.WriteFile(constants.KubeManifestFileName, bytes)
if err != nil {
return err
}
return nil
} }