github.com/alexanderthaller/godep@v0.0.0-20141231210904-0baa7ea46402/main.go (about)

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