From 703f7a76668490a6019e7aae5f12bcce3456473e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Sat, 28 Oct 2023 16:53:19 +0200 Subject: [PATCH] feat: add utils packages in ext (#8766) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- ext/file-info/ext.go | 28 +++++++++++ ext/file/ext.go | 19 ++++++++ ext/file/ext_test.go | 110 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 ext/file-info/ext.go create mode 100644 ext/file/ext.go create mode 100644 ext/file/ext_test.go diff --git a/ext/file-info/ext.go b/ext/file-info/ext.go new file mode 100644 index 0000000000..b591e36646 --- /dev/null +++ b/ext/file-info/ext.go @@ -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()) +} diff --git a/ext/file/ext.go b/ext/file/ext.go new file mode 100644 index 0000000000..9347506a17 --- /dev/null +++ b/ext/file/ext.go @@ -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) +} diff --git a/ext/file/ext_test.go b/ext/file/ext_test.go new file mode 100644 index 0000000000..43e8d55e74 --- /dev/null +++ b/ext/file/ext_test.go @@ -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) + } + }) + } +}