/
main.go
207 lines (194 loc) · 6.39 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Command gobin Go commands by reading their import paths, go-getting
// their repositories and building new executables.
//
// Command gobin can search for and list Go executables, create a $GOPATH workspace
// out of a binary. It can also update an executable which does not have its
// sources fetched in current $GOPATH.
//
// Source
//
// Command gobin can guess an origin of the sources used to build Go executables
// it finds on a system. It can be used to create precise mirror of the sources
// for system Go executables, without any unneeded packages.
//
// ~ $ gobin -s /tmp/gopath godoc golint goimports gotree gowhich
// code.google.com/p/go.tools (download)
// github.com/rjeczalik/tools (download)
// github.com/rjeczalik/which (download)
// (...)
// github.com/rjeczalik/which/cmd/gowhich
// code.google.com/p/go.tools/cmd/godoc
// ~ $ tree -L 3 /tmp/gopath/src/
// /tmp/gopath/src/
// ├── code.google.com
// │ └── p
// │ └── go.tools
// └── github.com
// └── rjeczalik
// ├── tools
// └── which
//
// 7 directories, 0 files
//
// Update
//
// Command gobin can update single executable or automagically discover all of
// them in directories specified in $GOPATH, $GOBIN and $GOPATH environment
// variables.
//
// Executing gobin without any arguments makes it list all Go executables found
// in $GOPATH, $GOBIN and $GOPATH.
//
// Updating multiple executables is performed on multiple goroutines, bumping
// up $GOMAXPROCS environment variable may speed up the overall run-time
// significantly.
//
// Example
//
// ~ $ GOMAXPROCS=2 gobin -u
// ok /home/rjeczalik/bin/goimports (code.google.com/p/go.tools/cmd/goimports) 13.966s
// ok /home/rjeczalik/bin/godoc (code.google.com/p/go.tools/cmd/godoc) 17.960s
// ok /home/rjeczalik/bin/pulsecli (github.com/x-formation/pulsekit/cmd/pulsecli) 13.052s
// ok /home/rjeczalik/workspace/bin/pulsecli (github.com/x-formation/pulsekit/cmd/pulsecli) 13.052s
//
// Usage
//
// NAME:
// gobin - looks for Go executables system-wide ($PATH/$GOBIN/$GOPATH),
// lists them, fetches their sources and updates them
//
// USAGE:
// gobin [-u] [-s=.|gopath] [path|package...]
//
// FLAGS:
// -u Updates Go binaries
// -s <dir> Go-gets sources for Go specified binaries into <dir> $GOPATH
// (use '.' for current $GOPATH)
// -ldflags=<flags> passes "-ldflags=flags" to "go install"
//
// EXAMPLES:
// gobin Lists all Go binaries (looks up $PATH/$GOBIN/$GOPATH)
// gobin -s=. ~/bin Go-gets sources used to build all Go binaries in ~/bin
// into current $GOPATH
// gobin -s=/var/mirror Go-gets all sources used to build all Go binaries found
// on system into new /var/mirror $GOPATH
// gobin -u Updates all Go binaries
// gobin -u github.com Updates all Go binaries installed from github.com
// gobin ~/bin Lists all Go binaries from the ~/bin directory
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/rjeczalik/bin"
)
func die(v interface{}) {
fmt.Fprintln(os.Stderr, v)
os.Exit(1)
}
const usage = `NAME:
gobin - performs discovery of Go executables ($PATH/$GOBIN/$GOPATH),
lists them, fetches their sources and updates them
USAGE:
gobin [-u] [-s=.|gopath] [path|package...]
FLAGS:
-u Updates Go binaries
-s <dir> Go-gets sources for Go specified binaries into <dir> $GOPATH
(use '.' for current $GOPATH)
-ldflags=<flags> passes "-ldflags=flags" to "go install"
EXAMPLES:
gobin Lists all Go binaries (looks up $PATH/$GOBIN/$GOPATH)
gobin -s=. ~/bin Go-gets sources used to build all Go binaries in ~/bin
into current $GOPATH
gobin -s=/var/mirror Go-gets all sources used to build all Go binaries found
on system into new /var/mirror $GOPATH
gobin -u Updates all Go binaries in-place
gobin -u github.com Updates all Go binaries installed from github.com
gobin ~/bin Lists all Go binaries from the ~/bin directory
gobin -u -ldflags='-w -s' Updates all go binaries in-place, using "go install -ldflags='-w -s'"`
var (
source, ldflags string
update bool
)
func ishelp(s string) bool {
return s == "-h" || s == "-help" || s == "help" || s == "--help" || s == "/?"
}
func parse() []string {
flag.Usage = func() { die(usage) }
flag.StringVar(&source, "s", "", "")
flag.BoolVar(&update, "u", false, "")
flag.StringVar(&ldflags, "ldflags", "", "")
flag.Parse()
return flag.Args()
}
func self() string {
if strings.Contains(os.Args[0], string(os.PathSeparator)) {
if self, err := filepath.Abs(os.Args[0]); err == nil {
if fiself, err := os.Stat(self); err == nil {
if fiargs, err := os.Stat(os.Args[0]); err == nil && os.SameFile(fiself, fiargs) {
return self
}
}
}
}
if self, err := exec.LookPath(filepath.Base(os.Args[0])); err == nil {
return self
}
return ""
}
func log(b *bin.Bin, d time.Duration, err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "fail\t%s\t(%s)\n", b.Path, b.Package)
fmt.Fprintf(os.Stderr, "\terror: %v\n", err)
} else {
fmt.Printf("ok\t%s\t(%s)\t%.3fs\n", b.Path, b.Package, d.Seconds())
}
}
// TODO(rjeczalik): Bin.CanWrite needs a Flock here
func main() {
if len(os.Args) == 2 && ishelp(os.Args[1]) {
fmt.Println(usage)
return
}
var b, e = bin.Search(parse())
if e != nil {
die(e)
}
var installFlags []string
switch {
case update:
if self := self(); self != "" {
for i := range b {
if b[i].Path == self {
b[i], b = b[len(b)-1], b[:len(b)-1]
break
}
}
}
if ldflags != "" {
installFlags = append(installFlags, "-ldflags="+ldflags)
}
bin.Update(bin.UpdateOpts{Bins: b, Log: log, Flags: installFlags})
case source != "":
if source == "." {
source = os.Getenv("GOPATH")
if source == "" {
die("bin: unable to read current $GOPATH or $GOPATH is empty")
}
if i := strings.Index(source, string(os.PathListSeparator)); i != -1 {
source = source[:i]
}
}
if bin.Source(b, source) != nil {
os.Exit(1)
}
default:
for i := range b {
fmt.Printf("%s\t(%s)\n", b[i].Path, b[i].Package)
}
}
}