-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
62 lines (54 loc) · 1.18 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// cmd/gowhich shows the import path of Go executables
//
// cmd/gowhich takes one argument, which is either program name or abosolute or
// relative path to an executable; when a program name is provided, it's looked up
// up in the $PATH.
//
// cmd/gowhich looks for a main.main symbol in the given executable and tries
// to guess the import name from its source files path.
//
// cmd/gowhich does not work on Go executables from $GOTOOLDIR.
//
// Example usage
//
// ~ $ gowhich godoc
// code.google.com/p/go.tools/cmd/godoc
package main
import (
"fmt"
"os"
"os/exec"
"github.com/rjeczalik/which"
)
func die(v interface{}) {
fmt.Fprintln(os.Stderr, v)
os.Exit(1)
}
const usage = `NAME:
gowhich - shows the import path of Go executables
USAGE:
gowhich name|path
EXAMPLES:
gowhich godoc
gowhich ~/bin/godoc`
func ishelp(s string) bool {
return s == "-h" || s == "-help" || s == "help" || s == "--help" || s == "/?"
}
func main() {
if len(os.Args) != 2 {
die(usage)
}
if ishelp(os.Args[1]) {
fmt.Println(usage)
return
}
path, err := exec.LookPath(os.Args[1])
if err != nil {
die(err)
}
pkg, err := which.Import(path)
if err != nil {
die(err)
}
fmt.Println(pkg)
}