Skip to content
Permalink
Browse files

refactor to place high-level code on top

For consistency and better readability.

Also, do all the flag handling inside main, and pass the file
parameter to run. This reduces reliance of run on external state.
  • Loading branch information...
dmitshur committed Mar 2, 2019
1 parent 974a64d commit f27e3c4457117258b7e471ac9ce256e551ced60b
Showing with 32 additions and 31 deletions.
  1. +32 −31 200/cmd/importpathof/main.go
@@ -16,6 +16,38 @@ import (
"strings"
)

func main() {
flag.Parse()
if flag.NArg() != 1 {
fmt.Fprintln(os.Stderr, "usage: importpathof file")
os.Exit(2)
}
file := flag.Arg(0)

err := run(file)
if err != nil {
log.Fatalln(err)
}
}

func run(file string) error {
table, err := table(file)
if err != nil {
return err
}
mainFile, err := mainFile(table)
if err != nil {
return err
}
importPath, err := importPath(mainFile)
if err != nil {
return err
}

fmt.Println(importPath)
return nil
}

// table extracts a Go symbol and line number table embedded in Go binary
// that file points to.
func table(file string) (*gosym.Table, error) {
@@ -109,34 +141,3 @@ func importPath(file string) (string, error) {
}
return "", fmt.Errorf("couldn't find an import path corresponding to %q", file)
}

func run() error {
table, err := table(flag.Arg(0))
if err != nil {
return err
}
file, err := mainFile(table)
if err != nil {
return err
}
importPath, err := importPath(file)
if err != nil {
return err
}

fmt.Println(importPath)
return nil
}

func main() {
flag.Parse()
if flag.NArg() != 1 {
fmt.Fprintln(os.Stderr, "usage: importpathof file")
os.Exit(2)
}

err := run()
if err != nil {
log.Fatalln(err)
}
}

0 comments on commit f27e3c4

Please sign in to comment.
You can’t perform that action at this time.