github.com/sc0rp1us/gb@v0.4.1-0.20160319180011-4ba8cf1baa5a/cmd/gb-vendor/list.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"html/template"
     7  	"os"
     8  	"text/tabwriter"
     9  
    10  	"github.com/constabulary/gb"
    11  	"github.com/constabulary/gb/cmd"
    12  	"github.com/constabulary/gb/vendor"
    13  )
    14  
    15  var format string
    16  
    17  var cmdList = &cmd.Command{
    18  	Name:      "list",
    19  	UsageLine: "list [-f format]",
    20  	Short:     "lists dependencies, one per line",
    21  	Long: `gb vendor list formats lists the contents of the manifest file.
    22  
    23  The output
    24  
    25  Flags:
    26  	-f
    27  		controls the template used for printing each manifest entry. If not supplied
    28  		the default value is "{{.Importpath}}\t{{.Repository}}{{.Path}}\t{{.Branch}}\t{{.Revision}}"
    29  
    30  `,
    31  	Run: func(ctx *gb.Context, args []string) error {
    32  		m, err := vendor.ReadManifest(manifestFile(ctx))
    33  		if err != nil {
    34  			return fmt.Errorf("could not load manifest: %v", err)
    35  		}
    36  		tmpl, err := template.New("list").Parse(format)
    37  		if err != nil {
    38  			return fmt.Errorf("unable to parse template %q: %v", format, err)
    39  		}
    40  		w := tabwriter.NewWriter(os.Stdout, 1, 2, 1, ' ', 0)
    41  		for _, dep := range m.Dependencies {
    42  			if err := tmpl.Execute(w, dep); err != nil {
    43  				return fmt.Errorf("unable to execute template: %v", err)
    44  			}
    45  			fmt.Fprintln(w)
    46  		}
    47  		return w.Flush()
    48  	},
    49  	AddFlags: func(fs *flag.FlagSet) {
    50  		fs.StringVar(&format, "f", "{{.Importpath}}\t{{.Repository}}{{.Path}}\t{{.Branch}}\t{{.Revision}}", "format template")
    51  	},
    52  }