lang/funcs: Remove homedir.Expand() and refactor path trimming with filepath.Rel() in fileset() function

Reference: https://github.com/hashicorp/terraform/pull/22621#pullrequestreview-282259385
This commit is contained in:
Brian Flad 2019-08-30 19:41:47 -04:00
parent 820b79d91d
commit 769f626a0e
No known key found for this signature in database
GPG Key ID: EC6252B42B012823
1 changed files with 5 additions and 7 deletions

View File

@ -6,7 +6,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"unicode/utf8" "unicode/utf8"
"github.com/hashicorp/hcl2/hcl" "github.com/hashicorp/hcl2/hcl"
@ -227,11 +226,6 @@ func MakeFileSetFunc(baseDir string) function.Function {
path := args[0].AsString() path := args[0].AsString()
pattern := args[1].AsString() pattern := args[1].AsString()
path, err := homedir.Expand(path)
if err != nil {
return cty.UnknownVal(cty.Set(cty.String)), fmt.Errorf("failed to expand ~: %s", err)
}
if !filepath.IsAbs(path) { if !filepath.IsAbs(path) {
path = filepath.Join(baseDir, path) path = filepath.Join(baseDir, path)
} }
@ -259,7 +253,11 @@ func MakeFileSetFunc(baseDir string) function.Function {
} }
// Remove the path and file separator from matches. // Remove the path and file separator from matches.
match = strings.TrimPrefix(match, path+string(filepath.Separator)) match, err = filepath.Rel(path, match)
if err != nil {
return cty.UnknownVal(cty.Set(cty.String)), fmt.Errorf("failed to trim path of match (%s): %s", match, err)
}
// Replace any remaining file separators with forward slash (/) // Replace any remaining file separators with forward slash (/)
// separators for cross-system compatibility. // separators for cross-system compatibility.