1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

chore: create cli pathutils package (#8164)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-08-29 20:20:17 +02:00 committed by GitHub
parent 4c05c2833c
commit afd736428f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 21 deletions

View file

@ -3,7 +3,6 @@ package test
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
@ -14,6 +13,7 @@ import (
policyreportv1alpha2 "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/common"
pathutils "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/path"
sanitizederror "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/sanitizedError"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/store"
"github.com/kyverno/kyverno/pkg/autogen"
@ -81,17 +81,17 @@ func applyPoliciesFromPath(
}
}
policyFullPath := getFullPath(values.Policies, policyResourcePath, isGit)
resourceFullPath := getFullPath(values.Resources, policyResourcePath, isGit)
policyFullPath := pathutils.GetFullPaths(values.Policies, policyResourcePath, isGit)
resourceFullPath := pathutils.GetFullPaths(values.Resources, policyResourcePath, isGit)
for i, result := range values.Results {
arrPatchedResource := []string{result.PatchedResource}
arrGeneratedResource := []string{result.GeneratedResource}
arrCloneSourceResource := []string{result.CloneSourceResource}
patchedResourceFullPath := getFullPath(arrPatchedResource, policyResourcePath, isGit)
generatedResourceFullPath := getFullPath(arrGeneratedResource, policyResourcePath, isGit)
CloneSourceResourceFullPath := getFullPath(arrCloneSourceResource, policyResourcePath, isGit)
patchedResourceFullPath := pathutils.GetFullPaths(arrPatchedResource, policyResourcePath, isGit)
generatedResourceFullPath := pathutils.GetFullPaths(arrGeneratedResource, policyResourcePath, isGit)
CloneSourceResourceFullPath := pathutils.GetFullPaths(arrCloneSourceResource, policyResourcePath, isGit)
values.Results[i].PatchedResource = patchedResourceFullPath[0]
values.Results[i].GeneratedResource = generatedResourceFullPath[0]
@ -253,21 +253,6 @@ func applyPoliciesFromPath(
return resultsMap, testResults, nil
}
func getFullPath(paths []string, policyResourcePath string, isGit bool) []string {
var pols []string
if !isGit {
for _, path := range paths {
if !filepath.IsAbs(path) {
pols = append(pols, filepath.Join(policyResourcePath, path))
} else {
pols = append(pols, path)
}
}
return pols
}
return paths
}
func selectResourcesForCheck(resources []*unstructured.Unstructured, values *api.Test) []*unstructured.Unstructured {
res, _, _ := selectResourcesForCheckInternal(resources, values)
return res

View file

@ -0,0 +1,24 @@
package path
import (
"path/filepath"
)
func GetFullPath(path string, basePath string) string {
if !filepath.IsAbs(path) {
return filepath.Join(basePath, path)
} else {
return path
}
}
func GetFullPaths(paths []string, basePath string, git bool) []string {
if git {
return paths
}
var out []string
for _, path := range paths {
out = append(out, GetFullPath(path, basePath))
}
return out
}