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.

main.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. // Gitea (git with a cup of tea) is a painless self-hosted Git Service.
  5. package main // import "code.gitea.io/gitea"
  6. import (
  7. "fmt"
  8. "os"
  9. "runtime"
  10. "strings"
  11. "time"
  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/asciicast"
  17. _ "code.gitea.io/gitea/modules/markup/console"
  18. _ "code.gitea.io/gitea/modules/markup/csv"
  19. _ "code.gitea.io/gitea/modules/markup/markdown"
  20. _ "code.gitea.io/gitea/modules/markup/orgmode"
  21. "github.com/urfave/cli"
  22. )
  23. var (
  24. // Version holds the current Gitea version
  25. Version = "development"
  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. setting.AppStartTime = time.Now().UTC()
  38. // Grab the original help templates
  39. originalAppHelpTemplate = cli.AppHelpTemplate
  40. originalCommandHelpTemplate = cli.CommandHelpTemplate
  41. originalSubcommandHelpTemplate = cli.SubcommandHelpTemplate
  42. }
  43. func main() {
  44. app := cli.NewApp()
  45. app.Name = "Gitea"
  46. app.Usage = "A painless self-hosted Git service"
  47. app.Description = `By default, gitea will start serving using the webserver with no
  48. arguments - which can alternatively be run by running the subcommand web.`
  49. app.Version = Version + formatBuiltWith()
  50. app.Commands = []cli.Command{
  51. cmd.CmdWeb,
  52. cmd.CmdServ,
  53. cmd.CmdHook,
  54. cmd.CmdDump,
  55. cmd.CmdCert,
  56. cmd.CmdAdmin,
  57. cmd.CmdGenerate,
  58. cmd.CmdMigrate,
  59. cmd.CmdKeys,
  60. cmd.CmdConvert,
  61. cmd.CmdDoctor,
  62. cmd.CmdManager,
  63. cmd.Cmdembedded,
  64. cmd.CmdMigrateStorage,
  65. cmd.CmdDocs,
  66. cmd.CmdDumpRepository,
  67. cmd.CmdRestoreRepository,
  68. cmd.CmdActions,
  69. }
  70. // Now adjust these commands to add our global configuration options
  71. // First calculate the default paths and set the AppHelpTemplates in this context
  72. setting.SetCustomPathAndConf("", "", "")
  73. setAppHelpTemplates()
  74. // default configuration flags
  75. defaultFlags := []cli.Flag{
  76. cli.StringFlag{
  77. Name: "custom-path, C",
  78. Value: setting.CustomPath,
  79. Usage: "Custom path file path",
  80. },
  81. cli.StringFlag{
  82. Name: "config, c",
  83. Value: setting.CustomConf,
  84. Usage: "Custom configuration file path",
  85. },
  86. cli.VersionFlag,
  87. cli.StringFlag{
  88. Name: "work-path, w",
  89. Value: setting.AppWorkPath,
  90. Usage: "Set the gitea working path",
  91. },
  92. }
  93. // Set the default to be equivalent to cmdWeb and add the default flags
  94. app.Flags = append(app.Flags, cmd.CmdWeb.Flags...)
  95. app.Flags = append(app.Flags, defaultFlags...)
  96. app.Action = cmd.CmdWeb.Action
  97. // Add functions to set these paths and these flags to the commands
  98. app.Before = establishCustomPath
  99. for i := range app.Commands {
  100. setFlagsAndBeforeOnSubcommands(&app.Commands[i], defaultFlags, establishCustomPath)
  101. }
  102. app.EnableBashCompletion = true
  103. err := app.Run(os.Args)
  104. if err != nil {
  105. log.Fatal("Failed to run app with %s: %v", os.Args, err)
  106. }
  107. log.GetManager().Close()
  108. }
  109. func setFlagsAndBeforeOnSubcommands(command *cli.Command, defaultFlags []cli.Flag, before cli.BeforeFunc) {
  110. command.Flags = append(command.Flags, defaultFlags...)
  111. command.Before = establishCustomPath
  112. for i := range command.Subcommands {
  113. setFlagsAndBeforeOnSubcommands(&command.Subcommands[i], defaultFlags, before)
  114. }
  115. }
  116. func establishCustomPath(ctx *cli.Context) error {
  117. var providedCustom string
  118. var providedConf string
  119. var providedWorkPath string
  120. currentCtx := ctx
  121. for {
  122. if len(providedCustom) != 0 && len(providedConf) != 0 && len(providedWorkPath) != 0 {
  123. break
  124. }
  125. if currentCtx == nil {
  126. break
  127. }
  128. if currentCtx.IsSet("custom-path") && len(providedCustom) == 0 {
  129. providedCustom = currentCtx.String("custom-path")
  130. }
  131. if currentCtx.IsSet("config") && len(providedConf) == 0 {
  132. providedConf = currentCtx.String("config")
  133. }
  134. if currentCtx.IsSet("work-path") && len(providedWorkPath) == 0 {
  135. providedWorkPath = currentCtx.String("work-path")
  136. }
  137. currentCtx = currentCtx.Parent()
  138. }
  139. setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath)
  140. setAppHelpTemplates()
  141. if ctx.IsSet("version") {
  142. cli.ShowVersion(ctx)
  143. os.Exit(0)
  144. }
  145. return nil
  146. }
  147. func setAppHelpTemplates() {
  148. cli.AppHelpTemplate = adjustHelpTemplate(originalAppHelpTemplate)
  149. cli.CommandHelpTemplate = adjustHelpTemplate(originalCommandHelpTemplate)
  150. cli.SubcommandHelpTemplate = adjustHelpTemplate(originalSubcommandHelpTemplate)
  151. }
  152. func adjustHelpTemplate(originalTemplate string) string {
  153. overridden := ""
  154. if _, ok := os.LookupEnv("GITEA_CUSTOM"); ok {
  155. overridden = "(GITEA_CUSTOM)"
  156. }
  157. return fmt.Sprintf(`%s
  158. DEFAULT CONFIGURATION:
  159. CustomPath: %s %s
  160. CustomConf: %s
  161. AppPath: %s
  162. AppWorkPath: %s
  163. `, originalTemplate, setting.CustomPath, overridden, setting.CustomConf, setting.AppPath, setting.AppWorkPath)
  164. }
  165. func formatBuiltWith() string {
  166. version := runtime.Version()
  167. if len(MakeVersion) > 0 {
  168. version = MakeVersion + ", " + runtime.Version()
  169. }
  170. if len(Tags) == 0 {
  171. return " built with " + version
  172. }
  173. return " built with " + version + " : " + strings.ReplaceAll(Tags, " ", ", ")
  174. }