本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
5.1KB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. // Gitea (git with a cup of tea) is a painless self-hosted Git Service.
  6. package main // import "code.gitea.io/gitea"
  7. import (
  8. "fmt"
  9. "os"
  10. "runtime"
  11. "strings"
  12. "code.gitea.io/gitea/cmd"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. // register supported doc types
  16. _ "code.gitea.io/gitea/modules/markup/csv"
  17. _ "code.gitea.io/gitea/modules/markup/markdown"
  18. _ "code.gitea.io/gitea/modules/markup/orgmode"
  19. // for embed
  20. _ "github.com/shurcooL/vfsgen"
  21. "github.com/urfave/cli"
  22. )
  23. var (
  24. // Version holds the current Gitea version
  25. Version = "1.9.0-dev"
  26. // Tags holds the build tags used
  27. Tags = ""
  28. // MakeVersion holds the current Make version if built with make
  29. MakeVersion = ""
  30. originalAppHelpTemplate = ""
  31. originalCommandHelpTemplate = ""
  32. originalSubcommandHelpTemplate = ""
  33. )
  34. func init() {
  35. setting.AppVer = Version
  36. setting.AppBuiltWith = formatBuiltWith()
  37. // Grab the original help templates
  38. originalAppHelpTemplate = cli.AppHelpTemplate
  39. originalCommandHelpTemplate = cli.CommandHelpTemplate
  40. originalSubcommandHelpTemplate = cli.SubcommandHelpTemplate
  41. }
  42. func main() {
  43. app := cli.NewApp()
  44. app.Name = "Gitea"
  45. app.Usage = "A painless self-hosted Git service"
  46. app.Description = `By default, gitea will start serving using the webserver with no
  47. arguments - which can alternatively be run by running the subcommand web.`
  48. app.Version = Version + formatBuiltWith()
  49. app.Commands = []cli.Command{
  50. cmd.CmdWeb,
  51. cmd.CmdServ,
  52. cmd.CmdHook,
  53. cmd.CmdDump,
  54. cmd.CmdCert,
  55. cmd.CmdAdmin,
  56. cmd.CmdGenerate,
  57. cmd.CmdMigrate,
  58. cmd.CmdKeys,
  59. cmd.CmdConvert,
  60. }
  61. // Now adjust these commands to add our global configuration options
  62. // First calculate the default paths and set the AppHelpTemplates in this context
  63. setting.SetCustomPathAndConf("", "", "")
  64. setAppHelpTemplates()
  65. // default configuration flags
  66. defaultFlags := []cli.Flag{
  67. cli.StringFlag{
  68. Name: "custom-path, C",
  69. Value: setting.CustomPath,
  70. Usage: "Custom path file path",
  71. },
  72. cli.StringFlag{
  73. Name: "config, c",
  74. Value: setting.CustomConf,
  75. Usage: "Custom configuration file path",
  76. },
  77. cli.VersionFlag,
  78. cli.StringFlag{
  79. Name: "work-path, w",
  80. Value: setting.AppWorkPath,
  81. Usage: "Set the gitea working path",
  82. },
  83. }
  84. // Set the default to be equivalent to cmdWeb and add the default flags
  85. app.Flags = append(app.Flags, cmd.CmdWeb.Flags...)
  86. app.Flags = append(app.Flags, defaultFlags...)
  87. app.Action = cmd.CmdWeb.Action
  88. // Add functions to set these paths and these flags to the commands
  89. app.Before = establishCustomPath
  90. for i := range app.Commands {
  91. setFlagsAndBeforeOnSubcommands(&app.Commands[i], defaultFlags, establishCustomPath)
  92. }
  93. err := app.Run(os.Args)
  94. if err != nil {
  95. log.Fatal("Failed to run app with %s: %v", os.Args, err)
  96. }
  97. }
  98. func setFlagsAndBeforeOnSubcommands(command *cli.Command, defaultFlags []cli.Flag, before cli.BeforeFunc) {
  99. command.Flags = append(command.Flags, defaultFlags...)
  100. command.Before = establishCustomPath
  101. for i := range command.Subcommands {
  102. setFlagsAndBeforeOnSubcommands(&command.Subcommands[i], defaultFlags, before)
  103. }
  104. }
  105. func establishCustomPath(ctx *cli.Context) error {
  106. var providedCustom string
  107. var providedConf string
  108. var providedWorkPath string
  109. currentCtx := ctx
  110. for {
  111. if len(providedCustom) != 0 && len(providedConf) != 0 && len(providedWorkPath) != 0 {
  112. break
  113. }
  114. if currentCtx == nil {
  115. break
  116. }
  117. if currentCtx.IsSet("custom-path") && len(providedCustom) == 0 {
  118. providedCustom = currentCtx.String("custom-path")
  119. }
  120. if currentCtx.IsSet("config") && len(providedConf) == 0 {
  121. providedConf = currentCtx.String("config")
  122. }
  123. if currentCtx.IsSet("work-path") && len(providedWorkPath) == 0 {
  124. providedWorkPath = currentCtx.String("work-path")
  125. }
  126. currentCtx = currentCtx.Parent()
  127. }
  128. setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath)
  129. setAppHelpTemplates()
  130. if ctx.IsSet("version") {
  131. cli.ShowVersion(ctx)
  132. os.Exit(0)
  133. }
  134. return nil
  135. }
  136. func setAppHelpTemplates() {
  137. cli.AppHelpTemplate = adjustHelpTemplate(originalAppHelpTemplate)
  138. cli.CommandHelpTemplate = adjustHelpTemplate(originalCommandHelpTemplate)
  139. cli.SubcommandHelpTemplate = adjustHelpTemplate(originalSubcommandHelpTemplate)
  140. }
  141. func adjustHelpTemplate(originalTemplate string) string {
  142. overrided := ""
  143. if _, ok := os.LookupEnv("GITEA_CUSTOM"); ok {
  144. overrided = "(GITEA_CUSTOM)"
  145. }
  146. return fmt.Sprintf(`%s
  147. DEFAULT CONFIGURATION:
  148. CustomPath: %s %s
  149. CustomConf: %s
  150. AppPath: %s
  151. AppWorkPath: %s
  152. `, originalTemplate, setting.CustomPath, overrided, setting.CustomConf, setting.AppPath, setting.AppWorkPath)
  153. }
  154. func formatBuiltWith() string {
  155. var version = runtime.Version()
  156. if len(MakeVersion) > 0 {
  157. version = MakeVersion + ", " + runtime.Version()
  158. }
  159. if len(Tags) == 0 {
  160. return " built with " + version
  161. }
  162. return " built with " + version + " : " + strings.Replace(Tags, " ", ", ", -1)
  163. }
上海开阖软件有限公司 沪ICP备12045867号-1