xorm.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build go1.1
  5. package main
  6. import (
  7. "fmt"
  8. "io"
  9. "os"
  10. "runtime"
  11. "strings"
  12. "sync"
  13. "text/template"
  14. "unicode"
  15. "unicode/utf8"
  16. )
  17. // Test that go1.1 tag above is included in builds. main.go refers to this definition.
  18. const go11tag = true
  19. const version = "0.3.0324"
  20. var supportedDrivers = map[string]string{
  21. "mysql": "github.com/go-sql-driver/mysql",
  22. "mymysql": "github.com/ziutek/mymysql/godrv",
  23. "postgres": "github.com/lib/pq",
  24. "mssql": "github.com/denisenkom/go-mssqldb",
  25. }
  26. // Commands lists the available commands and help topics.
  27. // The order here is the order in which they are printed by 'gopm help'.
  28. var commands = []*Command{
  29. CmdReverse,
  30. CmdShell,
  31. CmdDump,
  32. CmdDriver,
  33. CmdSource,
  34. }
  35. func init() {
  36. runtime.GOMAXPROCS(runtime.NumCPU())
  37. }
  38. func main() {
  39. // Check length of arguments.
  40. args := os.Args[1:]
  41. if len(args) < 1 {
  42. usage()
  43. return
  44. }
  45. // Show help documentation.
  46. if args[0] == "help" {
  47. help(args[1:])
  48. return
  49. }
  50. // Check commands and run.
  51. for _, comm := range commands {
  52. if comm.Name() == args[0] && comm.Run != nil {
  53. comm.Run(comm, args[1:])
  54. exit()
  55. return
  56. }
  57. }
  58. fmt.Fprintf(os.Stderr, "xorm: unknown subcommand %q\nRun 'xorm help' for usage.\n", args[0])
  59. setExitStatus(2)
  60. exit()
  61. }
  62. var exitStatus = 0
  63. var exitMu sync.Mutex
  64. func setExitStatus(n int) {
  65. exitMu.Lock()
  66. if exitStatus < n {
  67. exitStatus = n
  68. }
  69. exitMu.Unlock()
  70. }
  71. var usageTemplate = `xorm is a database tool based xorm package.
  72. Version:
  73. ` + version + `
  74. Usage:
  75. xorm command [arguments]
  76. The commands are:
  77. {{range .}}{{if .Runnable}}
  78. {{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
  79. Use "xorm help [command]" for more information about a command.
  80. Additional help topics:
  81. {{range .}}{{if not .Runnable}}
  82. {{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
  83. Use "xorm help [topic]" for more information about that topic.
  84. `
  85. var helpTemplate = `{{if .Runnable}}usage: xorm {{.UsageLine}}
  86. {{end}}{{.Long | trim}}
  87. `
  88. // tmpl executes the given template text on data, writing the result to w.
  89. func tmpl(w io.Writer, text string, data interface{}) {
  90. t := template.New("top")
  91. t.Funcs(template.FuncMap{"trim": strings.TrimSpace, "capitalize": capitalize})
  92. template.Must(t.Parse(text))
  93. if err := t.Execute(w, data); err != nil {
  94. panic(err)
  95. }
  96. }
  97. func capitalize(s string) string {
  98. if s == "" {
  99. return s
  100. }
  101. r, n := utf8.DecodeRuneInString(s)
  102. return string(unicode.ToTitle(r)) + s[n:]
  103. }
  104. func printUsage(w io.Writer) {
  105. tmpl(w, usageTemplate, commands)
  106. }
  107. func usage() {
  108. printUsage(os.Stderr)
  109. os.Exit(2)
  110. }
  111. // help implements the 'help' command.
  112. func help(args []string) {
  113. if len(args) == 0 {
  114. printUsage(os.Stdout)
  115. // not exit 2: succeeded at 'gopm help'.
  116. return
  117. }
  118. if len(args) != 1 {
  119. fmt.Fprintf(os.Stderr, "usage: xorm help command\n\nToo many arguments given.\n")
  120. os.Exit(2) // failed at 'gopm help'
  121. }
  122. arg := args[0]
  123. for _, cmd := range commands {
  124. if cmd.Name() == arg {
  125. tmpl(os.Stdout, helpTemplate, cmd)
  126. // not exit 2: succeeded at 'gopm help cmd'.
  127. return
  128. }
  129. }
  130. fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run 'xorm help'.\n", arg)
  131. os.Exit(2) // failed at 'gopm help cmd'
  132. }
  133. var atexitFuncs []func()
  134. func atexit(f func()) {
  135. atexitFuncs = append(atexitFuncs, f)
  136. }
  137. func exit() {
  138. for _, f := range atexitFuncs {
  139. f()
  140. }
  141. os.Exit(exitStatus)
  142. }