Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 51 additions & 13 deletions cmd/limactl/editflags/editflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func RegisterEdit(cmd *cobra.Command, commentPrefix string) {
})

flags.StringSlice("mount", nil, commentPrefix+"Directories to mount, suffix ':w' for writable (Do not specify directories that overlap with the existing mounts)") // colima-compatible
flags.StringSlice("mount-only", nil, commentPrefix+"Similar to --mount, but overrides the existing mounts")

flags.Bool("mount-none", false, commentPrefix+"Remove all mounts")

flags.String("mount-type", "", commentPrefix+"Mount type (reverse-sshfs, 9p, virtiofs)") // Similar to colima's --mount-type=(sshfs|9p|virtiofs), but "reverse-sshfs" is Lima is called "sshfs" in colima
Expand Down Expand Up @@ -167,6 +169,20 @@ func BuildPortForwardExpression(portForwards []string) (string, error) {
return expr, nil
}

func buildMountListExpression(ss []string) (string, error) {
expr := `[`
for i, s := range ss {
writable := strings.HasSuffix(s, ":w")
loc := strings.TrimSuffix(s, ":w")
expr += fmt.Sprintf(`{"location": %q, "writable": %v}`, loc, writable)
if i < len(ss)-1 {
expr += ","
}
}
expr += `]`
return expr, nil
}

// YQExpressions returns YQ expressions.
func YQExpressions(flags *flag.FlagSet, newInstance bool) ([]string, error) {
type def struct {
Expand Down Expand Up @@ -207,30 +223,52 @@ func YQExpressions(flags *flag.FlagSet, newInstance bool) ([]string, error) {
if err != nil {
return "", err
}
expr := `.mounts += [`
for i, s := range ss {
writable := strings.HasSuffix(s, ":w")
loc := strings.TrimSuffix(s, ":w")
expr += fmt.Sprintf(`{"location": %q, "writable": %v}`, loc, writable)
if i < len(ss)-1 {
expr += ","
}
mountListExpr, err := buildMountListExpression(ss)
if err != nil {
return "", err
}
expr := `.mounts += ` + mountListExpr + ` | .mounts |= unique_by(.location)`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is just existing code that is being moved, but the logic is incorrect: the mounts need to be unique by .mountPoint, not by .location. The same location can be mounted to several mount points, and this logic would delete all but one of them.

But the mount point may only be filled in later (defaults to location), so we can't do anything here beyond making sure that the new mount points are defined first, so they will overrride existing mounts (mostly relevant only for the writable property).

I think this would be better (but have not tested it):

Suggested change
expr := `.mounts += ` + mountListExpr + ` | .mounts |= unique_by(.location)`
// mount options take precedence over template settings
expr := fmt.Sprintf(".mounts = %s + .mounts", mountListExpr)

Could also open a new issue to address later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mountOnly, err := flags.GetStringSlice("mount-only")
if err != nil {
return "", err
}
if len(mountOnly) > 0 {
return "", errors.New("flag `--mount` conflicts with `--mount-only`")
}
expr += `] | .mounts |= unique_by(.location)`
return expr, nil
},
false,
false,
},
{
"mount-none",
"mount-only",
func(_ *flag.Flag) (string, error) {
ss, err := flags.GetStringSlice("mount")
ss, err := flags.GetStringSlice("mount-only")
if err != nil {
return "", err
}
mountListExpr, err := buildMountListExpression(ss)
if err != nil {
return "", err
}
if len(ss) > 0 {
return "", errors.New("flag `--mount-none` conflicts with `--mount`")
expr := `.mounts = ` + mountListExpr
return expr, nil
},
false,
false,
},
{
"mount-none",
func(_ *flag.Flag) (string, error) {
incompatibleFlagNames := []string{"mount", "mount-only"}
for _, name := range incompatibleFlagNames {
ss, err := flags.GetStringSlice(name)
if err != nil {
return "", err
}
if len(ss) > 0 {
return "", errors.New("flag `--mount-none` conflicts with `" + name + "`")
}
Comment on lines +262 to +271
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could potentially be a shared helper function because we have other subcommands that have mutually exclusive options.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can add a helper when we have move conflicting options

}
return ".mounts = null", nil
},
Expand Down
44 changes: 44 additions & 0 deletions cmd/limactl/editflags/editflags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package editflags
import (
"testing"

"github.com/spf13/cobra"
"gotest.tools/v3/assert"
)

Expand Down Expand Up @@ -157,3 +158,46 @@ func TestParsePortForward(t *testing.T) {
})
}
}

func TestYQExpressions(t *testing.T) {
tests := []struct {
name string
args []string
newInstance bool
expected []string
expectError string
}{
{
name: "mount",
args: []string{"--mount", "/foo", "--mount", "/bar:w"},
newInstance: false,
expected: []string{`.mounts += [{"location": "/foo", "writable": false},{"location": "/bar", "writable": true}] | .mounts |= unique_by(.location)`},
},
{
name: "mount-only",
args: []string{"--mount-only", "/foo", "--mount-only", "/bar:w"},
newInstance: false,
expected: []string{`.mounts = [{"location": "/foo", "writable": false},{"location": "/bar", "writable": true}]`},
},
{
name: "mixture of mount and mount-only",
args: []string{"--mount", "/foo", "--mount-only", "/bar:w"},
newInstance: false,
expectError: "flag `--mount` conflicts with `--mount-only`",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := &cobra.Command{}
RegisterEdit(cmd, "")
assert.NilError(t, cmd.ParseFlags(tt.args))
expr, err := YQExpressions(cmd.Flags(), tt.newInstance)
if tt.expectError != "" {
assert.ErrorContains(t, err, tt.expectError)
} else {
assert.NilError(t, err)
assert.DeepEqual(t, tt.expected, expr)
}
})
}
}
Loading