Skip to content

Commit

Permalink
Allow using default for the default mounts
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Aug 31, 2024
1 parent 38522fa commit f517ff6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ func defaultContainerdArchives() []File {
}
}

func defaultMounts() []Mount {
return []Mount{
{
Location: "~",
Writable: ptr.Of(false),
},
{
Location: "/tmp/lima",
Writable: ptr.Of(true),
},
}
}

// FirstUsernetIndex gets the index of first usernet network under l.Network[]. Returns -1 if no usernet network found.
func FirstUsernetIndex(l *LimaYAML) int {
return slices.IndexFunc(l.Networks, func(network Network) bool { return networks.IsUsernet(network.Lima) })
Expand Down Expand Up @@ -640,6 +653,16 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
}
y.Mounts = mounts

mounts = []Mount{}
for _, mount := range y.Mounts {
if mount.Name == "default" {
mounts = append(mounts, defaultMounts()...)
continue
}
mounts = append(mounts, mount)
}
y.Mounts = mounts

for i := range y.Mounts {
mount := &y.Mounts[i]
if mount.SSHFS.Cache == nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type Disk struct {
}

type Mount struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Location string `yaml:"location" json:"location"` // REQUIRED
MountPoint string `yaml:"mountPoint,omitempty" json:"mountPoint,omitempty"`
Writable *bool `yaml:"writable,omitempty" json:"writable,omitempty"`
Expand Down
21 changes: 21 additions & 0 deletions pkg/limayaml/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import (
yamlv3 "gopkg.in/yaml.v3"
)

func unmarshalMount(dst *Mount, b []byte) error {
var s string
if err := yaml.Unmarshal(b, &s); err == nil {
*dst = Mount{Name: s}
return nil
}
return yaml.Unmarshal(b, dst)
}

func unmarshalDisk(dst *Disk, b []byte) error {
var s string
if err := yaml.Unmarshal(b, &s); err == nil {
Expand All @@ -32,10 +41,22 @@ func unmarshalImage(dst *Image, b []byte) error {
}

var customMarshalers = []yaml.DecodeOption{
yaml.CustomUnmarshaler[Mount](unmarshalMount),
yaml.CustomUnmarshaler[Disk](unmarshalDisk),
yaml.CustomUnmarshaler[Image](unmarshalImage),
}

func (d *Mount) UnmarshalYAML(value *yamlv3.Node) error {
var v interface{}
if err := value.Decode(&v); err != nil {
return err
}
if s, ok := v.(string); ok {
*d = Mount{Name: s}
}
return nil
}

func (d *Disk) UnmarshalYAML(value *yamlv3.Node) error {
var v interface{}
if err := value.Decode(&v); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/limayaml/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ func Validate(y *LimaYAML, warn bool) error {
reservedHome := fmt.Sprintf("/home/%s.linux", u.Username)

for i, f := range y.Mounts {
if f.Name != "" {
if f.Name != "default" {
return fmt.Errorf("field `mounts[%d].name` refers to an unknown name: %q",
i, f.Name)
}
continue
}
if !filepath.IsAbs(f.Location) && !strings.HasPrefix(f.Location, "~") {
return fmt.Errorf("field `mounts[%d].location` must be an absolute path, got %q",
i, f.Location)
Expand Down

0 comments on commit f517ff6

Please sign in to comment.