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

remove stdin communication with goctl #81

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# goctl-swagger
本库对goctl-swagger做以下修改
- 不在通过os.stdin读取解析结果,直接调用goctl parser解析
- 增加参数
- -api: 指定api文件
- -dir: 指定输出文件夹

本机运行命令
```
go build -o swagger.exe
./swagger.exe swagger --filename sso.json -api sso.api -dir . -host 127.0.0.2 -basepath /api
```

安装使用
```
GOPROXY=https://goproxy.cn/,direct go install github.com/shyandsy/goctl-swagger@latest
goctl-swagger swagger --filename sso.json -api sso.api -dir . -host 127.0.0.2 -basepath /api
```

# 原版文档
### 1. 编译goctl-swagger插件

```
Expand Down
69 changes: 67 additions & 2 deletions action/action.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,88 @@
package action

import (
"encoding/json"
"github.com/shyandsy/goctl-swagger/generate"
"github.com/urfave/cli/v2"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/plugin"
"github.com/zeromicro/goctl-swagger/generate"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"path/filepath"
)

func Generator(ctx *cli.Context) error {
fileName := ctx.String("filename")
apiFileName := ctx.String("api")
generatedDir := ctx.String("dir")

if len(fileName) == 0 {
fileName = "rest.swagger.json"
}

p, err := plugin.NewPlugin()
content, err := prepareArgs(apiFileName, generatedDir)
if err != nil {
panic(err)
}

p, err := NewPlugin(content)
if err != nil {
return err
}
basepath := ctx.String("basepath")
host := ctx.String("host")
return generate.Do(fileName, host, basepath, p)
}

func NewPlugin(content []byte) (*plugin.Plugin, error) {
var plugin plugin.Plugin

var info struct {
ApiFilePath string
Style string
Dir string
}
err := json.Unmarshal(content, &info)
if err != nil {
return nil, err
}

plugin.ApiFilePath = info.ApiFilePath
plugin.Style = info.Style
plugin.Dir = info.Dir
api, err := parser.Parse(info.ApiFilePath)
if err != nil {
return nil, err
}

plugin.Api = api
return &plugin, nil
}

func prepareArgs(apiPath string, generatedDir string) ([]byte, error) {
var transferData plugin.Plugin
if len(apiPath) > 0 && pathx.FileExists(apiPath) {
api, err := parser.Parse(apiPath)
if err != nil {
return nil, err
}
transferData.Api = api
}
absApiFilePath, err := filepath.Abs(apiPath)
if err != nil {
return nil, err
}
transferData.ApiFilePath = absApiFilePath
dirAbs, err := filepath.Abs(generatedDir)
if err != nil {
return nil, err
}

transferData.Dir = dirAbs
transferData.Style = config.DefaultFormat //VarStringStyle
content, err := json.Marshal(transferData)
if err != nil {
return nil, err
}
return content, nil
}
59 changes: 57 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/zeromicro/goctl-swagger
module github.com/shyandsy/goctl-swagger

go 1.16
go 1.18

require (
github.com/grpc-ecosystem/grpc-gateway v1.16.0
Expand All @@ -9,3 +9,58 @@ require (
github.com/zeromicro/go-zero/tools/goctl v1.5.4
golang.org/x/oauth2 v0.7.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gookit/color v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/openzipkin/zipkin-go v0.4.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/zeromicro/antlr v0.0.1 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/jaeger v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/zipkin v1.14.0 // indirect
go.opentelemetry.io/otel/sdk v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/automaxprocs v1.5.2 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.56.2 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading