From b754114b8739a6a955175b16ba8868fe1e055f73 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 17 Jan 2023 22:44:03 +0100 Subject: [PATCH 1/2] cli/compose/types: ServiceVolumeConfig: use strong type for Type field Using a strong type for this field, to make it clearer what's supported, and to allow assigning values without having to convert them to a string. Also renamed a variable that collided with an import. Signed-off-by: Sebastiaan van Stijn WIP: change type Signed-off-by: Sebastiaan van Stijn --- cli/command/container/opts.go | 2 +- cli/compose/convert/volume.go | 20 ++++++++------------ internal/volumespec/types.go | 4 +++- internal/volumespec/volumespec.go | 8 ++++---- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/cli/command/container/opts.go b/cli/command/container/opts.go index 680a84a8b56c..0e46ad51f42f 100644 --- a/cli/command/container/opts.go +++ b/cli/command/container/opts.go @@ -380,7 +380,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con if parsed.Source != "" { toBind := bind - if parsed.Type == string(mount.TypeBind) { + if parsed.Type == mount.TypeBind { if hostPart, targetPath, ok := strings.Cut(bind, ":"); ok { if !filepath.IsAbs(hostPart) && strings.HasPrefix(hostPart, ".") { if absHostPart, err := filepath.Abs(hostPart); err == nil { diff --git a/cli/compose/convert/volume.go b/cli/compose/convert/volume.go index 8eaa934cc097..3c9827e0438f 100644 --- a/cli/compose/convert/volume.go +++ b/cli/compose/convert/volume.go @@ -26,7 +26,7 @@ func Volumes(serviceVolumes []composetypes.ServiceVolumeConfig, stackVolumes vol func createMountFromVolume(volume composetypes.ServiceVolumeConfig) mount.Mount { return mount.Mount{ - Type: mount.Type(volume.Type), + Type: volume.Type, Target: volume.Target, ReadOnly: volume.ReadOnly, Source: volume.Source, @@ -235,23 +235,19 @@ func handleClusterToMount( return result, nil } -func convertVolumeToMount( - volume composetypes.ServiceVolumeConfig, - stackVolumes volumes, - namespace Namespace, -) (mount.Mount, error) { +func convertVolumeToMount(volume composetypes.ServiceVolumeConfig, stackVolumes volumes, namespace Namespace) (mount.Mount, error) { switch volume.Type { - case "volume", "": + case mount.TypeVolume, "": return handleVolumeToMount(volume, stackVolumes, namespace) - case "image": + case mount.TypeImage: return handleImageToMount(volume) - case "bind": + case mount.TypeBind: return handleBindToMount(volume) - case "tmpfs": + case mount.TypeTmpfs: return handleTmpfsToMount(volume) - case "npipe": + case mount.TypeNamedPipe: return handleNpipeToMount(volume) - case "cluster": + case mount.TypeCluster: return handleClusterToMount(volume, stackVolumes, namespace) } return mount.Mount{}, errors.New("volume type must be volume, bind, tmpfs, npipe, or cluster") diff --git a/internal/volumespec/types.go b/internal/volumespec/types.go index 7eb9a5006ef0..a345ccdbea9c 100644 --- a/internal/volumespec/types.go +++ b/internal/volumespec/types.go @@ -1,8 +1,10 @@ package volumespec +import "github.com/moby/moby/api/types/mount" + // VolumeConfig are references to a volume used by a service type VolumeConfig struct { - Type string `yaml:",omitempty" json:"type,omitempty"` + Type mount.Type `yaml:",omitempty" json:"type,omitempty"` Source string `yaml:",omitempty" json:"source,omitempty"` Target string `yaml:",omitempty" json:"target,omitempty"` ReadOnly bool `mapstructure:"read_only" yaml:"read_only,omitempty" json:"read_only,omitempty"` diff --git a/internal/volumespec/volumespec.go b/internal/volumespec/volumespec.go index 8f497714c83e..bba23e843d9b 100644 --- a/internal/volumespec/volumespec.go +++ b/internal/volumespec/volumespec.go @@ -25,7 +25,7 @@ func Parse(spec string) (VolumeConfig, error) { return volume, errors.New("invalid empty volume spec") case 1, 2: volume.Target = spec - volume.Type = string(mount.TypeVolume) + volume.Type = mount.TypeVolume return volume, nil } @@ -97,11 +97,11 @@ func populateType(volume *VolumeConfig) { switch { // Anonymous volume case volume.Source == "": - volume.Type = string(mount.TypeVolume) + volume.Type = mount.TypeVolume case isFilePath(volume.Source): - volume.Type = string(mount.TypeBind) + volume.Type = mount.TypeBind default: - volume.Type = string(mount.TypeVolume) + volume.Type = mount.TypeVolume } } From ebdd5fe48813db9e9c11bb31f18019a5628c5071 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 12 Apr 2023 15:51:46 +0200 Subject: [PATCH 2/2] cli/compose/types: ServiceVolumeConfig: use strong type for Consistency field Using a strong type for this field, to make it clearer what's supported, and to allow assigning values without having to cast them. Signed-off-by: Sebastiaan van Stijn --- cli/compose/convert/volume.go | 2 +- internal/volumespec/types.go | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cli/compose/convert/volume.go b/cli/compose/convert/volume.go index 3c9827e0438f..c810fd1da94e 100644 --- a/cli/compose/convert/volume.go +++ b/cli/compose/convert/volume.go @@ -30,7 +30,7 @@ func createMountFromVolume(volume composetypes.ServiceVolumeConfig) mount.Mount Target: volume.Target, ReadOnly: volume.ReadOnly, Source: volume.Source, - Consistency: mount.Consistency(volume.Consistency), + Consistency: volume.Consistency, } } diff --git a/internal/volumespec/types.go b/internal/volumespec/types.go index a345ccdbea9c..89168ea0217f 100644 --- a/internal/volumespec/types.go +++ b/internal/volumespec/types.go @@ -4,16 +4,16 @@ import "github.com/moby/moby/api/types/mount" // VolumeConfig are references to a volume used by a service type VolumeConfig struct { - Type mount.Type `yaml:",omitempty" json:"type,omitempty"` - Source string `yaml:",omitempty" json:"source,omitempty"` - Target string `yaml:",omitempty" json:"target,omitempty"` - ReadOnly bool `mapstructure:"read_only" yaml:"read_only,omitempty" json:"read_only,omitempty"` - Consistency string `yaml:",omitempty" json:"consistency,omitempty"` - Bind *BindOpts `yaml:",omitempty" json:"bind,omitempty"` - Volume *VolumeOpts `yaml:",omitempty" json:"volume,omitempty"` - Image *ImageOpts `yaml:",omitempty" json:"image,omitempty"` - Tmpfs *TmpFsOpts `yaml:",omitempty" json:"tmpfs,omitempty"` - Cluster *ClusterOpts `yaml:",omitempty" json:"cluster,omitempty"` + Type mount.Type `yaml:",omitempty" json:"type,omitempty"` + Source string `yaml:",omitempty" json:"source,omitempty"` + Target string `yaml:",omitempty" json:"target,omitempty"` + ReadOnly bool `mapstructure:"read_only" yaml:"read_only,omitempty" json:"read_only,omitempty"` + Consistency mount.Consistency `yaml:",omitempty" json:"consistency,omitempty"` + Bind *BindOpts `yaml:",omitempty" json:"bind,omitempty"` + Volume *VolumeOpts `yaml:",omitempty" json:"volume,omitempty"` + Image *ImageOpts `yaml:",omitempty" json:"image,omitempty"` + Tmpfs *TmpFsOpts `yaml:",omitempty" json:"tmpfs,omitempty"` + Cluster *ClusterOpts `yaml:",omitempty" json:"cluster,omitempty"` } // BindOpts are options for a service volume of type bind