Skip to content

Commit

Permalink
add convenience method for linting (#53)
Browse files Browse the repository at this point in the history
[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
)
```
  • Loading branch information
tanmaykm authored Aug 7, 2023
1 parent 3330e23 commit 71febea
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,25 @@ OpenAPI.stop_swagger_editor(;
)
```

### Spectral Linter

[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.

```julia
# specify a specification file to start with
OpenAPI.lint(
spec::AbstractString; # the OpenAPI specification to use
use_sudo::Bool=false # whether to use sudo while invoking docker
)

# specify a folder and specification file name to start with
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
)
```

## TODO

Not all OpenAPI features are supported yet, e.g.:
Expand Down
36 changes: 36 additions & 0 deletions src/tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,39 @@ function swagger_editor(; port::Int=8080, use_sudo::Bool=false)
cmd = `$docker run -d --rm -p $port:8080 $(SwaggerImage.Editor)`
return _start_swagger(cmd, port)
end

"""
lint(spec; use_sudo=false)
lint(spec_dir, spec_file; use_sudo=false)
Lint an OpenAPI spec file using Spectral.
Optional arguments:
- `use_sudo`: Whether to use `sudo` to run Docker commands. Defaults to false.
"""
function lint(spec::AbstractString; use_sudo::Bool=false)
spec = abspath(spec)
spec_dir = dirname(spec)
spec_file = basename(spec)
return lint(spec_dir, spec_file; use_sudo=use_sudo)
end

function lint(spec_dir::AbstractString, spec_file::AbstractString; use_sudo::Bool=false)
docker = docker_cmd(; use_sudo=use_sudo)
if isfile(joinpath(spec_dir, ".spectral.yaml"))
@debug("linting with existing configuration")
cmd = `$docker run --rm -v $spec_dir:/spec:ro -w /spec stoplight/spectral:latest lint /spec/$spec_file`
run(cmd)
else
# generate a default configuration file
@debug("linting with default configuration")
mktempdir() do tmpdir
open(joinpath(tmpdir, ".spectral.yaml"), "w") do f
write(f, """extends: ["spectral:oas", "spectral:asyncapi"]""")
end
cp(joinpath(spec_dir, spec_file), joinpath(tmpdir, spec_file))
cmd = `$docker run --rm -v $tmpdir:/spec:ro -w /spec stoplight/spectral:latest lint /spec/$spec_file`
run(cmd)
end
end
end

2 comments on commit 71febea

@tanmaykm
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/89179

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.15 -m "<description of version>" 71febeac30ff1298d879101121f367f11103c363
git push origin v0.1.15

Please sign in to comment.