@@ -335,32 +335,56 @@ type credentialSpecOpt struct {
335
335
source string
336
336
}
337
337
338
+ type credentialSpecType string
339
+
340
+ const (
341
+ credentialSpecConfig credentialSpecType = "config"
342
+ credentialSpecFile credentialSpecType = "file"
343
+ credentialSpecRegistry credentialSpecType = "registry"
344
+ )
345
+
338
346
func (c * credentialSpecOpt ) Set (value string ) error {
347
+ // TODO(thaJeztah): should c.source always be set, even if we may error further down?
339
348
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 :
343
363
// NOTE(dperny): we allow the user to specify the value of
344
364
// CredentialSpec Config using the Name of the config, but the API
345
365
// requires the ID of the config. For simplicity, we will parse
346
366
// whatever value is provided into the "Config" field, but before
347
367
// making API calls, we may need to swap the Config Name for the ID.
348
368
// Therefore, this isn't the definitive location for the value of
349
369
// 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
359
384
default :
385
+ c .value = & swarm.CredentialSpec {}
360
386
return errors .New (`invalid credential spec: value must be prefixed with "config://", "file://", or "registry://"` )
361
387
}
362
-
363
- return nil
364
388
}
365
389
366
390
func (* credentialSpecOpt ) Type () string {
0 commit comments