Skip to content

Commit

Permalink
add __file__ and __dir__ attributes to resource properties for file l…
Browse files Browse the repository at this point in the history
…inters
  • Loading branch information
Larry Hitchon committed Jul 23, 2018
1 parent 8a59f38 commit f0592ea
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
11 changes: 7 additions & 4 deletions linter/kubernetes.go
Expand Up @@ -2,6 +2,7 @@ package linter

import (
"github.com/stelligent/config-lint/assertion"
"path/filepath"
)

// KubernetesLinter lints resources in Kubernets YAML files
Expand Down Expand Up @@ -32,17 +33,19 @@ func (l KubernetesResourceLoader) Load(filename string) (FileResources, error) {
return loaded, err
}
for _, resource := range yamlResources {
m := resource.(map[string]interface{})
properties := resource.(map[string]interface{})
var resourceID string
if name, ok := getResourceIDFromMetadata(m); ok {
if name, ok := getResourceIDFromMetadata(properties); ok {
resourceID = name
} else {
resourceID = getResourceIDFromFilename(filename)
}
properties["__file__"] = filename
properties["__dir__"] = filepath.Dir(filename)
kr := assertion.Resource{
ID: resourceID,
Type: m["kind"].(string),
Properties: m,
Type: properties["kind"].(string),
Properties: properties,
Filename: filename,
}
loaded.Resources = append(loaded.Resources, kr)
Expand Down
19 changes: 19 additions & 0 deletions linter/kubernetes_test.go
@@ -0,0 +1,19 @@
package linter

import (
"github.com/stretchr/testify/assert"
"path/filepath"
"testing"
)

func TestKubernetesSpecialVariables(t *testing.T) {
loader := KubernetesResourceLoader{}
filename := "./testdata/resources/pod.yml"
loaded, err := loader.Load(filename)
assert.Nil(t, err, "Expecting Load to not return an error")
for _, resource := range loaded.Resources {
properties := resource.Properties.(map[string]interface{})
assert.Equal(t, filename, properties["__file__"])
assert.Equal(t, filepath.Dir(filename), properties["__dir__"])
}
}
3 changes: 3 additions & 0 deletions linter/rules_resource_loader.go
Expand Up @@ -2,6 +2,7 @@ package linter

import (
"github.com/stelligent/config-lint/assertion"
"path/filepath"
)

// RulesResourceLoader converts a YAML configuration file into a collection with Resource objects
Expand Down Expand Up @@ -40,6 +41,8 @@ func (l RulesResourceLoader) Load(filename string) (FileResources, error) {
rules := getAttr(m, "rules", "Rules")
for _, rule := range rules {
properties := rule.(map[string]interface{})
properties["__file__"] = filename
properties["__dir__"] = filepath.Dir(filename)
ruleResource := assertion.Resource{
ID: properties["id"].(string),
Type: "LintRule",
Expand Down
3 changes: 3 additions & 0 deletions linter/terraform.go
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/hcl/hcl/parser"
"github.com/stelligent/config-lint/assertion"
"io/ioutil"
"path/filepath"
"regexp"
)

Expand Down Expand Up @@ -160,6 +161,8 @@ func getResources(filename string, ast *ast.File, objects []interface{}, categor
for resourceID, templateResource := range templateResource.(map[string]interface{}) {
properties := getProperties(templateResource)
lineNumber := getResourceLineNumber(resourceType, resourceID, filename, ast)
properties["__file__"] = filename
properties["__dir__"] = filepath.Dir(filename)
tr := assertion.Resource{
ID: resourceID,
Type: resourceType,
Expand Down
11 changes: 11 additions & 0 deletions linter/testdata/resources/pod.yml
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: web
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
3 changes: 3 additions & 0 deletions linter/yaml_resource_loader.go
Expand Up @@ -2,6 +2,7 @@ package linter

import (
"github.com/stelligent/config-lint/assertion"
"path/filepath"
)

// YAMLResourceLoader loads a list of Resource objects based on the list of ResourceConfig objects
Expand Down Expand Up @@ -37,6 +38,8 @@ func (l YAMLResourceLoader) Load(filename string) (FileResources, error) {
if ok {
for _, element := range sliceOfProperties {
properties := element.(map[string]interface{})
properties["__file__"] = filename
properties["__dir__"] = filepath.Dir(filename)
resource := assertion.Resource{
ID: extractResourceID(resourceConfig.ID, properties),
Type: resourceConfig.Type,
Expand Down

0 comments on commit f0592ea

Please sign in to comment.