Skip to content

Commit a05b6f3

Browse files
committed
Filesystem is not required when reading from URL and Data
Signed-off-by: Maysun J Faisal <maysunaneek@gmail.com>
1 parent 8b432ca commit a05b6f3

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

pkg/devfile/parser/reader.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package parser
1717

1818
import (
1919
"bytes"
20+
"fmt"
2021
"io"
2122

2223
"github.com/devfile/library/pkg/util"
@@ -53,8 +54,9 @@ type KubernetesResources struct {
5354
// ReadKubernetesYaml reads a yaml Kubernetes file from either the Path, URL or Data provided.
5455
// It returns all the parsed Kubernetes objects as an array of interface.
5556
// Consumers interested in the Kubernetes resources are expected to Unmarshal
56-
// it to the struct of the respective Kubernetes resource.
57-
func ReadKubernetesYaml(src YamlSrc, fs afero.Afero) ([]interface{}, error) {
57+
// it to the struct of the respective Kubernetes resource. If a Path is being passed,
58+
// provide a filesystem, otherwise nil can be passed in
59+
func ReadKubernetesYaml(src YamlSrc, fs *afero.Afero) ([]interface{}, error) {
5860

5961
var data []byte
6062
var err error
@@ -65,6 +67,9 @@ func ReadKubernetesYaml(src YamlSrc, fs afero.Afero) ([]interface{}, error) {
6567
return nil, errors.Wrapf(err, "failed to download file %q", src.URL)
6668
}
6769
} else if src.Path != "" {
70+
if fs == nil {
71+
return nil, fmt.Errorf("cannot read from %s because fs passed in was nil", src.Path)
72+
}
6873
data, err = fs.ReadFile(src.Path)
6974
if err != nil {
7075
return nil, errors.Wrapf(err, "failed to read yaml from path %q", src.Path)

pkg/devfile/parser/reader_test.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
7171
tests := []struct {
7272
name string
7373
src YamlSrc
74-
fs afero.Afero
74+
fs *afero.Afero
7575
wantErr bool
7676
wantDeploymentNames []string
7777
wantServiceNames []string
@@ -84,7 +84,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
8484
src: YamlSrc{
8585
URL: "http://" + serverIP,
8686
},
87-
fs: fs,
87+
fs: nil,
8888
wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"},
8989
wantServiceNames: []string{"service-sample", "service-sample-2"},
9090
wantRouteNames: []string{"route-sample", "route-sample-2"},
@@ -96,19 +96,27 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
9696
src: YamlSrc{
9797
Path: "../../../tests/yamls/resources.yaml",
9898
},
99-
fs: fs,
99+
fs: &fs,
100100
wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"},
101101
wantServiceNames: []string{"service-sample", "service-sample-2"},
102102
wantRouteNames: []string{"route-sample", "route-sample-2"},
103103
wantIngressNames: []string{"ingress-sample", "ingress-sample-2"},
104104
wantOtherNames: []string{"pvc-sample", "pvc-sample-2"},
105105
},
106+
{
107+
name: "Read the YAML from the Path with no fs passed",
108+
src: YamlSrc{
109+
Path: "../../../tests/yamls/resources.yaml",
110+
},
111+
fs: nil,
112+
wantErr: true,
113+
},
106114
{
107115
name: "Read the YAML from the Data",
108116
src: YamlSrc{
109117
Data: data,
110118
},
111-
fs: fs,
119+
fs: nil,
112120
wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"},
113121
wantServiceNames: []string{"service-sample", "service-sample-2"},
114122
wantRouteNames: []string{"route-sample", "route-sample-2"},
@@ -120,23 +128,23 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
120128
src: YamlSrc{
121129
URL: "http://badurl",
122130
},
123-
fs: fs,
131+
fs: nil,
124132
wantErr: true,
125133
},
126134
{
127135
name: "Bad Path",
128136
src: YamlSrc{
129137
Path: "$%^&",
130138
},
131-
fs: fs,
139+
fs: &fs,
132140
wantErr: true,
133141
},
134142
{
135143
name: "Bad Data",
136144
src: YamlSrc{
137145
Data: badData,
138146
},
139-
fs: fs,
147+
fs: nil,
140148
wantErr: true,
141149
},
142150
}

0 commit comments

Comments
 (0)