summaryrefslogtreecommitdiff
path: root/src/cmd/go/list.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/go/list.go')
-rw-r--r--src/cmd/go/list.go78
1 files changed, 43 insertions, 35 deletions
diff --git a/src/cmd/go/list.go b/src/cmd/go/list.go
index edb59aa79..2d23d077e 100644
--- a/src/cmd/go/list.go
+++ b/src/cmd/go/list.go
@@ -9,11 +9,12 @@ import (
"encoding/json"
"io"
"os"
+ "strings"
"text/template"
)
var cmdList = &Command{
- UsageLine: "list [-e] [-f format] [-json] [packages]",
+ UsageLine: "list [-e] [-f format] [-json] [-tags 'tag list'] [packages]",
Short: "list packages",
Long: `
List lists the packages named by the import paths, one per line.
@@ -24,10 +25,10 @@ The default output shows the package import path:
code.google.com/p/goauth2/oauth
code.google.com/p/sqlite
-The -f flag specifies an alternate format for the list,
-using the syntax of package template. The default output
-is equivalent to -f '{{.ImportPath}}'. The struct
-being passed to the template is:
+The -f flag specifies an alternate format for the list, using the
+syntax of package template. The default output is equivalent to -f
+'{{.ImportPath}}'. One extra template function is available, "join",
+which calls strings.Join. The struct being passed to the template is:
type Package struct {
Dir string // directory containing package sources
@@ -41,12 +42,15 @@ being passed to the template is:
Root string // Go root or Go path dir containing this package
// Source files
- GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
- CgoFiles []string // .go sources files that import "C"
- CFiles []string // .c source files
- HFiles []string // .h source files
- SFiles []string // .s source files
- SysoFiles []string // .syso object files to add to archive
+ GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
+ CgoFiles []string // .go sources files that import "C"
+ IgnoredGoFiles []string // .go sources ignored due to build constraints
+ CFiles []string // .c source files
+ HFiles []string // .h source files
+ SFiles []string // .s source files
+ SysoFiles []string // .syso object files to add to archive
+ SwigFiles []string // .swig files
+ SwigCXXFiles []string // .swigcxx files
// Cgo directives
CgoCFLAGS []string // cgo: flags for C compiler
@@ -81,6 +85,9 @@ printing. Erroneous packages will have a non-empty ImportPath and
a non-nil Error field; other information may or may not be missing
(zeroed).
+The -tags flag specifies a list of build tags, like in the 'go build'
+command.
+
For more about specifying packages, see 'go help packages'.
`,
}
@@ -88,6 +95,7 @@ For more about specifying packages, see 'go help packages'.
func init() {
cmdList.Run = runList // break init cycle
cmdList.Flag.Var(buildCompiler{}, "compiler", "")
+ cmdList.Flag.Var((*stringsFlag)(&buildContext.BuildTags), "tags", "")
}
var listE = cmdList.Flag.Bool("e", false, "")
@@ -96,7 +104,7 @@ var listJson = cmdList.Flag.Bool("json", false, "")
var nl = []byte{'\n'}
func runList(cmd *Command, args []string) {
- out := newCountingWriter(os.Stdout)
+ out := newTrackingWriter(os.Stdout)
defer out.w.Flush()
var do func(*Package)
@@ -111,18 +119,17 @@ func runList(cmd *Command, args []string) {
out.Write(nl)
}
} else {
- tmpl, err := template.New("main").Parse(*listFmt)
+ tmpl, err := template.New("main").Funcs(template.FuncMap{"join": strings.Join}).Parse(*listFmt)
if err != nil {
fatalf("%s", err)
}
do = func(p *Package) {
- out.Reset()
if err := tmpl.Execute(out, p); err != nil {
out.Flush()
fatalf("%s", err)
}
- if out.Count() > 0 {
- out.w.WriteRune('\n')
+ if out.NeedNL() {
+ out.Write([]byte{'\n'})
}
}
}
@@ -137,32 +144,33 @@ func runList(cmd *Command, args []string) {
}
}
-// CountingWriter counts its data, so we can avoid appending a newline
-// if there was no actual output.
-type CountingWriter struct {
- w *bufio.Writer
- count int64
+// TrackingWriter tracks the last byte written on every write so
+// we can avoid printing a newline if one was already written or
+// if there is no output at all.
+type TrackingWriter struct {
+ w *bufio.Writer
+ last byte
}
-func newCountingWriter(w io.Writer) *CountingWriter {
- return &CountingWriter{
- w: bufio.NewWriter(w),
+func newTrackingWriter(w io.Writer) *TrackingWriter {
+ return &TrackingWriter{
+ w: bufio.NewWriter(w),
+ last: '\n',
}
}
-func (cw *CountingWriter) Write(p []byte) (n int, err error) {
- cw.count += int64(len(p))
- return cw.w.Write(p)
-}
-
-func (cw *CountingWriter) Flush() {
- cw.w.Flush()
+func (t *TrackingWriter) Write(p []byte) (n int, err error) {
+ n, err = t.w.Write(p)
+ if n > 0 {
+ t.last = p[n-1]
+ }
+ return
}
-func (cw *CountingWriter) Reset() {
- cw.count = 0
+func (t *TrackingWriter) Flush() {
+ t.w.Flush()
}
-func (cw *CountingWriter) Count() int64 {
- return cw.count
+func (t *TrackingWriter) NeedNL() bool {
+ return t.last != '\n'
}