mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-06-02 13:09:30 +00:00
Take in an io.Reader instead of file path (#885)
Signed-off-by: jolheiser <john.olheiser@gmail.com>
This commit is contained in:
parent
3a3282cde2
commit
67d76248a3
|
@ -44,7 +44,13 @@ func lintDir(c *cli.Context, dir string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func lintFile(_ *cli.Context, file 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 {
|
if err != nil {
|
||||||
fmt.Println("❌ Config is invalid")
|
fmt.Println("❌ Config is invalid")
|
||||||
for _, configError := range configErrors {
|
for _, configError := range configErrors {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package schema
|
||||||
import (
|
import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/xeipuuv/gojsonschema"
|
"github.com/xeipuuv/gojsonschema"
|
||||||
|
|
||||||
|
@ -12,9 +13,10 @@ import (
|
||||||
//go:embed schema.json
|
//go:embed schema.json
|
||||||
var schemaDefinition []byte
|
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)
|
schemaLoader := gojsonschema.NewBytesLoader(schemaDefinition)
|
||||||
j, err := yml.LoadYmlFileAsJSON(file)
|
j, err := yml.LoadYmlReaderAsJSON(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Failed to load yml file %w", err)
|
return nil, fmt.Errorf("Failed to load yml file %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package schema_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -78,7 +79,10 @@ func TestSchema(t *testing.T) {
|
||||||
|
|
||||||
for _, tt := range testTable {
|
for _, tt := range testTable {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
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 tt.fail {
|
||||||
if len(configErrors) == 0 {
|
if len(configErrors) == 0 {
|
||||||
assert.Error(t, err, "Expected config errors but got none")
|
assert.Error(t, err, "Expected config errors but got none")
|
||||||
|
|
|
@ -3,7 +3,7 @@ package yml
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"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)
|
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) {
|
func ToJSON(data []byte) ([]byte, error) {
|
||||||
m := &yaml.Node{}
|
m := &yaml.Node{}
|
||||||
if err := yaml.Unmarshal(data, m); err != nil {
|
if err := yaml.Unmarshal(data, m); err != nil {
|
||||||
|
@ -72,8 +73,9 @@ func ToJSON(data []byte) ([]byte, error) {
|
||||||
return json.Marshal(d)
|
return json.Marshal(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadYmlFileAsJSON(path string) (j []byte, err error) {
|
// LoadYmlReaderAsJSON reads from an io.Reader containing YAML and converts it to JSON
|
||||||
data, err := os.ReadFile(path)
|
func LoadYmlReaderAsJSON(r io.Reader) (j []byte, err error) {
|
||||||
|
data, err := io.ReadAll(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue