Skip to content

Commit

Permalink
Return errors only HSP-9244 (#5)
Browse files Browse the repository at this point in the history
* Return errors only HSP-9244

* add condition for allow-origin *

* add errors-only query param

---------

Co-authored-by: Christian Eickhoff <[email protected]>
  • Loading branch information
eic-chr and Christian Eickhoff authored Aug 23, 2024
1 parent 8f3f4fd commit 8d903eb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
24 changes: 15 additions & 9 deletions cmd/spectral.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package main

import (
"io/ioutil"
"errors"
"log"
"os"
"os/exec"
"strings"
)

const (
rulesetQueryParam = "ruleset"
acceptHeader = "Accept"
rulesetQueryParam = "ruleset"
errorsOnlyQueryParam = "errors-only"
acceptHeader = "Accept"
)

var outputFormats = map[string]string{
Expand All @@ -33,10 +34,11 @@ var spectral = Spectral{
}

type SpectralLintOpts struct {
Ruleset string
FilePath string
Format string
SkipRules []string
Ruleset string
FilePath string
Format string
SkipRules []string
DisplayOnlyFailures bool
}

func (opts *SpectralLintOpts) Output() string {
Expand All @@ -45,6 +47,9 @@ func (opts *SpectralLintOpts) Output() string {

func (opts *SpectralLintOpts) ToArgs() []string {
args := []string{"lint", "--quiet", "--ruleset", opts.Ruleset, "--format", opts.Format, "--output", opts.Output()}
if opts.DisplayOnlyFailures {
args = append(args, "--display-only-failures")
}
for _, skipRule := range opts.SkipRules {
args = append(args, "--skip-rule", skipRule)
}
Expand All @@ -57,7 +62,8 @@ func (spectral *Spectral) Lint(opts SpectralLintOpts) (string, error) {
cmd := exec.Command(spectral.Path, opts.ToArgs()...)
log.Println("running command:", cmd.Args)
stdoutBytes, err := cmd.Output()
exitErr, isExitErr := err.(*exec.ExitError)
var exitErr *exec.ExitError
isExitErr := errors.As(err, &exitErr)
if err != nil {
if isExitErr && exitErr.ProcessState.ExitCode() == 1 {
log.Println("There seem to be critical linting errors!")
Expand All @@ -67,7 +73,7 @@ func (spectral *Spectral) Lint(opts SpectralLintOpts) (string, error) {
}
}

outputBytes, err := ioutil.ReadFile(opts.Output())
outputBytes, err := os.ReadFile(opts.Output())
if err != nil {
log.Println("Failed to open output file:", opts.Output())
return "", err
Expand Down
42 changes: 29 additions & 13 deletions cmd/valigator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@ import (
"log"
"net/http"
"os"
"strconv"
"strings"

"github.com/google/uuid"
)

type ValigatorConfig struct {
Host string `json:"host"`
Port int `json:"port"`
BasePath string `json:"basePath"`
SkipRules []string `json:"skipRules"`
Host string `json:"host"`
Port int `json:"port"`
BasePath string `json:"basePath"`
DisplayOnlyFailures bool `json:"displayOnlyFailures"`
SkipRules []string `json:"skipRules"`
}

func NewValigatorConfig(configFile string) *ValigatorConfig {
config := ValigatorConfig{
Host: "0.0.0.0",
Port: 8081,
BasePath: "/valigator",
SkipRules: []string{},
Host: "0.0.0.0",
Port: 8081,
BasePath: "/valigator",
DisplayOnlyFailures: true,
SkipRules: []string{},
}

bytes, err := os.ReadFile(configFile)
Expand Down Expand Up @@ -151,11 +154,20 @@ func (context *ValigatorContext) validate(w http.ResponseWriter, r *http.Request
log.Panicln(err)
}

errorsOnlyParam := query.Get(errorsOnlyQueryParam)
errorsOnly, err := strconv.ParseBool(errorsOnlyParam)
if err != nil {
// Fehlerbehandlung, falls die Konvertierung fehlschlägt
log.Printf("Invalid value for 'errors-only': %v. Using default value", err)
errorsOnly = context.Config.DisplayOnlyFailures
}

spectralLintOpts := SpectralLintOpts{
FilePath: filePath,
Ruleset: ruleset,
Format: spectralMediaType,
SkipRules: context.Config.SkipRules,
FilePath: filePath,
Ruleset: ruleset,
Format: spectralMediaType,
DisplayOnlyFailures: errorsOnly,
SkipRules: context.Config.SkipRules,
}

spectralLintOutput, err := spectral.Lint(spectralLintOpts)
Expand All @@ -176,7 +188,11 @@ func (context *ValigatorContext) validate(w http.ResponseWriter, r *http.Request
} else {
w.Header().Add("Content-Type", "text/plain")
}

isLocalRequest := strings.Contains(r.Host, "localhost")
if isLocalRequest {
log.Printf("host is %s. Set Access-Control-Allow-Origin: *", r.Host)
w.Header().Add("Access-Control-Allow-Origin", "*")
}
_, err = w.Write([]byte(spectralLintOutput))
if err != nil {
log.Println("Write spectral lint output to response failed!")
Expand Down
3 changes: 2 additions & 1 deletion valigator.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"port": 8081,
"basePath": "/valigator",
"skipRules": [],
"displayOnlyFailures": true,
"ruleSets": [ "v5", "v10" ]
}
}

0 comments on commit 8d903eb

Please sign in to comment.