Skip to content

Commit 99c4922

Browse files
committed
Expand tilde using the current home path
For instance it might be C:\Users\anders on the host, but translated to /c/Users/anders path in the guest. Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
1 parent b53e7df commit 99c4922

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

pkg/cidata/cidata.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
134134
}
135135
for i, f := range y.Mounts {
136136
tag := fmt.Sprintf("mount%d", i)
137-
location, err := localpathutil.Expand(f.Location)
137+
location, err := localpathutil.ExpandHome(f.Location, hostHome)
138138
if err != nil {
139139
return err
140140
}
141-
mountPoint, err := localpathutil.Expand(f.MountPoint)
141+
mountPoint, err := localpathutil.ExpandHome(f.MountPoint, u.HomeDir)
142142
if err != nil {
143143
return err
144144
}

pkg/hostagent/mount.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/lima-vm/lima/pkg/limayaml"
1111
"github.com/lima-vm/lima/pkg/localpathutil"
12+
"github.com/lima-vm/lima/pkg/osutil"
1213
"github.com/lima-vm/sshocker/pkg/reversesshfs"
1314
"github.com/sirupsen/logrus"
1415
)
@@ -39,7 +40,11 @@ func (a *HostAgent) setupMount(ctx context.Context, m limayaml.Mount) (*mount, e
3940
return nil, err
4041
}
4142

42-
mountPoint, err := localpathutil.Expand(m.MountPoint)
43+
u, err := osutil.LimaUser(false)
44+
if err != nil {
45+
return nil, err
46+
}
47+
mountPoint, err := localpathutil.ExpandHome(m.MountPoint, u.HomeDir)
4348
if err != nil {
4449
return nil, err
4550
}

pkg/localpathutil/localpathutil.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@ import (
1212
// Paths like "~foo/bar" are unsupported.
1313
//
1414
// FIXME: is there an existing library for this?
15-
func Expand(orig string) (string, error) {
15+
func ExpandHome(orig string, homeDir string) (string, error) {
1616
s := orig
1717
if s == "" {
1818
return "", errors.New("empty path")
1919
}
20-
homeDir, err := os.UserHomeDir()
21-
if err != nil {
22-
return "", err
23-
}
2420

2521
if strings.HasPrefix(s, "~") {
2622
if s == "~" || strings.HasPrefix(s, "~/") {
@@ -30,5 +26,18 @@ func Expand(orig string) (string, error) {
3026
return "", fmt.Errorf("unexpandable path %q", orig)
3127
}
3228
}
29+
return s, nil
30+
}
31+
32+
// Expand expands a path like "~", "~/", "~/foo", on the host.
33+
func Expand(orig string) (string, error) {
34+
homeDir, err := os.UserHomeDir()
35+
if err != nil {
36+
return "", err
37+
}
38+
s, err := ExpandHome(orig, homeDir)
39+
if err != nil {
40+
return "", err
41+
}
3342
return filepath.Abs(s)
3443
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package localpathutil
2+
3+
import (
4+
"testing"
5+
"path/filepath"
6+
7+
"gotest.tools/v3/assert"
8+
)
9+
10+
func TestExpandTilde(t *testing.T) {
11+
_, err := Expand("~")
12+
assert.NilError(t, err)
13+
}
14+
15+
func TestExpandDir(t *testing.T) {
16+
h, err := Expand("~")
17+
assert.NilError(t, err)
18+
d, err := Expand("~/foo")
19+
assert.NilError(t, err)
20+
// make sure this uses the local filepath
21+
assert.Equal(t, d, filepath.Join(h, "foo"))
22+
}
23+
24+
func TestExpandHome(t *testing.T) {
25+
p, err := ExpandHome("~/bar", "/home/user")
26+
assert.NilError(t, err)
27+
// make sure this uses the unix path
28+
assert.Equal(t, p, "/home/user/bar")
29+
}

0 commit comments

Comments
 (0)