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.
 
 
 
 
 
 

199 lines
5.2 KiB

  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. "time"
  13. "code.gitea.io/gitea/cmd"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/setting"
  16. // register supported doc types
  17. _ "code.gitea.io/gitea/modules/markup/csv"
  18. _ "code.gitea.io/gitea/modules/markup/markdown"
  19. _ "code.gitea.io/gitea/modules/markup/orgmode"
  20. "github.com/urfave/cli"
  21. )
  22. var (
  23. // Version holds the current Gitea version
  24. Version = "development"
  25. // Tags holds the build tags used
  26. Tags = ""
  27. // MakeVersion holds the current Make version if built with make
  28. MakeVersion = ""
  29. originalAppHelpTemplate = ""
  30. originalCommandHelpTemplate = ""
  31. originalSubcommandHelpTemplate = ""
  32. )
  33. func init() {
  34. setting.AppVer = Version
  35. setting.AppBuiltWith = formatBuiltWith()
  36. setting.AppStartTime = time.Now().UTC()
  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. cmd.CmdDoctor,
  61. cmd.CmdManager,
  62. cmd.Cmdembedded,
  63. cmd.CmdMigrateStorage,
  64. cmd.CmdDocs,
  65. cmd.CmdDumpRepository,
  66. cmd.CmdRestoreRepository,
  67. }
  68. // Now adjust these commands to add our global configuration options
  69. // First calculate the default paths and set the AppHelpTemplates in this context
  70. setting.SetCustomPathAndConf("", "", "")
  71. setAppHelpTemplates()
  72. // default configuration flags
  73. defaultFlags := []cli.Flag{
  74. cli.StringFlag{
  75. Name: "custom-path, C",
  76. Value: setting.CustomPath,
  77. Usage: "Custom path file path",
  78. },
  79. cli.StringFlag{
  80. Name: "config, c",
  81. Value: setting.CustomConf,
  82. Usage: "Custom configuration file path",
  83. },
  84. cli.VersionFlag,
  85. cli.StringFlag{
  86. Name: "work-path, w",
  87. Value: setting.AppWorkPath,
  88. Usage: "Set the gitea working path",
  89. },
  90. }
  91. // Set the default to be equivalent to cmdWeb and add the default flags
  92. app.Flags = append(app.Flags, cmd.CmdWeb.Flags...)
  93. app.Flags = append(app.Flags, defaultFlags...)
  94. app.Action = cmd.CmdWeb.Action
  95. // Add functions to set these paths and these flags to the commands
  96. app.Before = establishCustomPath
  97. for i := range app.Commands {
  98. setFlagsAndBeforeOnSubcommands(&app.Commands[i], defaultFlags, establishCustomPath)
  99. }
  100. err := app.Run(os.Args)
  101. if err != nil {
  102. log.Fatal("Failed to run app with %s: %v", os.Args, err)
  103. }
  104. }
  105. func setFlagsAndBeforeOnSubcommands(command *cli.Command, defaultFlags []cli.Flag, before cli.BeforeFunc) {
  106. command.Flags = append(command.Flags, defaultFlags...)
  107. command.Before = establishCustomPath
  108. for i := range command.Subcommands {
  109. setFlagsAndBeforeOnSubcommands(&command.Subcommands[i], defaultFlags, before)
  110. }
  111. }
  112. func establishCustomPath(ctx *cli.Context) error {
  113. var providedCustom string
  114. var providedConf string
  115. var providedWorkPath string
  116. currentCtx := ctx
  117. for {
  118. if len(providedCustom) != 0 && len(providedConf) != 0 && len(providedWorkPath) != 0 {
  119. break
  120. }
  121. if currentCtx == nil {
  122. break
  123. }
  124. if currentCtx.IsSet("custom-path") && len(providedCustom) == 0 {
  125. providedCustom = currentCtx.String("custom-path")
  126. }
  127. if currentCtx.IsSet("config") && len(providedConf) == 0 {
  128. providedConf = currentCtx.String("config")
  129. }
  130. if currentCtx.IsSet("work-path") && len(providedWorkPath) == 0 {
  131. providedWorkPath = currentCtx.String("work-path")
  132. }
  133. currentCtx = currentCtx.Parent()
  134. }
  135. setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath)
  136. setAppHelpTemplates()
  137. if ctx.IsSet("version") {
  138. cli.ShowVersion(ctx)
  139. os.Exit(0)
  140. }
  141. return nil
  142. }
  143. func setAppHelpTemplates() {
  144. cli.AppHelpTemplate = adjustHelpTemplate(originalAppHelpTemplate)
  145. cli.CommandHelpTemplate = adjustHelpTemplate(originalCommandHelpTemplate)
  146. cli.SubcommandHelpTemplate = adjustHelpTemplate(originalSubcommandHelpTemplate)
  147. }
  148. func adjustHelpTemplate(originalTemplate string) string {
  149. overrided := ""
  150. if _, ok := os.LookupEnv("GITEA_CUSTOM"); ok {
  151. overrided = "(GITEA_CUSTOM)"
  152. }
  153. return fmt.Sprintf(`%s
  154. DEFAULT CONFIGURATION:
  155. CustomPath: %s %s
  156. CustomConf: %s
  157. AppPath: %s
  158. AppWorkPath: %s
  159. `, originalTemplate, setting.CustomPath, overrided, setting.CustomConf, setting.AppPath, setting.AppWorkPath)
  160. }
  161. func formatBuiltWith() string {
  162. var version = runtime.Version()
  163. if len(MakeVersion) > 0 {
  164. version = MakeVersion + ", " + runtime.Version()
  165. }
  166. if len(Tags) == 0 {
  167. return " built with " + version
  168. }
  169. return " built with " + version + " : " + strings.ReplaceAll(Tags, " ", ", ")
  170. }