gopkg.in/tools/godep.v41@v41.0.0-20151217180337-fe5ce707f879/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 ) 19 20 // Command is an implementation of a godep command 21 // like godep save or godep go. 22 type Command struct { 23 // Run runs the command. 24 // The args are the arguments after the command name. 25 Run func(cmd *Command, args []string) 26 27 // Name of the command 28 Name string 29 30 // Args the command would expect 31 Args string 32 33 // Short is the short description shown in the 'godep help' output. 34 Short string 35 36 // Long is the long message shown in the 37 // 'godep help <this-command>' output. 38 Long string 39 40 // Flag is a set of flags specific to this command. 41 Flag flag.FlagSet 42 } 43 44 // UsageExit prints usage information and exits. 45 func (c *Command) UsageExit() { 46 fmt.Fprintf(os.Stderr, "Args: godep %s [-v] [-d] %s\n\n", c.Name, c.Args) 47 fmt.Fprintf(os.Stderr, "Run 'godep help %s' for help.\n", c.Name) 48 os.Exit(2) 49 } 50 51 // Commands lists the available commands and help topics. 52 // The order here is the order in which they are printed 53 // by 'godep help'. 54 var commands = []*Command{ 55 cmdSave, 56 cmdGo, 57 cmdGet, 58 cmdPath, 59 cmdRestore, 60 cmdUpdate, 61 cmdDiff, 62 cmdVersion, 63 } 64 65 func main() { 66 flag.Usage = usageExit 67 flag.Parse() 68 log.SetFlags(0) 69 log.SetPrefix("godep: ") 70 args := flag.Args() 71 if len(args) < 1 { 72 usageExit() 73 } 74 75 if args[0] == "help" { 76 help(args[1:]) 77 return 78 } 79 80 for _, cmd := range commands { 81 if cmd.Name == args[0] { 82 cmd.Flag.BoolVar(&verbose, "v", false, "enable verbose output") 83 cmd.Flag.BoolVar(&debug, "d", false, "enable debug output") 84 cmd.Flag.StringVar(&cpuprofile, "cpuprofile", "", "Write cpu profile to this file") 85 cmd.Flag.Usage = func() { cmd.UsageExit() } 86 cmd.Flag.Parse(args[1:]) 87 if cpuprofile != "" { 88 f, err := os.Create(cpuprofile) 89 if err != nil { 90 log.Fatal(err) 91 } 92 pprof.StartCPUProfile(f) 93 defer pprof.StopCPUProfile() 94 } 95 cmd.Run(cmd, cmd.Flag.Args()) 96 return 97 } 98 } 99 100 fmt.Fprintf(os.Stderr, "godep: unknown command %q\n", args[0]) 101 fmt.Fprintf(os.Stderr, "Run 'godep help' for usage.\n") 102 os.Exit(2) 103 } 104 105 var usageTemplate = ` 106 Godep is a tool for managing Go package dependencies. 107 108 Usage: 109 110 godep command [arguments] 111 112 The commands are: 113 {{range .}} 114 {{.Name | printf "%-8s"}} {{.Short}}{{end}} 115 116 Use "godep help [command]" for more information about a command. 117 ` 118 119 var helpTemplate = ` 120 Args: godep {{.Name}} [-v] [-d] {{.Args}} 121 122 {{.Long | trim}} 123 124 If -v is given, verbose output is enabled. 125 126 If -d is given, debug output is enabled (you probably don't want this, see -v). 127 128 ` 129 130 func help(args []string) { 131 if len(args) == 0 { 132 printUsage(os.Stdout) 133 return 134 } 135 if len(args) != 1 { 136 fmt.Fprintf(os.Stderr, "usage: godep help command\n\n") 137 fmt.Fprintf(os.Stderr, "Too many arguments given.\n") 138 os.Exit(2) 139 } 140 for _, cmd := range commands { 141 if cmd.Name == args[0] { 142 tmpl(os.Stdout, helpTemplate, cmd) 143 return 144 } 145 } 146 } 147 148 func usageExit() { 149 printUsage(os.Stderr) 150 os.Exit(2) 151 } 152 153 func printUsage(w io.Writer) { 154 tmpl(w, usageTemplate, commands) 155 } 156 157 // tmpl executes the given template text on data, writing the result to w. 158 func tmpl(w io.Writer, text string, data interface{}) { 159 t := template.New("top") 160 t.Funcs(template.FuncMap{ 161 "trim": strings.TrimSpace, 162 }) 163 template.Must(t.Parse(strings.TrimSpace(text) + "\n\n")) 164 if err := t.Execute(w, data); err != nil { 165 panic(err) 166 } 167 }