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