Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
| remove unecessary `sync` parameter in setup call
| https://github.com/knative/client/pull/656[#656]

| 🐛
| Fix `--image` flag to only allow single occurence
| https://github.com/knative/client/pull/647[#647]

## v0.12.0 (2020-01-29)

[cols="1,10,3", options="header", width="100%"]
Expand Down
25 changes: 22 additions & 3 deletions pkg/kn/commands/service/configuration_edit_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

type ConfigurationEditFlags struct {
// Direct field manipulation
Image string
Image uniqueStringArg
Env []string
EnvFrom []string
Mount []string
Expand Down Expand Up @@ -67,6 +67,25 @@ type ResourceFlags struct {
Memory string
}

// -- uniqueStringArg Value
// Custom implementation of flag.Value interface to prevent multiple value assignment.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever :)

// Useful to enforce unique use of flags, e.g. --image.
type uniqueStringArg string

func (s *uniqueStringArg) Set(val string) error {
if len(*s) > 0 {
return errors.New("can be provided only once")
}
*s = uniqueStringArg(val)
return nil
}

func (s *uniqueStringArg) Type() string {
return "string"
}

func (s *uniqueStringArg) String() string { return string(*s) }

// markFlagMakesRevision indicates that a flag will create a new revision if you
// set it.
func (p *ConfigurationEditFlags) markFlagMakesRevision(f string) {
Expand All @@ -75,7 +94,7 @@ func (p *ConfigurationEditFlags) markFlagMakesRevision(f string) {

// addSharedFlags adds the flags common between create & update.
func (p *ConfigurationEditFlags) addSharedFlags(command *cobra.Command) {
command.Flags().StringVar(&p.Image, "image", "", "Image to run.")
command.Flags().VarP(&p.Image, "image", "", "Image to run.")
p.markFlagMakesRevision("image")
command.Flags().StringArrayVarP(&p.Env, "env", "e", []string{},
"Environment variable to set. NAME=value; you may provide this flag "+
Expand Down Expand Up @@ -251,7 +270,7 @@ func (p *ConfigurationEditFlags) Apply(
}
imageSet := false
if cmd.Flags().Changed("image") {
err = servinglib.UpdateImage(template, p.Image)
err = servinglib.UpdateImage(template, p.Image.String())
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/kn/commands/service/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"knative.dev/client/pkg/kn/commands"
servinglib "knative.dev/client/pkg/serving"
"knative.dev/client/pkg/util"
"knative.dev/client/pkg/wait"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -135,6 +136,13 @@ func TestServiceCreateImage(t *testing.T) {
}
}

func TestServiceCreateWithMultipleImages(t *testing.T) {
_, _, _, err := fakeServiceCreate([]string{
"service", "create", "foo", "--image", "gcr.io/foo/bar:baz", "--image", "gcr.io/bar/foo:baz", "--no-wait"}, false)

assert.Assert(t, util.ContainsAll(err.Error(), "\"--image\"", "\"gcr.io/bar/foo:baz\"", "flag", "once"))
}

func TestServiceCreateImageSync(t *testing.T) {
action, created, output, err := fakeServiceCreate([]string{
"service", "create", "foo", "--image", "gcr.io/foo/bar:baz"}, false)
Expand Down
8 changes: 8 additions & 0 deletions pkg/kn/commands/service/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ func TestServiceUpdateImage(t *testing.T) {
}
}

func TestServiceUpdateWithMultipleImages(t *testing.T) {
orig := newEmptyService()
_, _, _, err := fakeServiceUpdate(orig, []string{
"service", "create", "foo", "--image", "gcr.io/foo/bar:baz", "--image", "gcr.io/bar/foo:baz", "--no-wait"})

assert.Assert(t, util.ContainsAll(err.Error(), "\"--image\"", "\"gcr.io/bar/foo:baz\"", "flag", "once"))
}

func TestServiceUpdateCommand(t *testing.T) {
orig := newEmptyService()

Expand Down