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

View File

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