Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. }
  69. // Now adjust these commands to add our global configuration options
  70. // First calculate the default paths and set the AppHelpTemplates in this context
  71. setting.SetCustomPathAndConf("", "", "")
  72. setAppHelpTemplates()
  73. // default configuration flags
  74. defaultFlags := []cli.Flag{
  75. cli.StringFlag{
  76. Name: "custom-path, C",
  77. Value: setting.CustomPath,
  78. Usage: "Custom path file path",
  79. },
  80. cli.StringFlag{
  81. Name: "config, c",
  82. Value: setting.CustomConf,
  83. Usage: "Custom configuration file path",
  84. },
  85. cli.VersionFlag,
  86. cli.StringFlag{
  87. Name: "work-path, w",
  88. Value: setting.AppWorkPath,
  89. Usage: "Set the gitea working path",
  90. },
  91. }
  92. // Set the default to be equivalent to cmdWeb and add the default flags
  93. app.Flags = append(app.Flags, cmd.CmdWeb.Flags...)
  94. app.Flags = append(app.Flags, defaultFlags...)
  95. app.Action = cmd.CmdWeb.Action
  96. // Add functions to set these paths and these flags to the commands
  97. app.Before = establishCustomPath
  98. for i := range app.Commands {
  99. setFlagsAndBeforeOnSubcommands(&app.Commands[i], defaultFlags, establishCustomPath)
  100. }
  101. app.EnableBashCompletion = true
  102. err := app.Run(os.Args)
  103. if err != nil {
  104. log.Fatal("Failed to run app with %s: %v", os.Args, err)
  105. }
  106. }
  107. func setFlagsAndBeforeOnSubcommands(command *cli.Command, defaultFlags []cli.Flag, before cli.BeforeFunc) {
  108. command.Flags = append(command.Flags, defaultFlags...)
  109. command.Before = establishCustomPath
  110. for i := range command.Subcommands {
  111. setFlagsAndBeforeOnSubcommands(&command.Subcommands[i], defaultFlags, before)
  112. }
  113. }
  114. func establishCustomPath(ctx *cli.Context) error {
  115. var providedCustom string
  116. var providedConf string
  117. var providedWorkPath string
  118. currentCtx := ctx
  119. for {
  120. if len(providedCustom) != 0 && len(providedConf) != 0 && len(providedWorkPath) != 0 {
  121. break
  122. }
  123. if currentCtx == nil {
  124. break
  125. }
  126. if currentCtx.IsSet("custom-path") && len(providedCustom) == 0 {
  127. providedCustom = currentCtx.String("custom-path")
  128. }
  129. if currentCtx.IsSet("config") && len(providedConf) == 0 {
  130. providedConf = currentCtx.String("config")
  131. }
  132. if currentCtx.IsSet("work-path") && len(providedWorkPath) == 0 {
  133. providedWorkPath = currentCtx.String("work-path")
  134. }
  135. currentCtx = currentCtx.Parent()
  136. }
  137. setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath)
  138. setAppHelpTemplates()
  139. if ctx.IsSet("version") {
  140. cli.ShowVersion(ctx)
  141. os.Exit(0)
  142. }
  143. return nil
  144. }
  145. func setAppHelpTemplates() {
  146. cli.AppHelpTemplate = adjustHelpTemplate(originalAppHelpTemplate)
  147. cli.CommandHelpTemplate = adjustHelpTemplate(originalCommandHelpTemplate)
  148. cli.SubcommandHelpTemplate = adjustHelpTemplate(originalSubcommandHelpTemplate)
  149. }
  150. func adjustHelpTemplate(originalTemplate string) string {
  151. overridden := ""
  152. if _, ok := os.LookupEnv("GITEA_CUSTOM"); ok {
  153. overridden = "(GITEA_CUSTOM)"
  154. }
  155. return fmt.Sprintf(`%s
  156. DEFAULT CONFIGURATION:
  157. CustomPath: %s %s
  158. CustomConf: %s
  159. AppPath: %s
  160. AppWorkPath: %s
  161. `, originalTemplate, setting.CustomPath, overridden, setting.CustomConf, setting.AppPath, setting.AppWorkPath)
  162. }
  163. func formatBuiltWith() string {
  164. version := runtime.Version()
  165. if len(MakeVersion) > 0 {
  166. version = MakeVersion + ", " + runtime.Version()
  167. }
  168. if len(Tags) == 0 {
  169. return " built with " + version
  170. }
  171. return " built with " + version + " : " + strings.ReplaceAll(Tags, " ", ", ")
  172. }