@@ -71,19 +71,6 @@ type Reader struct {
7171 manifests []* ResourceDefinition
7272}
7373
74- type namedReader struct {
75- reader io.Reader
76- name string
77- }
78-
79- func (n * namedReader ) Read (p []byte ) (int , error ) {
80- return n .reader .Read (p )
81- }
82-
83- func (n * namedReader ) Name () string {
84- return n .name
85- }
86-
8774func name (r io.Reader ) string {
8875 type named interface {
8976 Name () string
@@ -96,8 +83,32 @@ func name(r io.Reader) string {
9683 return "manifest"
9784}
9885
86+ // ParseOption configures optional behavior for Parse.
87+ type ParseOption func (* parseOptions )
88+
89+ type parseOptions struct {
90+ origin string
91+ }
92+
93+ // WithOrigin overrides the origin name for resources parsed from the reader.
94+ func WithOrigin (origin string ) ParseOption {
95+ return func (po * parseOptions ) {
96+ po .origin = origin
97+ }
98+ }
99+
99100// Parse parses Kubernetes resource definitions from the provided input stream. They are then available via the Resources() or GetResources(apiVersion, kind) methods.
100- func (r * Reader ) Parse (input io.Reader ) error {
101+ func (r * Reader ) Parse (input io.Reader , opts ... ParseOption ) error {
102+ po := & parseOptions {}
103+ for _ , opt := range opts {
104+ opt (po )
105+ }
106+
107+ origin := po .origin
108+ if origin == "" {
109+ origin = name (input )
110+ }
111+
101112 yamlReader := yamlutil .NewYAMLReader (bufio .NewReader (input ))
102113
103114 for {
@@ -118,17 +129,17 @@ func (r *Reader) Parse(input io.Reader) error {
118129 if r .IgnoreErrors {
119130 continue
120131 }
121- return fmt .Errorf ("failed to decode resource %s: %w" , name ( input ) , err )
132+ return fmt .Errorf ("failed to decode resource %s: %w" , origin , err )
122133 }
123134
124135 if rd .APIVersion == "" || rd .Kind == "" {
125136 if r .IgnoreErrors {
126137 continue
127138 }
128- return fmt .Errorf ("missing apiVersion or kind in resource %s" , name ( input ) )
139+ return fmt .Errorf ("missing apiVersion or kind in resource %s" , origin )
129140 }
130141
131- rd .Origin = name ( input )
142+ rd .Origin = origin
132143 // Store the raw chunk
133144 rd .Raw = append ([]byte {}, rawChunk ... )
134145 r .manifests = append (r .manifests , rd )
@@ -138,18 +149,18 @@ func (r *Reader) Parse(input io.Reader) error {
138149}
139150
140151// ParseString parses Kubernetes resource definitions from the provided string.
141- func (r * Reader ) ParseString (input string ) error {
142- return r .Parse (strings .NewReader (input ))
152+ func (r * Reader ) ParseString (input string , opts ... ParseOption ) error {
153+ return r .Parse (strings .NewReader (input ), opts ... )
143154}
144155
145156// ParseBytes parses Kubernetes resource definitions from the provided byte slice.
146- func (r * Reader ) ParseBytes (input []byte ) error {
147- return r .ParseBytesWithOrigin ( input , "" )
157+ func (r * Reader ) ParseBytes (input []byte , opts ... ParseOption ) error {
158+ return r .Parse ( bytes . NewReader ( input ), opts ... )
148159}
149160
150161// ParseBytesWithOrigin parses raw bytes and attributes them to the provided origin.
151162func (r * Reader ) ParseBytesWithOrigin (input []byte , origin string ) error {
152- return r .Parse ( & namedReader { reader : bytes . NewReader ( input ), name : origin } )
163+ return r .ParseBytes ( input , WithOrigin ( origin ) )
153164}
154165
155166// Resources returns all parsed Kubernetes resource definitions.
0 commit comments