Skip to content

Fix --image flag to only allow single occurence#647

Merged
knative-prow-robot merged 6 commits into
knative:masterfrom
dsimansk:issue-533
Feb 12, 2020
Merged

Fix --image flag to only allow single occurence#647
knative-prow-robot merged 6 commits into
knative:masterfrom
dsimansk:issue-533

Conversation

@dsimansk
Copy link
Copy Markdown
Contributor

@dsimansk dsimansk commented Feb 11, 2020

Fixes #533

Proposed Changes

  • Introduce new type singletonString for flag values that only allow single value assignment, otherwise return error
  • Fix --image flag with the new type.

Example:

➜  client git:(issue-533) kn service create svc-test --image gcr.io/knative-samples/helloworld-go --image gcr.io/foo/bar:barz --no-wait 
invalid argument "gcr.io/foo/bar:barz" for "--image" flag: value is already set

@googlebot googlebot added the cla: yes Indicates the PR's author has signed the CLA. label Feb 11, 2020
Copy link
Copy Markdown
Contributor

@knative-prow-robot knative-prow-robot left a comment

Choose a reason for hiding this comment

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

@dsimansk: 0 warnings.

Details

In response to this:

Fixes #533

Proposed Changes

  • Introduce new type singletonString for flag values that only allow single value assignment, otherwise return error
  • Fix --image flag with the new type.

Example:

➜  client git:(issue-533) kn service create svc-test --image gcr.io/knative-samples/helloworld-go --image --image gcr.io/foo/bar:barz --no-wait
invalid argument "--image" for "--image" flag: value is already set

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@knative-prow-robot knative-prow-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Feb 11, 2020
Copy link
Copy Markdown
Contributor

@rhuss rhuss left a comment

Choose a reason for hiding this comment

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

Nice ! That's the way to go, please add some unit tests, too.

See also below for a suggestion how to improve the error message.


func (s *singletonString) Set(val string) error {
if len(*s) > 0 {
return errors.New("value is already set")
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.

I'm not sure how this "feels" like on the CLI. If this is the only error printed, then it might be confusing (especially when you have a large list of options). It would be better to actually print which argument was duplicated (i.e. --image here). I think you could implement that by using a type like

type uniqueStringArg struct {  
   // which kind of argument it is
    kind string 

   // Argument value
   value string
}

func newUniqueStringArg(kind string) uniqueStringArg {
     return uniqueStringArg{kind: kind}
}

and then partially pre-initialize the ConfiguarionEditFlags like in

ceF = ConfigurationEditFlags{
    Image: newUniqueStringArg("image")
}

and then finally adapt the error message to include the kind.

Does this make sense ?

Copy link
Copy Markdown
Contributor Author

@dsimansk dsimansk Feb 11, 2020

Choose a reason for hiding this comment

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

Actually, the error already contains the flag.

➜  client git:(issue-533) kn service create svc-test --image gcr.io/knative-samples/helloworld-go --image gcr.io/foo/bar:barz --no-wait 
invalid argument "gcr.io/foo/bar:barz" for "--image" flag: value is already set

Copy link
Copy Markdown
Contributor Author

@dsimansk dsimansk Feb 11, 2020

Choose a reason for hiding this comment

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

Maybe a bit more descriptive error message to explain what is wrong with value already set?

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.

Ah, if this is the case, then I'm fine with having the current solution.

However for the error message I woudl use something like "flag can be provided only once" (so from the POV of the user, not from the flag's POV)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK, anyway I do like your suggested name uniqueStringArg a bit more. I'll change the name, error message and finally add some unit tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reflected review feedback in c68fe1d and added unit tests in c990288.

@knative-prow-robot knative-prow-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 11, 2020
Copy link
Copy Markdown
Contributor

@rhuss rhuss left a comment

Choose a reason for hiding this comment

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

tests are looking good with some minor remark how they could be improved.

Comment thread pkg/kn/commands/service/create_test.go Outdated
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, false)
assert.Error(t, err, "invalid argument \"gcr.io/bar/foo:baz\" for \"--image\" flag: can be provided only once")
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.

What we also do typically in unit test to assert on the content of the error message: not the exact wording but that key context information is included. This helps us to ensure that the error message is meaningful, but the test is also as robust as possible so that the exact wording could be changed without breaking the tests.

We have an utility util.ContainsAll() which helps here. Check for other tests that use that method.

@dsimansk
Copy link
Copy Markdown
Contributor Author

/retest

Copy link
Copy Markdown
Contributor

@maximilien maximilien left a comment

Choose a reason for hiding this comment

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

Nice, thanks for the contribution 👍 letting others chime in and approve

LGTM

}

// -- 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 :)

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 11, 2020
@dsimansk dsimansk force-pushed the issue-533 branch 2 times, most recently from cb1962f to 253cdce Compare February 12, 2020 07:30
@knative-metrics-robot
Copy link
Copy Markdown

The following is the coverage report on the affected files.
Say /test pull-knative-client-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/kn/commands/service/configuration_edit_flags.go 83.0% 83.5% 0.5

Copy link
Copy Markdown
Contributor

@rhuss rhuss left a comment

Choose a reason for hiding this comment

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

Thanks !

/lgtm

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Feb 12, 2020
@knative-prow-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dsimansk, maximilien, rhuss

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow-robot knative-prow-robot merged commit dd2dc97 into knative:master Feb 12, 2020
dsimansk pushed a commit to dsimansk/client that referenced this pull request Mar 25, 2021
dsimansk added a commit to dsimansk/client that referenced this pull request May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cla: yes Indicates the PR's author has signed the CLA. lgtm Indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Does not reject multiple --image parameters

6 participants