forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/swaggo/swag/golist.go

130 lines
1.7 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
package swag
import (
"bytes"
"context"
"encoding/json"
"fmt"
"go/build"
"os/exec"
"path/filepath"
)
func listPackages(ctx context.Context, dir string, env []string, args ...string) (pkgs []*build.Package, finalErr error) {
2024-05-14 13:07:09 +00:00
cmd := exec.CommandContext(ctx, "go", append([]string{"list", "-json", "-e"}, args...)...)
2024-05-14 13:07:09 +00:00
cmd.Env = env
2024-05-14 13:07:09 +00:00
cmd.Dir = dir
stdout, err := cmd.StdoutPipe()
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
var stderrBuf bytes.Buffer
2024-05-14 13:07:09 +00:00
cmd.Stderr = &stderrBuf
2024-05-14 13:07:09 +00:00
defer func() {
2024-05-14 13:07:09 +00:00
if stderrBuf.Len() > 0 {
2024-05-14 13:07:09 +00:00
finalErr = fmt.Errorf("%v\n%s", finalErr, stderrBuf.Bytes())
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}()
err = cmd.Start()
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
dec := json.NewDecoder(stdout)
2024-05-14 13:07:09 +00:00
for dec.More() {
2024-05-14 13:07:09 +00:00
var pkg build.Package
2024-05-14 13:07:09 +00:00
err = dec.Decode(&pkg)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
pkgs = append(pkgs, &pkg)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
err = cmd.Wait()
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return pkgs, nil
2024-05-14 13:07:09 +00:00
}
func (parser *Parser) getAllGoFileInfoFromDepsByList(pkg *build.Package, parseFlag ParseFlag) error {
2024-05-14 13:07:09 +00:00
ignoreInternal := pkg.Goroot && !parser.ParseInternal
2024-05-14 13:07:09 +00:00
if ignoreInternal { // ignored internal
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
if parser.skipPackageByPrefix(pkg.ImportPath) {
2024-05-14 13:07:09 +00:00
return nil // ignored by user-defined package path prefixes
2024-05-14 13:07:09 +00:00
}
srcDir := pkg.Dir
2024-05-14 13:07:09 +00:00
var err error
2024-05-14 13:07:09 +00:00
for i := range pkg.GoFiles {
2024-05-14 13:07:09 +00:00
err = parser.parseFile(pkg.ImportPath, filepath.Join(srcDir, pkg.GoFiles[i]), nil, parseFlag)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
// parse .go source files that import "C"
2024-05-14 13:07:09 +00:00
for i := range pkg.CgoFiles {
2024-05-14 13:07:09 +00:00
err = parser.parseFile(pkg.ImportPath, filepath.Join(srcDir, pkg.CgoFiles[i]), nil, parseFlag)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
return nil
2024-05-14 13:07:09 +00:00
}