From bd1748b2a6efd8a16bf7f6aa989b6b80223e7f04 Mon Sep 17 00:00:00 2001 From: Christian Eickhoff Date: Thu, 22 Aug 2024 15:33:31 +0200 Subject: [PATCH 1/3] Return errors only HSP-9244 --- cmd/spectral.go | 19 ++++++++++++------- cmd/valigator.go | 29 ++++++++++++++++------------- valigator.json | 3 ++- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/cmd/spectral.go b/cmd/spectral.go index ca90484..7a882ab 100644 --- a/cmd/spectral.go +++ b/cmd/spectral.go @@ -1,7 +1,7 @@ package main import ( - "io/ioutil" + "errors" "log" "os" "os/exec" @@ -33,10 +33,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 { @@ -45,6 +46,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) } @@ -57,7 +61,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!") @@ -67,7 +72,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 diff --git a/cmd/valigator.go b/cmd/valigator.go index 48bf80a..6fb188c 100644 --- a/cmd/valigator.go +++ b/cmd/valigator.go @@ -13,18 +13,20 @@ import ( ) 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) @@ -152,10 +154,11 @@ func (context *ValigatorContext) validate(w http.ResponseWriter, r *http.Request } spectralLintOpts := SpectralLintOpts{ - FilePath: filePath, - Ruleset: ruleset, - Format: spectralMediaType, - SkipRules: context.Config.SkipRules, + FilePath: filePath, + Ruleset: ruleset, + Format: spectralMediaType, + DisplayOnlyFailures: context.Config.DisplayOnlyFailures, + SkipRules: context.Config.SkipRules, } spectralLintOutput, err := spectral.Lint(spectralLintOpts) @@ -176,7 +179,7 @@ func (context *ValigatorContext) validate(w http.ResponseWriter, r *http.Request } else { w.Header().Add("Content-Type", "text/plain") } - + w.Header().Add("Access-Control-Allow-Origin", "*") _, err = w.Write([]byte(spectralLintOutput)) if err != nil { log.Println("Write spectral lint output to response failed!") diff --git a/valigator.json b/valigator.json index f404746..069d1dd 100644 --- a/valigator.json +++ b/valigator.json @@ -3,5 +3,6 @@ "port": 8081, "basePath": "/valigator", "skipRules": [], + "displayOnlyFailures": true, "ruleSets": [ "v5", "v10" ] -} \ No newline at end of file +} From 7f11eaf8c6115f4c0ed8d39a2bf6d4cae12c7316 Mon Sep 17 00:00:00 2001 From: Christian Eickhoff Date: Thu, 22 Aug 2024 17:42:08 +0200 Subject: [PATCH 2/3] add condition for allow-origin * --- cmd/valigator.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/valigator.go b/cmd/valigator.go index 6fb188c..11b97b6 100644 --- a/cmd/valigator.go +++ b/cmd/valigator.go @@ -179,7 +179,11 @@ func (context *ValigatorContext) validate(w http.ResponseWriter, r *http.Request } else { w.Header().Add("Content-Type", "text/plain") } - w.Header().Add("Access-Control-Allow-Origin", "*") + 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!") From 7c0765623d28948bbb509073b7aeb8484863cd45 Mon Sep 17 00:00:00 2001 From: Christian Eickhoff Date: Fri, 23 Aug 2024 07:03:35 +0200 Subject: [PATCH 3/3] add errors-only query param --- cmd/spectral.go | 5 +++-- cmd/valigator.go | 11 ++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/spectral.go b/cmd/spectral.go index 7a882ab..9658eb4 100644 --- a/cmd/spectral.go +++ b/cmd/spectral.go @@ -9,8 +9,9 @@ import ( ) const ( - rulesetQueryParam = "ruleset" - acceptHeader = "Accept" + rulesetQueryParam = "ruleset" + errorsOnlyQueryParam = "errors-only" + acceptHeader = "Accept" ) var outputFormats = map[string]string{ diff --git a/cmd/valigator.go b/cmd/valigator.go index 11b97b6..766736d 100644 --- a/cmd/valigator.go +++ b/cmd/valigator.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "os" + "strconv" "strings" "github.com/google/uuid" @@ -153,11 +154,19 @@ 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, - DisplayOnlyFailures: context.Config.DisplayOnlyFailures, + DisplayOnlyFailures: errorsOnly, SkipRules: context.Config.SkipRules, }