From 67d76248a346c3600dc8f153adf8a21bdbe5cddf Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Tue, 19 Apr 2022 03:40:48 -0500 Subject: [PATCH] Take in an io.Reader instead of file path (#885) Signed-off-by: jolheiser --- cli/lint/lint.go | 8 +++++++- pipeline/schema/schema.go | 6 ++++-- pipeline/schema/schema_test.go | 6 +++++- shared/yml/yml.go | 8 +++++--- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cli/lint/lint.go b/cli/lint/lint.go index 56507b5e5..052d1ee07 100644 --- a/cli/lint/lint.go +++ b/cli/lint/lint.go @@ -44,7 +44,13 @@ func lintDir(c *cli.Context, dir string) error { } func lintFile(_ *cli.Context, file string) error { - configErrors, err := schema.Lint(file) + fi, err := os.Open(file) + if err != nil { + return err + } + defer fi.Close() + + configErrors, err := schema.Lint(fi) if err != nil { fmt.Println("❌ Config is invalid") for _, configError := range configErrors { diff --git a/pipeline/schema/schema.go b/pipeline/schema/schema.go index 9d85b2a83..34c229e09 100644 --- a/pipeline/schema/schema.go +++ b/pipeline/schema/schema.go @@ -3,6 +3,7 @@ package schema import ( _ "embed" "fmt" + "io" "github.com/xeipuuv/gojsonschema" @@ -12,9 +13,10 @@ import ( //go:embed schema.json var schemaDefinition []byte -func Lint(file string) ([]gojsonschema.ResultError, error) { +// Lint lints an io.Reader against the Woodpecker schema.json +func Lint(r io.Reader) ([]gojsonschema.ResultError, error) { schemaLoader := gojsonschema.NewBytesLoader(schemaDefinition) - j, err := yml.LoadYmlFileAsJSON(file) + j, err := yml.LoadYmlReaderAsJSON(r) if err != nil { return nil, fmt.Errorf("Failed to load yml file %w", err) } diff --git a/pipeline/schema/schema_test.go b/pipeline/schema/schema_test.go index 4316ea147..a2d3ee0d9 100644 --- a/pipeline/schema/schema_test.go +++ b/pipeline/schema/schema_test.go @@ -2,6 +2,7 @@ package schema_test import ( "fmt" + "os" "testing" "github.com/stretchr/testify/assert" @@ -78,7 +79,10 @@ func TestSchema(t *testing.T) { for _, tt := range testTable { t.Run(tt.name, func(t *testing.T) { - configErrors, err := schema.Lint(tt.testFile) + fi, err := os.Open(tt.testFile) + assert.NoError(t, err, "could not open test file") + defer fi.Close() + configErrors, err := schema.Lint(fi) if tt.fail { if len(configErrors) == 0 { assert.Error(t, err, "Expected config errors but got none") diff --git a/shared/yml/yml.go b/shared/yml/yml.go index 90af53b1f..dea05e260 100644 --- a/shared/yml/yml.go +++ b/shared/yml/yml.go @@ -3,7 +3,7 @@ package yml import ( "encoding/json" "fmt" - "os" + "io" "strconv" "gopkg.in/yaml.v3" @@ -59,6 +59,7 @@ func toJSON(node *yaml.Node) (interface{}, error) { return nil, fmt.Errorf("do not support yaml node kind '%v'", node.Kind) } +// ToJSON converts YAML bytes to JSON func ToJSON(data []byte) ([]byte, error) { m := &yaml.Node{} if err := yaml.Unmarshal(data, m); err != nil { @@ -72,8 +73,9 @@ func ToJSON(data []byte) ([]byte, error) { return json.Marshal(d) } -func LoadYmlFileAsJSON(path string) (j []byte, err error) { - data, err := os.ReadFile(path) +// LoadYmlReaderAsJSON reads from an io.Reader containing YAML and converts it to JSON +func LoadYmlReaderAsJSON(r io.Reader) (j []byte, err error) { + data, err := io.ReadAll(r) if err != nil { return nil, err }