-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinit.go
More file actions
48 lines (42 loc) · 1.52 KB
/
init.go
File metadata and controls
48 lines (42 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package preview
import (
"github.com/aquasecurity/trivy/pkg/iac/scanners/terraform/parser/funcs"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
"golang.org/x/xerrors"
"github.com/coder/preview/hclext"
)
// init intends to override some of the default functions afforded by terraform.
// Specifically, any functions that require the context of the host.
//
// This is really unfortunate, but all the functions are globals, and this
// is the only way to override them.
func init() {
// PathExpandFunc looks for references to a home directory on the host. The
// preview rendering should not have access to the host's home directory path,
// and will return an error if it is used.
funcs.PathExpandFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "path",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
// This code is taken directly from https://github.com/mitchellh/go-homedir/blob/af06845cf3004701891bf4fdb884bfe4920b3727/homedir.go#L58
// The only change is that instead of expanding the path, we return an error
path, ok := hclext.AsString(args[0])
if !ok {
return cty.NilVal, xerrors.Errorf("invalid path argument")
}
if len(path) == 0 {
return cty.StringVal(path), nil
}
if path[0] != '~' {
return cty.StringVal(path), nil
}
return cty.NilVal, xerrors.Errorf("not allowed to expand paths starting with '~' in this context")
},
})
}