Skip to content

Commit

Permalink
fix: work-around incomplete variable interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
d-led committed Dec 31, 2023
1 parent c704472 commit 0569e70
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
8 changes: 2 additions & 6 deletions common/nix_candidate_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"runtime"
"strings"

"go.spiff.io/expand"

g "github.com/zyedidia/generic"
"github.com/zyedidia/generic/hashset"
)
Expand Down Expand Up @@ -115,10 +113,8 @@ func (s *NixCandidateSource) crawlPathLists() {

// input is some path definition
func (s *NixCandidateSource) harvestPaths(input string) []string {
input = fixHomeExpansion(input)
input = strings.TrimSpace(input)
input = strings.Trim(input, fmt.Sprintf("%v\"'", os.PathListSeparator))
inputExpanded := expand.Expand(input, func(key string) (value string, ok bool) {

inputExpanded := CustomExpandVariables(input, func(key string) (value string, ok bool) {
switch key {
// do not expand the desired variable itself
case s.key:
Expand Down
9 changes: 1 addition & 8 deletions common/os_filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package common
import (
"os"
"path/filepath"
"strings"

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

func (*OsFilesystem) GetAbsolutePath(path string) string {
// homedir is assumed to work correctly
path = fixHomeExpansion(path)
path = ConvertSimpleVarsToBraces(path)
expandedPath, err := homedir.Expand(path)
if err == nil {
path = expandedPath
Expand All @@ -25,12 +24,6 @@ func (*OsFilesystem) GetAbsolutePath(path string) string {
return expand.Expand(path, os.LookupEnv)
}

func fixHomeExpansion(path string) string {
path = strings.ReplaceAll(path, "$HOME/", "~/")
path = strings.ReplaceAll(path, "${HOME}/", "~/")
return path
}

// returns (exists, isDir)
func (f *OsFilesystem) PathStatus(path string) (bool, bool) {
path = f.GetAbsolutePath(path)
Expand Down
25 changes: 25 additions & 0 deletions common/variable_expander.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package common

import (
"fmt"
"os"
"regexp"
"strings"

"go.spiff.io/expand"
)

func CustomExpandVariables(input string, fn func(key string) (value string, ok bool)) string {
input = ConvertSimpleVarsToBraces(input)
input = strings.TrimSpace(input)
input = strings.Trim(input, fmt.Sprintf("%v\"'", os.PathListSeparator))
return expand.Expand(input, fn)
}

func ConvertSimpleVarsToBraces(input string) string {
r := regexp.MustCompile(`\$([0-9a-zA-Z_]+)`)
return r.ReplaceAllStringFunc(input, func(m string) string {
parts := r.FindStringSubmatch(m)
return fmt.Sprintf(`${%s}`, parts[1])
})
}
24 changes: 24 additions & 0 deletions common/variable_expander_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package common

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_expanding_with_custom_callback(t *testing.T) {
input := "${VAR1}/$VAR2/bin"

res := CustomExpandVariables(input, func(key string) (string, bool) {
switch key {
case "VAR1":
return "/var1", true
case "VAR2":
return "var2", true
default:
return os.LookupEnv(key)
}
})
assert.Equal(t, "/var1/var2/bin", res)
}

0 comments on commit 0569e70

Please sign in to comment.