Merge pull request #222 from Liujingfang1/glob

Add glob support in edit add resource
This commit is contained in:
Jeff Regan
2018-08-01 15:51:45 -07:00
committed by GitHub
10 changed files with 84 additions and 83 deletions

View File

@@ -18,7 +18,7 @@ package commands
import (
"errors"
"fmt"
"log"
"github.com/spf13/cobra"
@@ -27,7 +27,7 @@ import (
)
type addResourceOptions struct {
resourceFilePath string
resourceFilePaths []string
}
// newCmdAddResource adds the name of a file containing a resource to the kustomization file.
@@ -56,10 +56,10 @@ func newCmdAddResource(fsys fs.FileSystem) *cobra.Command {
// Validate validates addResource command.
func (o *addResourceOptions) Validate(args []string) error {
if len(args) != 1 {
if len(args) == 0 {
return errors.New("must specify a resource file")
}
o.resourceFilePath = args[0]
o.resourceFilePaths = args
return nil
}
@@ -70,9 +70,24 @@ func (o *addResourceOptions) Complete(cmd *cobra.Command, args []string) error {
// RunAddResource runs addResource command (do real work).
func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error {
if !fsys.Exists(o.resourceFilePath) {
return errors.New(o.resourceFilePath + " does not exist")
var resources []string
for _, pattern := range o.resourceFilePaths {
files, err := fsys.Glob(pattern)
if err != nil {
return err
}
if len(files) == 0 {
log.Printf("%s has no match", pattern)
continue
}
resources = append(resources, files...)
}
if len(resources) == 0 {
return nil
}
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil {
return err
@@ -83,11 +98,13 @@ func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error {
return err
}
if stringInSlice(o.resourceFilePath, m.Resources) {
return fmt.Errorf("resource %s already in kustomization file", o.resourceFilePath)
for _, resource := range resources {
if stringInSlice(resource, m.Resources) {
log.Printf("resource %s already in kustomization file", resource)
continue
}
m.Resources = append(m.Resources, resource)
}
m.Resources = append(m.Resources, o.resourceFilePath)
return mf.write(m)
}

View File

@@ -17,9 +17,8 @@ limitations under the License.
package commands
import (
"testing"
"strings"
"testing"
"github.com/kubernetes-sigs/kustomize/pkg/constants"
"github.com/kubernetes-sigs/kustomize/pkg/fs"
@@ -52,10 +51,11 @@ secretGenerator: []
func TestAddResourceHappyPath(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
fakeFS.WriteFile(resourceFileName+"another", []byte(resourceFileContent))
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
cmd := newCmdAddResource(fakeFS)
args := []string{resourceFileName}
args := []string{resourceFileName + "*"}
err := cmd.RunE(cmd, args)
if err != nil {
t.Errorf("unexpected cmd error: %v", err)
@@ -67,6 +67,9 @@ func TestAddResourceHappyPath(t *testing.T) {
if !strings.Contains(string(content), resourceFileName) {
t.Errorf("expected resource name in kustomization")
}
if !strings.Contains(string(content), resourceFileName+"another") {
t.Errorf("expected resource name in kustomization")
}
}
func TestAddResourceAlreadyThere(t *testing.T) {
@@ -81,13 +84,10 @@ func TestAddResourceAlreadyThere(t *testing.T) {
t.Fatalf("unexpected cmd error: %v", err)
}
// adding an existing resource should return an error
// adding an existing resource doesn't return an error
err = cmd.RunE(cmd, args)
if err == nil {
t.Errorf("expected already there problem")
}
if err.Error() != "resource "+resourceFileName+" already in kustomization file" {
t.Errorf("unexpected error %v", err)
if err != nil {
t.Errorf("unexpected cmd error :%v", err)
}
}