PageRenderTime 426ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/src/github.com/revel/cmd/revel/rev.go

https://github.com/arvindram03/golang-spike
Go | 129 lines | 107 code | 16 blank | 6 comment | 22 complexity | 299bd455dfd23275b932119dcff9d3a2 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, Apache-2.0, LGPL-3.0
  1. // The command line tool for running Revel apps.
  2. package main
  3. import (
  4. "flag"
  5. "fmt"
  6. "github.com/agtorre/gocolorize"
  7. "io"
  8. "math/rand"
  9. "os"
  10. "runtime"
  11. "strings"
  12. "text/template"
  13. "time"
  14. )
  15. // Cribbed from the genius organization of the "go" command.
  16. type Command struct {
  17. Run func(args []string)
  18. UsageLine, Short, Long string
  19. }
  20. func (cmd *Command) Name() string {
  21. name := cmd.UsageLine
  22. i := strings.Index(name, " ")
  23. if i >= 0 {
  24. name = name[:i]
  25. }
  26. return name
  27. }
  28. var commands = []*Command{
  29. cmdNew,
  30. cmdRun,
  31. cmdBuild,
  32. cmdPackage,
  33. cmdClean,
  34. cmdTest,
  35. }
  36. func main() {
  37. if runtime.GOOS == "windows" {
  38. gocolorize.SetPlain(true)
  39. }
  40. fmt.Fprintf(os.Stdout, gocolorize.NewColor("blue").Paint(header))
  41. flag.Usage = func() { usage(1) }
  42. flag.Parse()
  43. args := flag.Args()
  44. if len(args) < 1 || args[0] == "help" {
  45. if len(args) == 1 {
  46. usage(0)
  47. }
  48. if len(args) > 1 {
  49. for _, cmd := range commands {
  50. if cmd.Name() == args[1] {
  51. tmpl(os.Stdout, helpTemplate, cmd)
  52. return
  53. }
  54. }
  55. }
  56. usage(2)
  57. }
  58. // Commands use panic to abort execution when something goes wrong.
  59. // Panics are logged at the point of error. Ignore those.
  60. defer func() {
  61. if err := recover(); err != nil {
  62. if _, ok := err.(LoggedError); !ok {
  63. // This panic was not expected / logged.
  64. panic(err)
  65. }
  66. os.Exit(1)
  67. }
  68. }()
  69. for _, cmd := range commands {
  70. if cmd.Name() == args[0] {
  71. cmd.Run(args[1:])
  72. return
  73. }
  74. }
  75. errorf("unknown command %q\nRun 'revel help' for usage.\n", args[0])
  76. }
  77. func errorf(format string, args ...interface{}) {
  78. // Ensure the user's command prompt starts on the next line.
  79. if !strings.HasSuffix(format, "\n") {
  80. format += "\n"
  81. }
  82. fmt.Fprintf(os.Stderr, format, args...)
  83. panic(LoggedError{}) // Panic instead of os.Exit so that deferred will run.
  84. }
  85. const header = `~
  86. ~ revel! http://revel.github.io
  87. ~
  88. `
  89. const usageTemplate = `usage: revel command [arguments]
  90. The commands are:
  91. {{range .}}
  92. {{.Name | printf "%-11s"}} {{.Short}}{{end}}
  93. Use "revel help [command]" for more information.
  94. `
  95. var helpTemplate = `usage: revel {{.UsageLine}}
  96. {{.Long}}
  97. `
  98. func usage(exitCode int) {
  99. tmpl(os.Stderr, usageTemplate, commands)
  100. os.Exit(exitCode)
  101. }
  102. func tmpl(w io.Writer, text string, data interface{}) {
  103. t := template.New("top")
  104. template.Must(t.Parse(text))
  105. if err := t.Execute(w, data); err != nil {
  106. panic(err)
  107. }
  108. }
  109. func init() {
  110. rand.Seed(time.Now().UnixNano())
  111. }