Fix --image flag to only allow single occurence#647
Conversation
knative-prow-robot
left a comment
There was a problem hiding this comment.
@dsimansk: 0 warnings.
Details
In response to this:
Fixes #533
Proposed Changes
- Introduce new type
singletonStringfor flag values that only allow single value assignment, otherwise return error- Fix
--imageflag 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.
rhuss
left a comment
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Maybe a bit more descriptive error message to explain what is wrong with value already set?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
rhuss
left a comment
There was a problem hiding this comment.
tests are looking good with some minor remark how they could be improved.
| 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") |
There was a problem hiding this comment.
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.
|
/retest |
maximilien
left a comment
There was a problem hiding this comment.
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. |
cb1962f to
253cdce
Compare
|
The following is the coverage report on the affected files.
|
|
[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 DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Fixes #533
Proposed Changes
singletonStringfor flag values that only allow single value assignment, otherwise return error--imageflag with the new type.Example: