Use regexp in set imagetag

This commit is contained in:
Jingfang Liu
2018-07-31 15:35:10 -07:00
parent 6a834b6262
commit 4471b75912
2 changed files with 10 additions and 6 deletions

View File

@@ -18,20 +18,22 @@ package commands
import (
"errors"
"strings"
"regexp"
"sort"
"github.com/spf13/cobra"
"github.com/kubernetes-sigs/kustomize/pkg/constants"
"github.com/kubernetes-sigs/kustomize/pkg/fs"
"github.com/kubernetes-sigs/kustomize/pkg/types"
"sort"
)
type setImageTagOptions struct {
imageTagMap map[string]string
}
var pattern = regexp.MustCompile("^(.*):([a-zA-Z0-9._-]*)$")
// newCmdSetImageTag sets the new tags for images in the kustomization.
func newCmdSetImageTag(fsys fs.FileSystem) *cobra.Command {
var o setImageTagOptions
@@ -71,11 +73,11 @@ func (o *setImageTagOptions) Validate(args []string) error {
}
o.imageTagMap = make(map[string]string)
for _, arg := range args {
imagetag := strings.Split(arg, ":")
if len(imagetag) != 2 {
imagetag := pattern.FindStringSubmatch(arg)
if len(imagetag) != 3 {
return errors.New("Invalid format of imagetag, must specify it as <image>:<newtag>")
}
o.imageTagMap[imagetag[0]] = imagetag[1]
o.imageTagMap[imagetag[1]] = imagetag[2]
}
return nil
}

View File

@@ -29,7 +29,7 @@ func TestSetImageTagsHappyPath(t *testing.T) {
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
cmd := newCmdSetImageTag(fakeFS)
args := []string{"image1:tag1", "image2:tag2"}
args := []string{"image1:tag1", "image2:tag2", "localhost:5000/operator:1.0.0"}
err := cmd.RunE(cmd, args)
if err != nil {
t.Errorf("unexpected cmd error: %v", err)
@@ -44,6 +44,8 @@ imageTags:
newTag: tag1
- name: image2
newTag: tag2
- name: localhost:5000/operator
newTag: 1.0.0
`)
if !strings.Contains(string(content), string(expected)) {
t.Errorf("expected imageTags in kustomization file")