Skip to content

Commit 25f9587

Browse files
authored
Merge pull request docker#6219 from thaJeztah/cleanup_credentialSpecOpt
cli/command/service: credentialSpecOpt: use strings.Cut
2 parents 14ed619 + 5c3577f commit 25f9587

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

cli/command/service/opts.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -335,32 +335,56 @@ type credentialSpecOpt struct {
335335
source string
336336
}
337337

338+
type credentialSpecType string
339+
340+
const (
341+
credentialSpecConfig credentialSpecType = "config"
342+
credentialSpecFile credentialSpecType = "file"
343+
credentialSpecRegistry credentialSpecType = "registry"
344+
)
345+
338346
func (c *credentialSpecOpt) Set(value string) error {
347+
// TODO(thaJeztah): should c.source always be set, even if we may error further down?
339348
c.source = value
340-
c.value = &swarm.CredentialSpec{}
341-
switch {
342-
case strings.HasPrefix(value, "config://"):
349+
if value == "" {
350+
// if the value of the flag is an empty string, that means there is no
351+
// CredentialSpec needed. This is useful for removing a CredentialSpec
352+
// during a service update.
353+
c.value = &swarm.CredentialSpec{}
354+
return nil
355+
}
356+
357+
scheme, val, ok := strings.Cut(value, "://")
358+
if !ok {
359+
scheme = ""
360+
}
361+
switch credentialSpecType(scheme) {
362+
case credentialSpecConfig:
343363
// NOTE(dperny): we allow the user to specify the value of
344364
// CredentialSpec Config using the Name of the config, but the API
345365
// requires the ID of the config. For simplicity, we will parse
346366
// whatever value is provided into the "Config" field, but before
347367
// making API calls, we may need to swap the Config Name for the ID.
348368
// Therefore, this isn't the definitive location for the value of
349369
// Config that is passed to the API.
350-
c.value.Config = strings.TrimPrefix(value, "config://")
351-
case strings.HasPrefix(value, "file://"):
352-
c.value.File = strings.TrimPrefix(value, "file://")
353-
case strings.HasPrefix(value, "registry://"):
354-
c.value.Registry = strings.TrimPrefix(value, "registry://")
355-
case value == "":
356-
// if the value of the flag is an empty string, that means there is no
357-
// CredentialSpec needed. This is useful for removing a CredentialSpec
358-
// during a service update.
370+
c.value = &swarm.CredentialSpec{
371+
Config: val,
372+
}
373+
return nil
374+
case credentialSpecFile:
375+
c.value = &swarm.CredentialSpec{
376+
File: val,
377+
}
378+
return nil
379+
case credentialSpecRegistry:
380+
c.value = &swarm.CredentialSpec{
381+
Registry: val,
382+
}
383+
return nil
359384
default:
385+
c.value = &swarm.CredentialSpec{}
360386
return errors.New(`invalid credential spec: value must be prefixed with "config://", "file://", or "registry://"`)
361387
}
362-
363-
return nil
364388
}
365389

366390
func (*credentialSpecOpt) Type() string {

0 commit comments

Comments
 (0)