forked from lazada/swgui
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI tool to inspect OpenAPI schemas (#39)
- Loading branch information
Showing
6 changed files
with
231 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# This script is provided by github.com/bool64/dev. | ||
|
||
# This script uploads application binaries as GitHub release assets. | ||
name: release-assets | ||
on: | ||
release: | ||
types: | ||
- created | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
GO_VERSION: 1.20.x | ||
jobs: | ||
build: | ||
name: Upload Release Assets | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Install Go stable | ||
if: env.GO_VERSION != 'tip' | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: ${{ env.GO_VERSION }} | ||
- name: Install Go tip | ||
if: env.GO_VERSION == 'tip' | ||
run: | | ||
curl -sL https://storage.googleapis.com/go-build-snap/go/linux-amd64/$(git ls-remote https://github.com/golang/go.git HEAD | awk '{print $1;}').tar.gz -o gotip.tar.gz | ||
ls -lah gotip.tar.gz | ||
mkdir -p ~/sdk/gotip | ||
tar -C ~/sdk/gotip -xzf gotip.tar.gz | ||
~/sdk/gotip/bin/go version | ||
echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
- name: Build artifacts | ||
run: | | ||
make release-assets | ||
- name: Upload linux_amd64.tar.gz | ||
if: hashFiles('linux_amd64.tar.gz') != '' | ||
uses: actions/upload-release-asset@v1 | ||
with: | ||
upload_url: ${{ github.event.release.upload_url }} | ||
asset_path: ./linux_amd64.tar.gz | ||
asset_name: linux_amd64.tar.gz | ||
asset_content_type: application/tar+gzip | ||
- name: Upload linux_amd64_dbg.tar.gz | ||
if: hashFiles('linux_amd64.tar.gz') != '' | ||
uses: actions/upload-release-asset@v1 | ||
with: | ||
upload_url: ${{ github.event.release.upload_url }} | ||
asset_path: ./linux_amd64_dbg.tar.gz | ||
asset_name: linux_amd64_dbg.tar.gz | ||
asset_content_type: application/tar+gzip | ||
- name: Upload linux_arm64.tar.gz | ||
if: hashFiles('linux_arm64.tar.gz') != '' | ||
uses: actions/upload-release-asset@v1 | ||
with: | ||
upload_url: ${{ github.event.release.upload_url }} | ||
asset_path: ./linux_arm64.tar.gz | ||
asset_name: linux_arm64.tar.gz | ||
asset_content_type: application/tar+gzip | ||
- name: Upload linux_arm.tar.gz | ||
if: hashFiles('linux_arm.tar.gz') != '' | ||
uses: actions/upload-release-asset@v1 | ||
with: | ||
upload_url: ${{ github.event.release.upload_url }} | ||
asset_path: ./linux_arm.tar.gz | ||
asset_name: linux_arm.tar.gz | ||
asset_content_type: application/tar+gzip | ||
- name: Upload darwin_amd64.tar.gz | ||
if: hashFiles('darwin_amd64.tar.gz') != '' | ||
uses: actions/upload-release-asset@v1 | ||
with: | ||
upload_url: ${{ github.event.release.upload_url }} | ||
asset_path: ./darwin_amd64.tar.gz | ||
asset_name: darwin_amd64.tar.gz | ||
asset_content_type: application/tar+gzip | ||
- name: Upload darwin_arm64.tar.gz | ||
if: hashFiles('darwin_arm64.tar.gz') != '' | ||
uses: actions/upload-release-asset@v1 | ||
with: | ||
upload_url: ${{ github.event.release.upload_url }} | ||
asset_path: ./darwin_arm64.tar.gz | ||
asset_name: darwin_arm64.tar.gz | ||
asset_content_type: application/tar+gzip | ||
- name: Upload windows_amd64.zip | ||
if: hashFiles('windows_amd64.zip') != '' | ||
uses: actions/upload-release-asset@v1 | ||
with: | ||
upload_url: ${{ github.event.release.upload_url }} | ||
asset_path: ./windows_amd64.zip | ||
asset_name: windows_amd64.zip | ||
asset_content_type: application/zip | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Package main provides CLI tool to inspect OpenAPI schemas with Swagger UI. | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"os/exec" | ||
"path" | ||
"runtime" | ||
|
||
"github.com/bool64/dev/version" | ||
swgui "github.com/swaggest/swgui/v5emb" | ||
) | ||
|
||
func main() { | ||
ver := flag.Bool("version", false, "Show version and exit.") | ||
flag.Parse() | ||
|
||
if *ver { | ||
fmt.Printf("%s, Swagger UI %s\n", version.Info().Version, "v5.1.3") | ||
|
||
return | ||
} | ||
|
||
if flag.NArg() < 1 { | ||
fmt.Println("Usage: swgui <path-to-schema>") | ||
flag.PrintDefaults() | ||
|
||
return | ||
} | ||
|
||
filePathToSchema := flag.Arg(0) | ||
urlToSchema := "/" + path.Base(filePathToSchema) | ||
|
||
swh := swgui.NewHandler(filePathToSchema, urlToSchema, "/") | ||
hh := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path == urlToSchema { | ||
http.ServeFile(rw, r, filePathToSchema) | ||
} | ||
|
||
swh.ServeHTTP(rw, r) | ||
}) | ||
|
||
srv := httptest.NewServer(hh) | ||
|
||
log.Println("Starting Swagger UI server at", srv.URL) | ||
log.Println("Press Ctrl+C to stop") | ||
|
||
if err := open(srv.URL); err != nil { | ||
log.Println("open browser:", err.Error()) | ||
} | ||
|
||
<-make(chan struct{}) | ||
} | ||
|
||
// open opens the specified URL in the default browser of the user. | ||
func open(url string) error { | ||
var ( | ||
cmd string | ||
args []string | ||
) | ||
|
||
switch runtime.GOOS { | ||
case "windows": | ||
cmd = "cmd" | ||
args = []string{"/c", "start"} | ||
case "darwin": | ||
cmd = "open" | ||
default: // "linux", "freebsd", "openbsd", "netbsd" | ||
cmd = "xdg-open" | ||
} | ||
|
||
args = append(args, url) | ||
|
||
return exec.Command(cmd, args...).Start() //nolint:gosec | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters