Skip to content

Commit

Permalink
cmd/vfsgendev: Improve formatting of errors.
Browse files Browse the repository at this point in the history
Helps #36.
  • Loading branch information
dmitshur committed Nov 28, 2017
1 parent f3d80d2 commit bb654ea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cmd/vfsgendev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ func main() {
importPath, variableName, err := parseSourceFlag(*sourceFlag)
if err != nil {
fmt.Fprintln(os.Stderr, "-source flag has invalid value:", err)
fmt.Fprintln(os.Stderr)
flag.Usage()
os.Exit(2)
}
tag, err := parseTagFlag(*tagFlag)
if err != nil {
fmt.Fprintln(os.Stderr, "-tag flag has invalid value:", err)
fmt.Fprintln(os.Stderr)
flag.Usage()
os.Exit(2)
}
Expand Down
15 changes: 13 additions & 2 deletions cmd/vfsgendev/parse.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"bytes"
"fmt"
"go/ast"
"go/build"
"go/doc"
"go/parser"
"go/printer"
"go/token"
"path/filepath"
"strconv"
Expand All @@ -24,11 +26,11 @@ func parseSourceFlag(sourceFlag string) (importPath, variableName string, err er
}
se, ok := e.(*ast.SelectorExpr)
if !ok {
return "", "", fmt.Errorf("invalid format, expression %v is not a selector expression but %T", e, e)
return "", "", fmt.Errorf("invalid format, expression %v is not a selector expression but %T", sourceFlag, e)
}
importPath, err = stringValue(se.X)
if err != nil {
return "", "", fmt.Errorf("invalid format, expression %v is not a properly quoted Go string: %v", se.X, err)
return "", "", fmt.Errorf("invalid format, expression %v is not a properly quoted Go string: %v", stringifyAST(se.X), err)
}
variableName = se.Sel.Name
return importPath, variableName, nil
Expand Down Expand Up @@ -75,6 +77,15 @@ func lookupNameAndComment(bctx build.Context, importPath, variableName string) (
return bpkg.Name, variableComment, nil
}

func stringifyAST(node interface{}) string {
var buf bytes.Buffer
err := printer.Fprint(&buf, token.NewFileSet(), node)
if err != nil {
return "printer.Fprint error: " + err.Error()
}
return buf.String()
}

// TODO: Keep in sync or unify with github.com/shurcooL/cmd/gorepogen/main.go.
// TODO: See if these can be cleaned up.

Expand Down

0 comments on commit bb654ea

Please sign in to comment.