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:
parent
4c05c2833c
commit
afd736428f
2 changed files with 30 additions and 21 deletions
|
@ -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
|
||||
|
|
24
cmd/cli/kubectl-kyverno/utils/path/path.go
Normal file
24
cmd/cli/kubectl-kyverno/utils/path/path.go
Normal 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
|
||||
}
|
Loading…
Add table
Reference in a new issue