cli/command/service: credentialSpecOpt: use strings.Cut#6219
cli/command/service: credentialSpecOpt: use strings.Cut#6219thaJeztah merged 1 commit intodocker:masterfrom
Conversation
- Rewrite the function to use strings.Cut instead of checking for, and trimming prefixes for each option. - More explicitly set the value, instead of setting an empty value, then propagating the struct. - Define a "type" to provide a more enum-like construct. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| // TODO(thaJeztah): should c.source always be set, even if we may error further down? | ||
| c.source = value |
There was a problem hiding this comment.
This was existing behavior, but felt a bit "odd" to set the value regardless if it was valid.
There was a problem hiding this comment.
Looking at go stdlib flags; the Set validates before setting the value (c.source is what's returned by c.String() for us); https://github.com/golang/go/blob/7a1679d7ae32dd8a01bd355413ee77ba517f5f43/src/flag/flag.go#L221-L233
func newUint64Value(val uint64, p *uint64) *uint64Value {
*p = val
return (*uint64Value)(p)
}
func (i *uint64Value) Set(s string) error {
v, err := strconv.ParseUint(s, 0, 64)
if err != nil {
err = numError(err)
}
*i = uint64Value(v)
return err
}spf13/pflags also sets the value regardless (although the function used may return a default value);
cli/vendor/github.com/spf13/pflag/uint64.go
Lines 8 to 23 in 6686ada
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
| c.value = &swarm.CredentialSpec{} | ||
| return errors.New(`invalid credential spec: value must be prefixed with "config://", "file://", or "registry://"`) |
There was a problem hiding this comment.
This was also the previous behavior (where the value would be set to an empty struct regardless), but not sure if the value is used if an error is returned, so perhaps either skipping, or setting it to nil could work?
- Human readable description for the release notes
- A picture of a cute animal (not mandatory but encouraged)