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