gopkg.in/tools/godep.v47@v47.0.0-20160112223013-b922ec76f713/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 (majorGoVersion == "devel" && os.Getenv("GO15VENDOREXPERIMENT") != "0") 94 95 // sep is the signature set of path elements that 96 // precede the original path of an imported package. 97 sep = defaultSep(VendorExperiment) 98 99 for _, cmd := range commands { 100 if cmd.Name == args[0] { 101 cmd.Flag.BoolVar(&verbose, "v", false, "enable verbose output") 102 cmd.Flag.BoolVar(&debug, "d", false, "enable debug output") 103 cmd.Flag.StringVar(&cpuprofile, "cpuprofile", "", "Write cpu profile to this file") 104 cmd.Flag.Usage = func() { cmd.UsageExit() } 105 cmd.Flag.Parse(args[1:]) 106 107 debugln("majorGoVersion", majorGoVersion) 108 debugln("VendorExperiment", VendorExperiment) 109 debugln("sep", sep) 110 111 if cpuprofile != "" { 112 f, err := os.Create(cpuprofile) 113 if err != nil { 114 log.Fatal(err) 115 } 116 pprof.StartCPUProfile(f) 117 defer pprof.StopCPUProfile() 118 } 119 cmd.Run(cmd, cmd.Flag.Args()) 120 return 121 } 122 } 123 124 fmt.Fprintf(os.Stderr, "godep: unknown command %q\n", args[0]) 125 fmt.Fprintf(os.Stderr, "Run 'godep help' for usage.\n") 126 os.Exit(2) 127 } 128 129 var usageTemplate = ` 130 Godep is a tool for managing Go package dependencies. 131 132 Usage: 133 134 godep command [arguments] 135 136 The commands are: 137 {{range .}} 138 {{.Name | printf "%-8s"}} {{.Short}}{{end}} 139 140 Use "godep help [command]" for more information about a command. 141 ` 142 143 var helpTemplate = ` 144 Args: godep {{.Name}} [-v] [-d] {{.Args}} 145 146 {{.Long | trim}} 147 148 If -v is given, verbose output is enabled. 149 150 If -d is given, debug output is enabled (you probably don't want this, see -v). 151 152 ` 153 154 func help(args []string) { 155 if len(args) == 0 { 156 printUsage(os.Stdout) 157 return 158 } 159 if len(args) != 1 { 160 fmt.Fprintf(os.Stderr, "usage: godep help command\n\n") 161 fmt.Fprintf(os.Stderr, "Too many arguments given.\n") 162 os.Exit(2) 163 } 164 for _, cmd := range commands { 165 if cmd.Name == args[0] { 166 tmpl(os.Stdout, helpTemplate, cmd) 167 return 168 } 169 } 170 } 171 172 func usageExit() { 173 printUsage(os.Stderr) 174 os.Exit(2) 175 } 176 177 func printUsage(w io.Writer) { 178 tmpl(w, usageTemplate, commands) 179 } 180 181 // tmpl executes the given template text on data, writing the result to w. 182 func tmpl(w io.Writer, text string, data interface{}) { 183 t := template.New("top") 184 t.Funcs(template.FuncMap{ 185 "trim": strings.TrimSpace, 186 }) 187 template.Must(t.Parse(strings.TrimSpace(text) + "\n\n")) 188 if err := t.Execute(w, data); err != nil { 189 panic(err) 190 } 191 }