-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgodir.go
126 lines (110 loc) · 2.68 KB
/
godir.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"os"
"path/filepath"
putil "github.com/Masterminds/godir/pathutil"
"github.com/urfave/cli"
)
var version = "0.0.1"
var Description = `godir - The Go Path Tool
This tool is a utility for working with paths that are significant for Go.
Unless otherwise noted, the tools here are designed to ignore directories that
Go treats as special, including prefixed directories ("_scripts/"), test
directories ("testdata/") and the vendor directory ("vendor/").
EXAMPLES:
Run "go test" without running the tests in vendor:
$ go test $(godir pkgs)
Given a case where $GOPATH has more than one path, show which path the current
code lives on.
$ GOPATH=~/Code/Go:~/Code/tmp godir gopath
/Users/mbutcher/Code/Go
`
func main() {
app := cli.NewApp()
app.Name = "godir"
app.Usage = Description
app.Version = version
app.Action = listPackages
commands(app)
app.Run(os.Args)
}
func commands(app *cli.App) {
app.Commands = []cli.Command{
{
Name: "name",
Usage: "Print the name of this package, relative to $GOPATH.",
ArgsUsage: "[PATH]",
Action: func(c *cli.Context) error {
fmt.Println(putil.Name(argOrWdir(c)))
return nil
},
},
{
Name: "gopath",
Usage: "Print the path in $GOPATH that the given package was found on.\n\tThis is for cases where $GOPATH has numerous paths.",
ArgsUsage: "[PATH]",
Action: func(c *cli.Context) error {
fmt.Println(putil.WhichGopath(argOrWdir(c)))
return nil
},
},
{
Name: "pkgs",
Usage: "Print all packages (that contain Go code) from the current directory. Skip vendor/",
Action: listPackages,
},
{
Name: "paths",
Usage: "Print all subpaths from the current directory.",
Action: func(c *cli.Context) error {
wd := argOrWdir(c)
sp := putil.Subpaths(wd, false)
r := c.Bool("relative")
for _, p := range sp {
if r {
rp, err := filepath.Rel(wd, p)
if err == nil {
p = rp
}
}
fmt.Println(p)
}
return nil
},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "relative,r",
Usage: "Print paths relative to the directory.",
},
},
},
}
}
func listPackages(c *cli.Context) error {
sp := putil.Subpaths(argOrWdir(c), true)
for _, p := range sp {
fmt.Println(putil.Name(p))
}
return nil
}
func argOrWdir(c *cli.Context) string {
a := c.Args()
if len(a) < 1 {
return wdir()
}
p, err := filepath.Abs(a[0])
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get absolute path of %s: %s", a[0], err)
os.Exit(1)
}
return p
}
func wdir() string {
wd, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "Could not get working directory: %s.\n", err)
os.Exit(1)
}
return wd
}