Repository: datreeio/datree
Pull request #585 (2022-04-25 08:41:59)

feat: save and use default rules from datree dir


Patch:
diff --git a/pkg/cliClient/evaluation.go b/pkg/cliClient/evaluation.go
index 86a0e12..ea0c339 100644
--- a/pkg/cliClient/evaluation.go
+++ b/pkg/cliClient/evaluation.go
@@ -98,6 +98,7 @@ type EvaluationPrerunDataResponse struct {
 	RegistrationURL       string                    `json:"registrationURL"`
 	PromptRegistrationURL string                    `json:"promptRegistrationURL"`
 	IsPolicyAsCodeMode    bool                      `json:"isPolicyAsCodeMode"`
+	DefaultRulesYaml      string                    `json:"defaultRulesYaml"`
 }
 
 func (c *CliClient) RequestEvaluationPrerunData(tokenId string) (*EvaluationPrerunDataResponse, error) {
diff --git a/pkg/policy/policy.go b/pkg/policy/policy.go
index 1432ce2..34d288b 100644
--- a/pkg/policy/policy.go
+++ b/pkg/policy/policy.go
@@ -3,6 +3,9 @@ package policy
 import (
 	_ "embed"
 	"fmt"
+	"io/ioutil"
+	"os"
+	"path/filepath"
 
 	"github.com/datreeio/datree/pkg/cliClient"
 	"github.com/datreeio/datree/pkg/fileReader"
@@ -11,7 +14,7 @@ import (
 )
 
 //go:embed defaultRules.yaml
-var defaultRulesYamlContent string
+var embeddedDefaultRulesYamlContent string
 
 //go:embed policiesSchema.json
 var policiesSchemaContent string
@@ -33,8 +36,12 @@ type DefaultRuleDefinition struct {
 }
 
 func GetDefaultRules() (*DefaultRulesDefinitions, error) {
-	defaultRulesDefinitions, err := yamlToStruct(defaultRulesYamlContent)
-	return defaultRulesDefinitions, err
+	configDefaultRulesYamlContent, err := getDefaultRulesFromFile()
+	if err == nil {
+		return yamlToStruct(configDefaultRulesYamlContent)
+	}
+
+	return yamlToStruct(embeddedDefaultRulesYamlContent)
 }
 
 func GetPoliciesFileFromPath(path string) (*cliClient.EvaluationPrerunPolicies, error) {
@@ -92,3 +99,12 @@ func yamlToStruct(content string) (*DefaultRulesDefinitions, error) {
 	}
 	return &defaultRulesDefinitions, err
 }
+
+func getDefaultRulesFromFile() (string, error) {
+	homeDir, err := os.UserHomeDir()
+	if err != nil {
+		return "", err
+	}
+	defaultRulesFileContent, err := ioutil.ReadFile(filepath.Join(homeDir, ".datree", "defaultRules.yaml"))
+	return string(defaultRulesFileContent), err
+}


Test patch:
diff --git a/cmd/test/main.go b/cmd/test/main.go
index ec8a6a6..1471686 100644
--- a/cmd/test/main.go
+++ b/cmd/test/main.go
@@ -3,7 +3,9 @@ package test
 import (
 	"fmt"
 	"io"
+	"io/ioutil"
 	"os"
+	"path/filepath"
 	"regexp"
 	"strings"
 
@@ -193,6 +195,8 @@ func New(ctx *TestCommandContext) *cobra.Command {
 			}
 
 			evaluationPrerunData, err := ctx.CliClient.RequestEvaluationPrerunData(localConfigContent.Token)
+			saveDefaultRulesAsFile(ctx, evaluationPrerunData.DefaultRulesYaml)
+
 			if err != nil {
 				return err
 			}
@@ -515,3 +519,19 @@ func wereViolationsFound(validationManager *ValidationManager, results *evaluati
 		return false
 	}
 }
+
+func saveDefaultRulesAsFile(ctx *TestCommandContext, preRunDefaultRulesYaml string) {
+	if preRunDefaultRulesYaml == "" {
+		return
+	}
+
+	homeDir, err := os.UserHomeDir()
+	if err != nil {
+		return
+	}
+
+	defaultRulesFilePath := filepath.Join(homeDir, ".datree", "defaultRules.yaml")
+
+	const fileReadPermission = 0644
+	_ = ioutil.WriteFile(defaultRulesFilePath, []byte(preRunDefaultRulesYaml), os.FileMode(fileReadPermission))
+}