gopkg.in/tools/godep.v21@v21.0.0-20151104013723-2cf1d6e3f557/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"io"
     7  	"log"
     8  	"os"
     9  	"runtime"
    10  	"strings"
    11  	"text/template"
    12  )
    13  
    14  var verbose bool // Verbose flag for commands that support it
    15  
    16  // Command is an implementation of a godep command
    17  // like godep save or godep go.
    18  type Command struct {
    19  	// Run runs the command.
    20  	// The args are the arguments after the command name.
    21  	Run func(cmd *Command, args []string)
    22  
    23  	// Usage is the one-line usage message.
    24  	// The first word in the line is taken to be the command name.
    25  	Usage string
    26  
    27  	// Short is the short description shown in the 'godep help' output.
    28  	Short string
    29  
    30  	// Long is the long message shown in the
    31  	// 'godep help <this-command>' output.
    32  	Long string
    33  
    34  	// Flag is a set of flags specific to this command.
    35  	Flag flag.FlagSet
    36  }
    37  
    38  // Name returns the name of a command.
    39  func (c *Command) Name() string {
    40  	name := c.Usage
    41  	i := strings.Index(name, " ")
    42  	if i >= 0 {
    43  		name = name[:i]
    44  	}
    45  	return name
    46  }
    47  
    48  // UsageExit prints usage information and exits.
    49  func (c *Command) UsageExit() {
    50  	fmt.Fprintf(os.Stderr, "Usage: godep %s\n\n", c.Usage)
    51  	fmt.Fprintf(os.Stderr, "Run 'godep help %s' for help.\n", c.Name())
    52  	os.Exit(2)
    53  }
    54  
    55  // Commands lists the available commands and help topics.
    56  // The order here is the order in which they are printed
    57  // by 'godep help'.
    58  var commands = []*Command{
    59  	cmdSave,
    60  	cmdGo,
    61  	cmdGet,
    62  	cmdPath,
    63  	cmdRestore,
    64  	cmdUpdate,
    65  	cmdDiff,
    66  }
    67  
    68  func main() {
    69  	flag.Usage = usageExit
    70  	flag.Parse()
    71  	log.SetFlags(0)
    72  	log.SetPrefix("godep: ")
    73  	args := flag.Args()
    74  	if len(args) < 1 {
    75  		usageExit()
    76  	}
    77  
    78  	if args[0] == "help" {
    79  		help(args[1:])
    80  		return
    81  	}
    82  
    83  	if args[0] == "version" {
    84  		fmt.Printf("godep v%d (%s/%s/%s)\n", version, runtime.GOOS, runtime.GOARCH, runtime.Version())
    85  		return
    86  	}
    87  
    88  	for _, cmd := range commands {
    89  		if cmd.Name() == args[0] {
    90  			cmd.Flag.Usage = func() { cmd.UsageExit() }
    91  			cmd.Flag.Parse(args[1:])
    92  			cmd.Run(cmd, cmd.Flag.Args())
    93  			return
    94  		}
    95  	}
    96  
    97  	fmt.Fprintf(os.Stderr, "godep: unknown command %q\n", args[0])
    98  	fmt.Fprintf(os.Stderr, "Run 'godep help' for usage.\n")
    99  	os.Exit(2)
   100  }
   101  
   102  var usageTemplate = `
   103  Godep is a tool for managing Go package dependencies.
   104  
   105  Usage:
   106  
   107  	godep command [arguments]
   108  
   109  The commands are:
   110  {{range .}}
   111      {{.Name | printf "%-8s"}} {{.Short}}{{end}}
   112  
   113  Use "godep help [command]" for more information about a command.
   114  `
   115  
   116  var helpTemplate = `
   117  Usage: godep {{.Usage}}
   118  
   119  {{.Long | trim}}
   120  `
   121  
   122  func help(args []string) {
   123  	if len(args) == 0 {
   124  		printUsage(os.Stdout)
   125  		return
   126  	}
   127  	if len(args) != 1 {
   128  		fmt.Fprintf(os.Stderr, "usage: godep help command\n\n")
   129  		fmt.Fprintf(os.Stderr, "Too many arguments given.\n")
   130  		os.Exit(2)
   131  	}
   132  	for _, cmd := range commands {
   133  		if cmd.Name() == args[0] {
   134  			tmpl(os.Stdout, helpTemplate, cmd)
   135  			return
   136  		}
   137  	}
   138  }
   139  
   140  func usageExit() {
   141  	printUsage(os.Stderr)
   142  	os.Exit(2)
   143  }
   144  
   145  func printUsage(w io.Writer) {
   146  	tmpl(w, usageTemplate, commands)
   147  }
   148  
   149  // tmpl executes the given template text on data, writing the result to w.
   150  func tmpl(w io.Writer, text string, data interface{}) {
   151  	t := template.New("top")
   152  	t.Funcs(template.FuncMap{
   153  		"trim": strings.TrimSpace,
   154  	})
   155  	template.Must(t.Parse(strings.TrimSpace(text) + "\n\n"))
   156  	if err := t.Execute(w, data); err != nil {
   157  		panic(err)
   158  	}
   159  }