1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

feat: add utils packages in ext (#8766)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-10-28 16:53:19 +02:00 committed by GitHub
parent c25a1df566
commit 703f7a7666
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 157 additions and 0 deletions

28
ext/file-info/ext.go Normal file
View file

@ -0,0 +1,28 @@
package fileinfo
import (
"io/fs"
"github.com/kyverno/kyverno/ext/file"
)
func IsYaml(info fs.FileInfo) bool {
if info.IsDir() {
return false
}
return file.IsYaml(info.Name())
}
func IsJson(info fs.FileInfo) bool {
if info.IsDir() {
return false
}
return file.IsJson(info.Name())
}
func IsYamlOrJson(info fs.FileInfo) bool {
if info.IsDir() {
return false
}
return file.IsYamlOrJson(info.Name())
}

19
ext/file/ext.go Normal file
View file

@ -0,0 +1,19 @@
package file
import (
"path/filepath"
)
func IsYaml(path string) bool {
ext := filepath.Ext(path)
return ext == ".yml" || ext == ".yaml"
}
func IsJson(path string) bool {
ext := filepath.Ext(path)
return ext == ".json"
}
func IsYamlOrJson(path string) bool {
return IsYaml(path) || IsJson(path)
}

110
ext/file/ext_test.go Normal file
View file

@ -0,0 +1,110 @@
package file
import (
"testing"
)
func TestIsYaml(t *testing.T) {
tests := []struct {
name string
path string
want bool
}{{
name: "empty",
path: "",
want: false,
}, {
name: "yaml",
path: "something.yaml",
want: true,
}, {
name: "yml",
path: "something.yml",
want: true,
}, {
name: "json",
path: "something.json",
want: false,
}, {
name: "pdf",
path: "something.pdf",
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsYaml(tt.path); got != tt.want {
t.Errorf("IsYaml() = %v, want %v", got, tt.want)
}
})
}
}
func TestIsJson(t *testing.T) {
tests := []struct {
name string
path string
want bool
}{{
name: "empty",
path: "",
want: false,
}, {
name: "yaml",
path: "something.yaml",
want: false,
}, {
name: "yml",
path: "something.yml",
want: false,
}, {
name: "json",
path: "something.json",
want: true,
}, {
name: "pdf",
path: "something.pdf",
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsJson(tt.path); got != tt.want {
t.Errorf("IsJson() = %v, want %v", got, tt.want)
}
})
}
}
func TestIsYamlOrJson(t *testing.T) {
tests := []struct {
name string
path string
want bool
}{{
name: "empty",
path: "",
want: false,
}, {
name: "yaml",
path: "something.yaml",
want: true,
}, {
name: "yml",
path: "something.yml",
want: true,
}, {
name: "json",
path: "something.json",
want: true,
}, {
name: "pdf",
path: "something.pdf",
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsYamlOrJson(tt.path); got != tt.want {
t.Errorf("IsYamlOrJson() = %v, want %v", got, tt.want)
}
})
}
}