Skip to content

Expand tilde using the current home path #954

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions pkg/cidata/cidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
}
for i, f := range y.Mounts {
tag := fmt.Sprintf("mount%d", i)
location, err := localpathutil.Expand(f.Location)
location, err := localpathutil.ExpandHome(f.Location, hostHome)
if err != nil {
return err
}
mountPoint, err := localpathutil.Expand(f.MountPoint)
mountPoint, err := localpathutil.ExpandHome(f.MountPoint, u.HomeDir)
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/hostagent/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/localpathutil"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/sshocker/pkg/reversesshfs"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -39,7 +40,11 @@ func (a *HostAgent) setupMount(ctx context.Context, m limayaml.Mount) (*mount, e
return nil, err
}

mountPoint, err := localpathutil.Expand(m.MountPoint)
u, err := osutil.LimaUser(false)
if err != nil {
return nil, err
}
mountPoint, err := localpathutil.ExpandHome(m.MountPoint, u.HomeDir)
if err != nil {
return nil, err
}
Expand Down
19 changes: 14 additions & 5 deletions pkg/localpathutil/localpathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ import (
// Paths like "~foo/bar" are unsupported.
//
// FIXME: is there an existing library for this?
func Expand(orig string) (string, error) {
func ExpandHome(orig string, homeDir string) (string, error) {
s := orig
if s == "" {
return "", errors.New("empty path")
}
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}

if strings.HasPrefix(s, "~") {
if s == "~" || strings.HasPrefix(s, "~/") {
Expand All @@ -30,5 +26,18 @@ func Expand(orig string) (string, error) {
return "", fmt.Errorf("unexpandable path %q", orig)
}
}
return s, nil
}

// Expand expands a path like "~", "~/", "~/foo", on the host.
func Expand(orig string) (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
s, err := ExpandHome(orig, homeDir)
if err != nil {
return "", err
}
return filepath.Abs(s)
}
29 changes: 29 additions & 0 deletions pkg/localpathutil/localpathutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package localpathutil

import (
"path/filepath"
"testing"

"gotest.tools/v3/assert"
)

func TestExpandTilde(t *testing.T) {
_, err := Expand("~")
assert.NilError(t, err)
}

func TestExpandDir(t *testing.T) {
h, err := Expand("~")
assert.NilError(t, err)
d, err := Expand("~/foo")
assert.NilError(t, err)
// make sure this uses the local filepath
assert.Equal(t, d, filepath.Join(h, "foo"))
}

func TestExpandHome(t *testing.T) {
p, err := ExpandHome("~/bar", "/home/user")
assert.NilError(t, err)
// make sure this uses the unix path
assert.Equal(t, p, "/home/user/bar")
}