Skip to content

Commit

Permalink
Merge pull request #9 from wy-z/dev
Browse files Browse the repository at this point in the history
[tspec] Fix 'tspec.Import'
  • Loading branch information
wy-z authored Sep 11, 2017
2 parents df5eb23 + 071bf61 commit ddf0833
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
17 changes: 2 additions & 15 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"encoding/json"
"fmt"
"go/build"
"os"

"github.com/urfave/cli"
Expand Down Expand Up @@ -45,22 +44,10 @@ func Run(version string) {
return
}

wd, err := os.Getwd()
if err != nil {
msg := fmt.Sprintf("failed to get working dir: %s", err)
err = cli.NewExitError(msg, 1)
return
}
importPkg, err := build.Import(opts.PkgPath, wd, build.ImportComment)
if err != nil {
msg := fmt.Sprintf("failed to import pkg %s: %s", opts.PkgPath, err)
err = cli.NewExitError(msg, 1)
return
}
parser := tspec.NewParser()
pkg, err := parser.ParseDir(opts.PkgPath, importPkg.Name)
pkg, err := parser.Import(opts.PkgPath)
if err != nil {
msg := fmt.Sprintf("failed to parse pkg %s: %s", opts.PkgPath, err)
msg := fmt.Sprintf("failed to import pkg %s: %s", pkg.Name, err)
err = cli.NewExitError(msg, 1)
return
}
Expand Down
9 changes: 4 additions & 5 deletions tspec/tspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ func (t *Parser) ParseDir(dirPath string, pkgName string) (pkg *ast.Package, err
return
}

// Import parses import spec and returns related package
func (t *Parser) Import(ispec *ast.ImportSpec) (pkg *ast.Package, err error) {
pkgPath := strings.Trim(ispec.Path.Value, "\"")

// Import imports package dir and returns related package
func (t *Parser) Import(pkgPath string) (pkg *ast.Package, err error) {
wd, err := os.Getwd()
if err != nil {
err = errors.WithStack(err)
Expand Down Expand Up @@ -137,7 +135,8 @@ func (t *Parser) parseTypeStr(oPkg *ast.Package, typeStr string) (pkg *ast.Packa
var p *ast.Package
for _, file := range oPkg.Files {
for _, ispec := range file.Imports {
p, err = t.Import(ispec)
pkgPath := strings.Trim(ispec.Path.Value, "\"")
p, err = t.Import(pkgPath)
if err != nil {
err = errors.WithStack(err)
return
Expand Down
4 changes: 3 additions & 1 deletion tspec/tspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go/ast"
"go/parser"
"go/token"
"strings"
"testing"

"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -73,7 +74,8 @@ import (
"github.com/wy-z/tspec/samples"
)
`, parser.ImportsOnly)
pkg, err := tspec.NewParser().Import(f.Imports[0])
pkgPath := strings.Trim(f.Imports[0].Path.Value, "\"")
pkg, err := tspec.NewParser().Import(pkgPath)
if err != nil {
msg := fmt.Sprintf("failed to import 'github.com/wy-z/tspec/samples': %s", err)
panic(msg)
Expand Down

0 comments on commit ddf0833

Please sign in to comment.