-
Notifications
You must be signed in to change notification settings - Fork 719
editflags: add --mount-only flag
#3947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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 { | ||
|
|
@@ -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)` | ||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| }, | ||
|
|
||
There was a problem hiding this comment.
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
writableproperty).I think this would be better (but have not tested it):
Could also open a new issue to address later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--mount:.mounts |= unique_by(.location)seems incorrect #3959