Commit 9714691a authored by Roger Peppe's avatar Roger Peppe

cmd/go: add join template function.

It's common to use the go list command in shell scripts, but
currently it's awkward to print a string slice from the Package
type in a way that's easily parseable by the shell.  For example:

        go list -f '{{range .Deps}}{{.}}
        {{end}}'

(and even that prints an unwanted new line at the end|).

To make this easier, this CL adds a "join" function to the
format template.

        go list -f '{{join .Deps "\n"}}'

R=rsc, dsymonds, minux.ma, remyoudompheng, r
CC=golang-dev
https://golang.org/cl/6680044
parent 319131f2
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"os" "os"
"strings"
"text/template" "text/template"
) )
...@@ -24,10 +25,10 @@ The default output shows the package import path: ...@@ -24,10 +25,10 @@ The default output shows the package import path:
code.google.com/p/goauth2/oauth code.google.com/p/goauth2/oauth
code.google.com/p/sqlite code.google.com/p/sqlite
The -f flag specifies an alternate format for the list, The -f flag specifies an alternate format for the list, using the
using the syntax of package template. The default output syntax of package template. The default output is equivalent to -f
is equivalent to -f '{{.ImportPath}}'. The struct '{{.ImportPath}}'. One extra template function is available, "join",
being passed to the template is: which calls strings.Join. The struct being passed to the template is:
type Package struct { type Package struct {
Dir string // directory containing package sources Dir string // directory containing package sources
...@@ -113,7 +114,7 @@ func runList(cmd *Command, args []string) { ...@@ -113,7 +114,7 @@ func runList(cmd *Command, args []string) {
out.Write(nl) out.Write(nl)
} }
} else { } else {
tmpl, err := template.New("main").Parse(*listFmt) tmpl, err := template.New("main").Funcs(template.FuncMap{"join": strings.Join}).Parse(*listFmt)
if err != nil { if err != nil {
fatalf("%s", err) fatalf("%s", err)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment