Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return errors only HSP-9244 #5

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions cmd/spectral.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"io/ioutil"
"errors"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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!")
Expand All @@ -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
Expand Down
33 changes: 20 additions & 13 deletions cmd/valigator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -176,7 +179,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")
eic-chr marked this conversation as resolved.
Show resolved Hide resolved
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" ]
}
}
Loading