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 Jun 16, 2024
1 parent 491da12 commit f0761d3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,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 @@ -633,6 +646,17 @@ 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
} else {

Check failure on line 654 in pkg/limayaml/defaults.go

View workflow job for this annotation

GitHub Actions / Lints

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)
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 @@ -111,6 +111,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 @@ -133,6 +133,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 f0761d3

Please sign in to comment.