Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
add(func): Add exist built-in function
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Jan 31, 2019
1 parent 59a4e0d commit f3380d0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
19 changes: 19 additions & 0 deletions _examples/manifests/.policy/rules.hcl
Expand Up @@ -36,3 +36,22 @@ rule "yaml_separator" {
message = "YAML separator \"---\" should be removed"
}
}

rule "pdb_defined" {
description = "Check the PDB resouces are defined"

precondition {
cases = [
"${kind("Deployment")}",
]
}

conditions = [
"${length(glob(format("%s/PodDisruptionBudget/*", dirname(dirname(filename))))) > 0}",
]

report {
level = "ERROR"
message = "PDB for this Deployment not found"
}
}
16 changes: 16 additions & 0 deletions docs/syntax/interpolation.md
Expand Up @@ -256,6 +256,22 @@ Usage:
# => "\x1b[31m\x1b[40mhello!\x1b[0m"
```

### `exist(path)`

Returns true if path exists

Types:

- input args: `string`
- return values: `boolean`

Usage:

```hcl
"${exist("/path/to/whatever")}"
# => true (if exists)
```

### `jsonpath(query, default...)`

WIP
Expand Down
6 changes: 1 addition & 5 deletions lint/internal/policy/context.go
Expand Up @@ -17,17 +17,12 @@ import (

// BuildContext is
func (p Policy) BuildContext(body hcl.Body, filename string, filedata []byte) (*hcl.EvalContext, hcl.Diagnostics) {
// var files []cty.Value
// for file := range p.Files {
// files = append(files, cty.StringVal(file))
// }
ctx := &hcl.EvalContext{
Variables: map[string]cty.Value{
"filename": cty.StringVal(filename), // alias of path.filename
"path": cty.ObjectVal(map[string]cty.Value{
"file": cty.StringVal(filename),
"dir": cty.StringVal(filepath.Base(filename)),
// "policies": cty.ListVal(files),
}),
},
Functions: map[string]function.Function{
Expand All @@ -37,6 +32,7 @@ func (p Policy) BuildContext(body hcl.Body, filename string, filedata []byte) (*
"glob": funcs.GlobFunc,
"pathshorten": funcs.PathShortenFunc,
"ext": funcs.ExtFunc,
"exist": funcs.ExistFunc,
// basic
"match": funcs.MatchFunc,
"color": funcs.ColorFunc,
Expand Down
24 changes: 21 additions & 3 deletions lint/internal/policy/funcs/filepath.go
@@ -1,14 +1,15 @@
package funcs

import (
"os"
"path/filepath"

pathshorten "github.com/b4b4r07/go-pathshorten"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)

// GlobFunc is
// GlobFunc returns a list of files matching a given pattern
var GlobFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Expand Down Expand Up @@ -37,7 +38,7 @@ var GlobFunc = function.New(&function.Spec{
},
})

// PathShortenFunc is
// PathShortenFunc returns the shorten path of given path
var PathShortenFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Expand All @@ -52,7 +53,7 @@ var PathShortenFunc = function.New(&function.Spec{
},
})

// ExtFunc is
// ExtFunc returns an extension of given file
var ExtFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Expand All @@ -65,3 +66,20 @@ var ExtFunc = function.New(&function.Spec{
return cty.StringVal(filepath.Ext(args[0].AsString())), nil
},
})

// ExistFunc returns true if given path exists
var ExistFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "path",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.Bool),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
path := args[0].AsString()
_, err = os.Stat(path)
exist := !os.IsNotExist(err)
return cty.BoolVal(exist), nil
},
})

0 comments on commit f3380d0

Please sign in to comment.