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
9 changes: 9 additions & 0 deletions pkg/limatmpl/abs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"path/filepath"
"runtime"
"strings"

"github.com/lima-vm/lima/v2/pkg/localpathutil"
)

// UseAbsLocators will replace all relative template locators with absolute ones, so this template
Expand Down Expand Up @@ -105,6 +107,13 @@ func absPath(locator, basePath string) (string, error) {
if err == nil && len(u.Scheme) > 1 {
return locator, nil
}
// Don't expand relative path to absolute. Tilde paths however are absolute paths already.
if localpathutil.IsTildePath(locator) {
locator, err = localpathutil.Expand(locator)
if err != nil {
return "", err
}
}
// Check for rooted locator; filepath.IsAbs() returns false on Windows when the volume name is missing
volumeLen := len(filepath.VolumeName(locator))
if locator[volumeLen] != '/' && locator[volumeLen] != filepath.Separator {
Expand Down
10 changes: 10 additions & 0 deletions pkg/limatmpl/abs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package limatmpl

import (
"os"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -205,6 +206,15 @@ func TestAbsPath(t *testing.T) {
assert.Equal(t, actual, filepath.Clean(volume+"/foo"))
})

t.Run("If the locator starts with ~/, then it will be expanded to an absolute path", func(t *testing.T) {
actual, err := absPath("~/foo", volume+"/root")
assert.NilError(t, err)
homeDir, err := os.UserHomeDir()
assert.NilError(t, err)
// homeDir already includes the volume
assert.Equal(t, actual, filepath.Join(homeDir, "foo"))
})

t.Run("", func(t *testing.T) {
actual, err := absPath("template://foo", volume+"/root")
assert.NilError(t, err)
Expand Down
16 changes: 11 additions & 5 deletions pkg/localpathutil/localpathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
"strings"
)

// IsTildePath returns true if the path is "~" or starts with "~/".
// This means Expand() can expand it with the home directory.
func IsTildePath(path string) bool {
return path == "~" || strings.HasPrefix(path, "~/")
}

// Expand expands a path like "~", "~/", "~/foo".
// Paths like "~foo/bar" are unsupported.
//
Expand All @@ -20,13 +26,13 @@ func Expand(orig string) (string, error) {
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, "~/") {
if IsTildePath(s) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
s = strings.Replace(s, "~", homeDir, 1)
} else {
// Paths like "~foo/bar" are unsupported.
Expand Down