diff --git a/cmd/spectral.go b/cmd/spectral.go index ca90484..9658eb4 100644 --- a/cmd/spectral.go +++ b/cmd/spectral.go @@ -1,7 +1,7 @@ package main import ( - "io/ioutil" + "errors" "log" "os" "os/exec" @@ -9,8 +9,9 @@ import ( ) const ( - rulesetQueryParam = "ruleset" - acceptHeader = "Accept" + rulesetQueryParam = "ruleset" + errorsOnlyQueryParam = "errors-only" + acceptHeader = "Accept" ) var outputFormats = map[string]string{ @@ -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 { @@ -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) } @@ -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!") @@ -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 diff --git a/cmd/valigator.go b/cmd/valigator.go index 48bf80a..766736d 100644 --- a/cmd/valigator.go +++ b/cmd/valigator.go @@ -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) @@ -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) @@ -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!") 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 +}