Skip to content

Commit c8637f7

Browse files
authored
Merge pull request #248 from ndeloof/volume_stringer
introduce ServiceVolumeConfig.String()
2 parents ef73f5a + 11d5aa1 commit c8637f7

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

loader/volume_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,18 @@ func TestParseVolumeInvalidSections(t *testing.T) {
280280
_, err := ParseVolume("/foo::rw")
281281
assert.ErrorContains(t, err, "invalid spec")
282282
}
283+
284+
func TestVolumeStringer(t *testing.T) {
285+
v := types.ServiceVolumeConfig{
286+
Type: "bind",
287+
Source: "/src",
288+
Target: "/target",
289+
ReadOnly: false,
290+
Bind: &types.ServiceVolumeBind{
291+
CreateHostPath: true,
292+
Propagation: types.PropagationShared,
293+
SELinux: types.SELinuxShared,
294+
},
295+
}
296+
assert.Equal(t, v.String(), "/src:/target:rw,z,shared")
297+
}

types/types.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -291,19 +291,19 @@ func (s set) toSlice() []string {
291291

292292
// BuildConfig is a type for build
293293
type BuildConfig struct {
294-
Context string `yaml:",omitempty" json:"context,omitempty"`
295-
Dockerfile string `yaml:",omitempty" json:"dockerfile,omitempty"`
296-
Args MappingWithEquals `yaml:",omitempty" json:"args,omitempty"`
297-
SSH SSHConfig `yaml:"ssh,omitempty" json:"ssh,omitempty"`
298-
Labels Labels `yaml:",omitempty" json:"labels,omitempty"`
299-
CacheFrom StringList `mapstructure:"cache_from" yaml:"cache_from,omitempty" json:"cache_from,omitempty"`
300-
CacheTo StringList `mapstructure:"cache_to" yaml:"cache_to,omitempty" json:"cache_to,omitempty"`
301-
NoCache bool `mapstructure:"no_cache" yaml:"no_cache,omitempty" json:"no_cache,omitempty"`
302-
Pull bool `mapstructure:"pull" yaml:"pull,omitempty" json:"pull,omitempty"`
303-
ExtraHosts HostsList `mapstructure:"extra_hosts" yaml:"extra_hosts,omitempty" json:"extra_hosts,omitempty"`
304-
Isolation string `yaml:",omitempty" json:"isolation,omitempty"`
305-
Network string `yaml:",omitempty" json:"network,omitempty"`
306-
Target string `yaml:",omitempty" json:"target,omitempty"`
294+
Context string `yaml:",omitempty" json:"context,omitempty"`
295+
Dockerfile string `yaml:",omitempty" json:"dockerfile,omitempty"`
296+
Args MappingWithEquals `yaml:",omitempty" json:"args,omitempty"`
297+
SSH SSHConfig `yaml:"ssh,omitempty" json:"ssh,omitempty"`
298+
Labels Labels `yaml:",omitempty" json:"labels,omitempty"`
299+
CacheFrom StringList `mapstructure:"cache_from" yaml:"cache_from,omitempty" json:"cache_from,omitempty"`
300+
CacheTo StringList `mapstructure:"cache_to" yaml:"cache_to,omitempty" json:"cache_to,omitempty"`
301+
NoCache bool `mapstructure:"no_cache" yaml:"no_cache,omitempty" json:"no_cache,omitempty"`
302+
Pull bool `mapstructure:"pull" yaml:"pull,omitempty" json:"pull,omitempty"`
303+
ExtraHosts HostsList `mapstructure:"extra_hosts" yaml:"extra_hosts,omitempty" json:"extra_hosts,omitempty"`
304+
Isolation string `yaml:",omitempty" json:"isolation,omitempty"`
305+
Network string `yaml:",omitempty" json:"network,omitempty"`
306+
Target string `yaml:",omitempty" json:"target,omitempty"`
307307
Secrets []ServiceSecretConfig `yaml:",omitempty" json:"secrets,omitempty"`
308308

309309
Extensions map[string]interface{} `yaml:",inline" json:"-"`
@@ -679,6 +679,25 @@ type ServiceVolumeConfig struct {
679679
Extensions map[string]interface{} `yaml:",inline" json:"-"`
680680
}
681681

682+
// String render ServiceVolumeConfig as a volume string, one can parse back using loader.ParseVolume
683+
func (s ServiceVolumeConfig) String() string {
684+
access := "rw"
685+
if s.ReadOnly {
686+
access = "ro"
687+
}
688+
options := []string{access}
689+
if s.Bind != nil && s.Bind.SELinux != "" {
690+
options = append(options, s.Bind.SELinux)
691+
}
692+
if s.Bind != nil && s.Bind.Propagation != "" {
693+
options = append(options, s.Bind.Propagation)
694+
}
695+
if s.Volume != nil && s.Volume.NoCopy {
696+
options = append(options, "nocopy")
697+
}
698+
return fmt.Sprintf("%s:%s:%s", s.Source, s.Target, strings.Join(options, ","))
699+
}
700+
682701
const (
683702
// VolumeTypeBind is the type for mounting host dir
684703
VolumeTypeBind = "bind"

0 commit comments

Comments
 (0)