Skip to content

Commit 0569e70

Browse files
committed
fix: work-around incomplete variable interpolation
1 parent c704472 commit 0569e70

File tree

4 files changed

+52
-14
lines changed

4 files changed

+52
-14
lines changed

common/nix_candidate_source.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"runtime"
77
"strings"
88

9-
"go.spiff.io/expand"
10-
119
g "github.com/zyedidia/generic"
1210
"github.com/zyedidia/generic/hashset"
1311
)
@@ -115,10 +113,8 @@ func (s *NixCandidateSource) crawlPathLists() {
115113

116114
// input is some path definition
117115
func (s *NixCandidateSource) harvestPaths(input string) []string {
118-
input = fixHomeExpansion(input)
119-
input = strings.TrimSpace(input)
120-
input = strings.Trim(input, fmt.Sprintf("%v\"'", os.PathListSeparator))
121-
inputExpanded := expand.Expand(input, func(key string) (value string, ok bool) {
116+
117+
inputExpanded := CustomExpandVariables(input, func(key string) (value string, ok bool) {
122118
switch key {
123119
// do not expand the desired variable itself
124120
case s.key:

common/os_filesystem.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package common
33
import (
44
"os"
55
"path/filepath"
6-
"strings"
76

87
"github.com/mitchellh/go-homedir"
98
"go.spiff.io/expand"
@@ -13,7 +12,7 @@ type OsFilesystem struct{}
1312

1413
func (*OsFilesystem) GetAbsolutePath(path string) string {
1514
// homedir is assumed to work correctly
16-
path = fixHomeExpansion(path)
15+
path = ConvertSimpleVarsToBraces(path)
1716
expandedPath, err := homedir.Expand(path)
1817
if err == nil {
1918
path = expandedPath
@@ -25,12 +24,6 @@ func (*OsFilesystem) GetAbsolutePath(path string) string {
2524
return expand.Expand(path, os.LookupEnv)
2625
}
2726

28-
func fixHomeExpansion(path string) string {
29-
path = strings.ReplaceAll(path, "$HOME/", "~/")
30-
path = strings.ReplaceAll(path, "${HOME}/", "~/")
31-
return path
32-
}
33-
3427
// returns (exists, isDir)
3528
func (f *OsFilesystem) PathStatus(path string) (bool, bool) {
3629
path = f.GetAbsolutePath(path)

common/variable_expander.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package common
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"regexp"
7+
"strings"
8+
9+
"go.spiff.io/expand"
10+
)
11+
12+
func CustomExpandVariables(input string, fn func(key string) (value string, ok bool)) string {
13+
input = ConvertSimpleVarsToBraces(input)
14+
input = strings.TrimSpace(input)
15+
input = strings.Trim(input, fmt.Sprintf("%v\"'", os.PathListSeparator))
16+
return expand.Expand(input, fn)
17+
}
18+
19+
func ConvertSimpleVarsToBraces(input string) string {
20+
r := regexp.MustCompile(`\$([0-9a-zA-Z_]+)`)
21+
return r.ReplaceAllStringFunc(input, func(m string) string {
22+
parts := r.FindStringSubmatch(m)
23+
return fmt.Sprintf(`${%s}`, parts[1])
24+
})
25+
}

common/variable_expander_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package common
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func Test_expanding_with_custom_callback(t *testing.T) {
11+
input := "${VAR1}/$VAR2/bin"
12+
13+
res := CustomExpandVariables(input, func(key string) (string, bool) {
14+
switch key {
15+
case "VAR1":
16+
return "/var1", true
17+
case "VAR2":
18+
return "var2", true
19+
default:
20+
return os.LookupEnv(key)
21+
}
22+
})
23+
assert.Equal(t, "/var1/var2/bin", res)
24+
}

0 commit comments

Comments
 (0)