gopkg.in/tools/godep.v42@v42.0.0-20151223001600-f221061cd941/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"io"
     7  	"log"
     8  	"os"
     9  	"runtime/pprof"
    10  	"strings"
    11  	"text/template"
    12  )
    13  
    14  var (
    15  	cpuprofile       string
    16  	verbose          bool // Verbose flag for commands that support it
    17  	debug            bool // Debug flag for commands that support it
    18  	majorGoVersion   string
    19  	VendorExperiment bool
    20  	sep              string
    21  )
    22  
    23  // Command is an implementation of a godep command
    24  // like godep save or godep go.
    25  type Command struct {
    26  	// Run runs the command.
    27  	// The args are the arguments after the command name.
    28  	Run func(cmd *Command, args []string)
    29  
    30  	// Name of the command
    31  	Name string
    32  
    33  	// Args the command would expect
    34  	Args string
    35  
    36  	// Short is the short description shown in the 'godep help' output.
    37  	Short string
    38  
    39  	// Long is the long message shown in the
    40  	// 'godep help <this-command>' output.
    41  	Long string
    42  
    43  	// Flag is a set of flags specific to this command.
    44  	Flag flag.FlagSet
    45  }
    46  
    47  // UsageExit prints usage information and exits.
    48  func (c *Command) UsageExit() {
    49  	fmt.Fprintf(os.Stderr, "Args: godep %s [-v] [-d] %s\n\n", c.Name, c.Args)
    50  	fmt.Fprintf(os.Stderr, "Run 'godep help %s' for help.\n", c.Name)
    51  	os.Exit(2)
    52  }
    53  
    54  // Commands lists the available commands and help topics.
    55  // The order here is the order in which they are printed
    56  // by 'godep help'.
    57  var commands = []*Command{
    58  	cmdSave,
    59  	cmdGo,
    60  	cmdGet,
    61  	cmdPath,
    62  	cmdRestore,
    63  	cmdUpdate,
    64  	cmdDiff,
    65  	cmdVersion,
    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  	var err error
    84  	majorGoVersion, err = goVersion()
    85  	if err != nil {
    86  		log.Fatal(err)
    87  	}
    88  
    89  	// VendorExperiment is the Go 1.5 vendor directory experiment flag, see
    90  	// https://github.com/golang/go/commit/183cc0cd41f06f83cb7a2490a499e3f9101befff
    91  	VendorExperiment = (majorGoVersion == "go1.5" && os.Getenv("GO15VENDOREXPERIMENT") == "1") ||
    92  		(majorGoVersion == "go1.6" && os.Getenv("GO15VENDOREXPERIMENT") != "0")
    93  
    94  	// sep is the signature set of path elements that
    95  	// precede the original path of an imported package.
    96  	sep = defaultSep(VendorExperiment)
    97  
    98  	for _, cmd := range commands {
    99  		if cmd.Name == args[0] {
   100  			cmd.Flag.BoolVar(&verbose, "v", false, "enable verbose output")
   101  			cmd.Flag.BoolVar(&debug, "d", false, "enable debug output")
   102  			cmd.Flag.StringVar(&cpuprofile, "cpuprofile", "", "Write cpu profile to this file")
   103  			cmd.Flag.Usage = func() { cmd.UsageExit() }
   104  			cmd.Flag.Parse(args[1:])
   105  
   106  			debugln("majorGoVersion", majorGoVersion)
   107  			debugln("VendorExperiment", VendorExperiment)
   108  			debugln("sep", sep)
   109  
   110  			if cpuprofile != "" {
   111  				f, err := os.Create(cpuprofile)
   112  				if err != nil {
   113  					log.Fatal(err)
   114  				}
   115  				pprof.StartCPUProfile(f)
   116  				defer pprof.StopCPUProfile()
   117  			}
   118  			cmd.Run(cmd, cmd.Flag.Args())
   119  			return
   120  		}
   121  	}
   122  
   123  	fmt.Fprintf(os.Stderr, "godep: unknown command %q\n", args[0])
   124  	fmt.Fprintf(os.Stderr, "Run 'godep help' for usage.\n")
   125  	os.Exit(2)
   126  }
   127  
   128  var usageTemplate = `
   129  Godep is a tool for managing Go package dependencies.
   130  
   131  Usage:
   132  
   133  	godep command [arguments]
   134  
   135  The commands are:
   136  {{range .}}
   137      {{.Name | printf "%-8s"}} {{.Short}}{{end}}
   138  
   139  Use "godep help [command]" for more information about a command.
   140  `
   141  
   142  var helpTemplate = `
   143  Args: godep {{.Name}} [-v] [-d] {{.Args}}
   144  
   145  {{.Long | trim}}
   146  
   147  If -v is given, verbose output is enabled.
   148  
   149  If -d is given, debug output is enabled (you probably don't want this, see -v).
   150  
   151  `
   152  
   153  func help(args []string) {
   154  	if len(args) == 0 {
   155  		printUsage(os.Stdout)
   156  		return
   157  	}
   158  	if len(args) != 1 {
   159  		fmt.Fprintf(os.Stderr, "usage: godep help command\n\n")
   160  		fmt.Fprintf(os.Stderr, "Too many arguments given.\n")
   161  		os.Exit(2)
   162  	}
   163  	for _, cmd := range commands {
   164  		if cmd.Name == args[0] {
   165  			tmpl(os.Stdout, helpTemplate, cmd)
   166  			return
   167  		}
   168  	}
   169  }
   170  
   171  func usageExit() {
   172  	printUsage(os.Stderr)
   173  	os.Exit(2)
   174  }
   175  
   176  func printUsage(w io.Writer) {
   177  	tmpl(w, usageTemplate, commands)
   178  }
   179  
   180  // tmpl executes the given template text on data, writing the result to w.
   181  func tmpl(w io.Writer, text string, data interface{}) {
   182  	t := template.New("top")
   183  	t.Funcs(template.FuncMap{
   184  		"trim": strings.TrimSpace,
   185  	})
   186  	template.Must(t.Parse(strings.TrimSpace(text) + "\n\n"))
   187  	if err := t.Execute(w, data); err != nil {
   188  		panic(err)
   189  	}
   190  }