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

add the mod support #10

Open
wants to merge 1 commit into
base: master
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,19 @@ func main() {
Value: "json",
Usage: "the type of swagger file (json or yaml)",
},
cli.BoolFlag{
Name: "mod, m",
Usage: "whether use the mod ",
},
}
app.Action = func(c *cli.Context) error {
if err := parser.Parse(c.String("project"),
c.String("swagger"),
c.String("output"),
c.String("type"),
c.Bool("dev")); err != nil {
c.Bool("dev"),
c.Bool("mod"),
); err != nil {
return err
}
return nil
Expand Down
6 changes: 4 additions & 2 deletions parser/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
goPaths = []string{}
goRoot = ""
devMode bool
useMod bool
)

func init() {
Expand All @@ -34,13 +35,14 @@ func init() {
}

// Parse the project by args
func Parse(projectPath, swaggerGo, output, t string, dev bool) (err error) {
func Parse(projectPath, swaggerGo, output, t string, dev bool, mod bool) (err error) {
absPPath, err := filepath.Abs(projectPath)
if err != nil {
return err
}
vendor = filepath.Join(absPPath, "vendor")
devMode = dev
useMod = mod

sw := swagger.NewV2()
if err = doc2Swagger(projectPath, swaggerGo, dev, sw); err != nil {
Expand All @@ -54,7 +56,7 @@ func Parse(projectPath, swaggerGo, output, t string, dev bool) (err error) {
switch t {
case "json":
filename = jsonFile
data, err = json.Marshal(sw)
data, err = json.MarshalIndent(sw, "", " ")
case "yaml":
filename = yamlFile
data, err = yaml.Marshal(sw)
Expand Down
15 changes: 15 additions & 0 deletions parser/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package parser

import (
"go/ast"
"golang.org/x/tools/go/packages"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -36,6 +37,20 @@ func str2RealType(s string, typ string) (ret interface{}, err error) {
}

func absPathFromGoPath(importPath string) (string, bool) {
if useMod {
//modPath, err := module.EscapePath(importPath)
x1, err := packages.Load(&packages.Config{
Mode: packages.LoadFiles,
}, importPath)
if err != nil {

}
realPath := x1[0].GoFiles[0]
x2 := strings.Split(realPath, "/")
x2 = x2[0 : len(x2)-1]
return strings.Join(x2, "/"), true
//return importPath, true
}
if vendor != "" {
vendorImport := filepath.Join(vendor, importPath)
if fileExists(vendorImport) {
Expand Down