github.com/moby/docker@v26.1.3+incompatible/volume/service/opts/opts.go (about)

     1  package opts
     2  
     3  // CreateOption is used to pass options in when creating a volume
     4  type CreateOption func(*CreateConfig)
     5  
     6  // CreateConfig is the set of config options that can be set when creating
     7  // a volume
     8  type CreateConfig struct {
     9  	Options   map[string]string
    10  	Labels    map[string]string
    11  	Reference string
    12  }
    13  
    14  // WithCreateLabel creates a CreateOption which adds a label with the given key/value pair
    15  func WithCreateLabel(key, value string) CreateOption {
    16  	return func(cfg *CreateConfig) {
    17  		if cfg.Labels == nil {
    18  			cfg.Labels = map[string]string{}
    19  		}
    20  		cfg.Labels[key] = value
    21  	}
    22  }
    23  
    24  // WithCreateLabels creates a CreateOption which sets the labels to the
    25  // passed in value
    26  func WithCreateLabels(labels map[string]string) CreateOption {
    27  	return func(cfg *CreateConfig) {
    28  		cfg.Labels = labels
    29  	}
    30  }
    31  
    32  // WithCreateOptions creates a CreateOption which sets the options passed
    33  // to the volume driver when creating a volume to the options passed in.
    34  func WithCreateOptions(opts map[string]string) CreateOption {
    35  	return func(cfg *CreateConfig) {
    36  		cfg.Options = opts
    37  	}
    38  }
    39  
    40  // WithCreateReference creats a CreateOption which sets a reference to use
    41  // when creating a volume. This ensures that the volume is created with a reference
    42  // already attached to it to prevent race conditions with Create and volume cleanup.
    43  func WithCreateReference(ref string) CreateOption {
    44  	return func(cfg *CreateConfig) {
    45  		cfg.Reference = ref
    46  	}
    47  }
    48  
    49  // GetConfig is used with `GetOption` to set options for the volumes service's
    50  // `Get` implementation.
    51  type GetConfig struct {
    52  	Driver        string
    53  	Reference     string
    54  	ResolveStatus bool
    55  }
    56  
    57  // GetOption is passed to the service `Get` add extra details on the get request
    58  type GetOption func(*GetConfig)
    59  
    60  // WithGetDriver provides the driver to get the volume from
    61  // If no driver is provided to `Get`, first the available metadata is checked
    62  // to see which driver it belongs to, if that is not available all drivers are
    63  // probed to find the volume.
    64  func WithGetDriver(name string) GetOption {
    65  	return func(o *GetConfig) {
    66  		o.Driver = name
    67  	}
    68  }
    69  
    70  // WithGetReference indicates to `Get` to increment the reference count for the
    71  // retrieved volume with the provided reference ID.
    72  func WithGetReference(ref string) GetOption {
    73  	return func(o *GetConfig) {
    74  		o.Reference = ref
    75  	}
    76  }
    77  
    78  // WithGetResolveStatus indicates to `Get` to also fetch the volume status.
    79  // This can cause significant overhead in the volume lookup.
    80  func WithGetResolveStatus(cfg *GetConfig) {
    81  	cfg.ResolveStatus = true
    82  }
    83  
    84  // RemoveConfig is used by `RemoveOption` to store config options for remove
    85  type RemoveConfig struct {
    86  	PurgeOnError bool
    87  }
    88  
    89  // RemoveOption is used to pass options to the volumes service `Remove` implementation
    90  type RemoveOption func(*RemoveConfig)
    91  
    92  // WithPurgeOnError is an option passed to `Remove` which will purge all cached
    93  // data about a volume even if there was an error while attempting to remove the
    94  // volume.
    95  func WithPurgeOnError(b bool) RemoveOption {
    96  	return func(o *RemoveConfig) {
    97  		o.PurgeOnError = b
    98  	}
    99  }