Skip to content

Commit 8b46cb2

Browse files
authored
Merge pull request #16 from onmetal/issue-9
Implements ReadFiles for reading multiple files from the specified folder
2 parents 73eea6f + fa47a62 commit 8b46cb2

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

testdata/testdata.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ func UnstructuredConfigMap() *unstructured.Unstructured {
9898
}
9999
}
100100

101+
func UnstructuredMyConfigMap() *unstructured.Unstructured {
102+
return &unstructured.Unstructured{
103+
Object: map[string]interface{}{
104+
"apiVersion": "v1",
105+
"kind": "ConfigMap",
106+
"metadata": map[string]interface{}{
107+
"name": "my-config",
108+
},
109+
"data": map[string]interface{}{
110+
"foo": "bar",
111+
},
112+
},
113+
}
114+
}
115+
101116
func Objects() []client.Object {
102117
return []client.Object{Secret(), ConfigMap()}
103118
}

unstructuredutils/unstructuredutils.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io"
2424
"os"
25+
"path/filepath"
2526

2627
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2728
"k8s.io/apimachinery/pkg/runtime"
@@ -45,6 +46,24 @@ func ReadFile(filename string) ([]unstructured.Unstructured, error) {
4546
return Read(f)
4647
}
4748

49+
// ReadFiles reads unstructured objects from a folder with the given name (including sub folders)
50+
// and file name matched with the pattern.
51+
func ReadFiles(pattern string) ([]unstructured.Unstructured, error) {
52+
var objs []unstructured.Unstructured
53+
files, err := filepath.Glob(pattern)
54+
if err != nil {
55+
return nil, err
56+
}
57+
for _, file := range files {
58+
uobjs, err := ReadFile(file)
59+
if err != nil {
60+
return nil, err
61+
}
62+
objs = append(objs, uobjs...)
63+
}
64+
return objs, nil
65+
}
66+
4867
// Read treats io.Reader as an incoming YAML or JSON stream and reads all unstructured.Unstructured objects of it.
4968
//
5069
// The document has to be well-formed. For multi-doc YAMLs, '---' is used as separator.

unstructuredutils/unstructuredutils_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ package unstructuredutils_test
1717
import (
1818
"bytes"
1919
_ "embed"
20+
"path/filepath"
2021
"strings"
2122

2223
"github.com/onmetal/controller-utils/testdata"
2324
. "github.com/onmetal/controller-utils/unstructuredutils"
2425
. "github.com/onsi/ginkgo"
2526
. "github.com/onsi/gomega"
2627
. "github.com/onsi/gomega/gstruct"
28+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2729
)
2830

2931
var _ = Describe("Unstructuredutils", func() {
@@ -58,6 +60,26 @@ var _ = Describe("Unstructuredutils", func() {
5860
})
5961
})
6062

63+
Describe("ReadFiles", func() {
64+
It("should read all objects from the folder", func() {
65+
objs, err := ReadFiles("../testdata/*.yaml")
66+
Expect(err).NotTo(HaveOccurred())
67+
Expect(objs).To(Equal([]unstructured.Unstructured{*testdata.UnstructuredMyConfigMap(), *testdata.UnstructuredSecret(), *testdata.UnstructuredConfigMap()}))
68+
})
69+
70+
It("should empty result and no error if there is no folder presents", func() {
71+
objs, err := ReadFiles("nonexistent-folder")
72+
Expect(err).NotTo(HaveOccurred())
73+
Expect(objs).NotTo(Equal([]unstructured.Unstructured{}))
74+
})
75+
76+
It("should result an ErrBadPattern error if pattern is wrong", func() {
77+
_, err := ReadFiles("nonexistent-folder[")
78+
Expect(err).Should(Equal(filepath.ErrBadPattern))
79+
80+
})
81+
})
82+
6183
Describe("UnstructuredSliceToObjectSliceNoCopy", func() {
6284
It("should return nil if the unstructureds are nil", func() {
6385
Expect(UnstructuredSliceToObjectSliceNoCopy(nil)).To(BeNil())

0 commit comments

Comments
 (0)