Skip to content

Commit 71febea

Browse files
authored
add convenience method for linting (#53)
[Spectral](https://stoplight.io/open-source/spectral) is an open-source API style guide enforcer and linter. This adds a convenience method for linting in Julia using spectral. ```julia OpenAPI.lint( spec::AbstractString; # the OpenAPI specification to use use_sudo::Bool=false # whether to use sudo while invoking docker ) OpenAPI.lint( spec_dir::AbstractString; # folder containing the specification file spec_file::AbstractString; # the specification file use_sudo::Bool=false # whether to use sudo while invoking docker ) ```
1 parent 3330e23 commit 71febea

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,25 @@ OpenAPI.stop_swagger_editor(;
314314
)
315315
```
316316

317+
### Spectral Linter
318+
319+
[Spectral](https://stoplight.io/open-source/spectral) is an open-source API style guide enforcer and linter. OpenAPI.jl includes a convenience method to use the Spectral OpenAPI linter from Julia.
320+
321+
```julia
322+
# specify a specification file to start with
323+
OpenAPI.lint(
324+
spec::AbstractString; # the OpenAPI specification to use
325+
use_sudo::Bool=false # whether to use sudo while invoking docker
326+
)
327+
328+
# specify a folder and specification file name to start with
329+
OpenAPI.lint(
330+
spec_dir::AbstractString; # folder containing the specification file
331+
spec_file::AbstractString; # the specification file
332+
use_sudo::Bool=false # whether to use sudo while invoking docker
333+
)
334+
```
335+
317336
## TODO
318337

319338
Not all OpenAPI features are supported yet, e.g.:

src/tools.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,39 @@ function swagger_editor(; port::Int=8080, use_sudo::Bool=false)
128128
cmd = `$docker run -d --rm -p $port:8080 $(SwaggerImage.Editor)`
129129
return _start_swagger(cmd, port)
130130
end
131+
132+
"""
133+
lint(spec; use_sudo=false)
134+
lint(spec_dir, spec_file; use_sudo=false)
135+
136+
Lint an OpenAPI spec file using Spectral.
137+
138+
Optional arguments:
139+
- `use_sudo`: Whether to use `sudo` to run Docker commands. Defaults to false.
140+
"""
141+
function lint(spec::AbstractString; use_sudo::Bool=false)
142+
spec = abspath(spec)
143+
spec_dir = dirname(spec)
144+
spec_file = basename(spec)
145+
return lint(spec_dir, spec_file; use_sudo=use_sudo)
146+
end
147+
148+
function lint(spec_dir::AbstractString, spec_file::AbstractString; use_sudo::Bool=false)
149+
docker = docker_cmd(; use_sudo=use_sudo)
150+
if isfile(joinpath(spec_dir, ".spectral.yaml"))
151+
@debug("linting with existing configuration")
152+
cmd = `$docker run --rm -v $spec_dir:/spec:ro -w /spec stoplight/spectral:latest lint /spec/$spec_file`
153+
run(cmd)
154+
else
155+
# generate a default configuration file
156+
@debug("linting with default configuration")
157+
mktempdir() do tmpdir
158+
open(joinpath(tmpdir, ".spectral.yaml"), "w") do f
159+
write(f, """extends: ["spectral:oas", "spectral:asyncapi"]""")
160+
end
161+
cp(joinpath(spec_dir, spec_file), joinpath(tmpdir, spec_file))
162+
cmd = `$docker run --rm -v $tmpdir:/spec:ro -w /spec stoplight/spectral:latest lint /spec/$spec_file`
163+
run(cmd)
164+
end
165+
end
166+
end

0 commit comments

Comments
 (0)