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:
parent
820b79d91d
commit
769f626a0e
|
@ -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.
|
||||||
|
|
Loading…
Reference in New Issue