Skip to content

Filesystem is not required when reading from URL and Data #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pkg/devfile/parser/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package parser

import (
"bytes"
"fmt"
"io"

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

var data []byte
var err error
Expand All @@ -65,6 +67,9 @@ func ReadKubernetesYaml(src YamlSrc, fs afero.Afero) ([]interface{}, error) {
return nil, errors.Wrapf(err, "failed to download file %q", src.URL)
}
} else if src.Path != "" {
if fs == nil {
return nil, fmt.Errorf("cannot read from %s because fs passed in was nil", src.Path)
}
data, err = fs.ReadFile(src.Path)
if err != nil {
return nil, errors.Wrapf(err, "failed to read yaml from path %q", src.Path)
Expand Down
22 changes: 15 additions & 7 deletions pkg/devfile/parser/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
tests := []struct {
name string
src YamlSrc
fs afero.Afero
fs *afero.Afero
wantErr bool
wantDeploymentNames []string
wantServiceNames []string
Expand All @@ -84,7 +84,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
src: YamlSrc{
URL: "http://" + serverIP,
},
fs: fs,
fs: nil,
wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"},
wantServiceNames: []string{"service-sample", "service-sample-2"},
wantRouteNames: []string{"route-sample", "route-sample-2"},
Expand All @@ -96,19 +96,27 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
src: YamlSrc{
Path: "../../../tests/yamls/resources.yaml",
},
fs: fs,
fs: &fs,
wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"},
wantServiceNames: []string{"service-sample", "service-sample-2"},
wantRouteNames: []string{"route-sample", "route-sample-2"},
wantIngressNames: []string{"ingress-sample", "ingress-sample-2"},
wantOtherNames: []string{"pvc-sample", "pvc-sample-2"},
},
{
name: "Read the YAML from the Path with no fs passed",
src: YamlSrc{
Path: "../../../tests/yamls/resources.yaml",
},
fs: nil,
wantErr: true,
},
{
name: "Read the YAML from the Data",
src: YamlSrc{
Data: data,
},
fs: fs,
fs: nil,
wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"},
wantServiceNames: []string{"service-sample", "service-sample-2"},
wantRouteNames: []string{"route-sample", "route-sample-2"},
Expand All @@ -120,23 +128,23 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
src: YamlSrc{
URL: "http://badurl",
},
fs: fs,
fs: nil,
wantErr: true,
},
{
name: "Bad Path",
src: YamlSrc{
Path: "$%^&",
},
fs: fs,
fs: &fs,
wantErr: true,
},
{
name: "Bad Data",
src: YamlSrc{
Data: badData,
},
fs: fs,
fs: nil,
wantErr: true,
},
}
Expand Down