Skip to content

Commit

Permalink
chore: internal refactor to reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
smlx committed Nov 3, 2021
1 parent c548265 commit 4ea375e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 33 deletions.
13 changes: 11 additions & 2 deletions cmd/lagoon-linter/validate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package main

import "github.com/uselagoon/lagoon-linter/internal/lagoonyml"
import (
"fmt"
"os"

"github.com/uselagoon/lagoon-linter/internal/lagoonyml"
)

// ValidateCmd represents the validate command.
type ValidateCmd struct {
Expand All @@ -9,5 +14,9 @@ type ValidateCmd struct {

// Run the validation of the Lagoon YAML.
func (cmd *ValidateCmd) Run() error {
return lagoonyml.LintFile(cmd.LagoonYAML, lagoonyml.RouteAnnotation())
rawYAML, err := os.ReadFile(cmd.LagoonYAML)
if err != nil {
return fmt.Errorf("couldn't read %v: %v", cmd.LagoonYAML, err)
}
return lagoonyml.Lint(rawYAML, lagoonyml.RouteAnnotation())
}
2 changes: 1 addition & 1 deletion cmd/lagoon-linter/validateconfigmapjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (cmd *ValidateConfigMapJSONCmd) Run() error {
// lint it
for _, cm := range cml.ConfigMaps {
if lagoonYAML, ok := cm.Data[".lagoon.yml"]; ok {
err := lagoonyml.LintYAML([]byte(lagoonYAML),
err := lagoonyml.Lint([]byte(lagoonYAML),
lagoonyml.RouteAnnotation())
if err != nil {
fmt.Printf("bad .lagoon.yml: %s: %v\n", cm.Metadata["namespace"], err)
Expand Down
33 changes: 4 additions & 29 deletions internal/lagoonyml/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,18 @@ package lagoonyml

import (
"fmt"
"os"

"sigs.k8s.io/yaml"
)

// Linter validates the given Lagoon struct.
type Linter func(*Lagoon) error

// LintFile takes a file path, reads it, and applies `.lagoon.yml` lint policy to
// it. Lint returns an error of type ErrLint if it finds problems with the
// file, a regular error if something else went wrong, and nil if the
// Lint takes a byte slice containing raw YAML and applies `.lagoon.yml` lint
// policy to it. Lint returns an error of type ErrLint if it finds problems
// with the file, a regular error if something else went wrong, and nil if the
// `.lagoon.yml` is valid.
func LintFile(path string, linters ...Linter) error {
var l Lagoon
rawYAML, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("couldn't read %v: %v", path, err)
}
err = yaml.Unmarshal(rawYAML, &l)
if err != nil {
return fmt.Errorf("couldn't unmarshal %v: %v", path, err)
}
for _, linter := range linters {
if err := linter(&l); err != nil {
return &ErrLint{
Detail: err.Error(),
}
}
}
return nil
}

// LintYAML takes a byte slice containing raw YAML and applies `.lagoon.yml` lint policy to
// it. Lint returns an error of type ErrLint if it finds problems with the
// file, a regular error if something else went wrong, and nil if the
// `.lagoon.yml` is valid.
func LintYAML(rawYAML []byte, linters ...Linter) error {
func Lint(rawYAML []byte, linters ...Linter) error {
var l Lagoon
if err := yaml.Unmarshal(rawYAML, &l); err != nil {
return fmt.Errorf("couldn't unmarshal YAML: %v", err)
Expand Down
7 changes: 6 additions & 1 deletion internal/lagoonyml/lint_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lagoonyml_test

import (
"os"
"testing"

"github.com/uselagoon/lagoon-linter/internal/lagoonyml"
Expand Down Expand Up @@ -58,7 +59,11 @@ func TestLint(t *testing.T) {
}
for name, tc := range testCases {
t.Run(name, func(tt *testing.T) {
err := lagoonyml.LintFile(tc.input, lagoonyml.RouteAnnotation())
rawYAML, err := os.ReadFile(tc.input)
if err != nil {
tt.Fatalf("couldn't read %v: %v", tc.input, err)
}
err = lagoonyml.Lint(rawYAML, lagoonyml.RouteAnnotation())
if tc.valid {
if err != nil {
tt.Fatalf("unexpected error %v", err)
Expand Down

0 comments on commit 4ea375e

Please sign in to comment.