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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. err := app.Run(os.Args)
  102. if err != nil {
  103. log.Fatal("Failed to run app with %s: %v", os.Args, err)
  104. }
  105. }
  106. func setFlagsAndBeforeOnSubcommands(command *cli.Command, defaultFlags []cli.Flag, before cli.BeforeFunc) {
  107. command.Flags = append(command.Flags, defaultFlags...)
  108. command.Before = establishCustomPath
  109. for i := range command.Subcommands {
  110. setFlagsAndBeforeOnSubcommands(&command.Subcommands[i], defaultFlags, before)
  111. }
  112. }
  113. func establishCustomPath(ctx *cli.Context) error {
  114. var providedCustom string
  115. var providedConf string
  116. var providedWorkPath string
  117. currentCtx := ctx
  118. for {
  119. if len(providedCustom) != 0 && len(providedConf) != 0 && len(providedWorkPath) != 0 {
  120. break
  121. }
  122. if currentCtx == nil {
  123. break
  124. }
  125. if currentCtx.IsSet("custom-path") && len(providedCustom) == 0 {
  126. providedCustom = currentCtx.String("custom-path")
  127. }
  128. if currentCtx.IsSet("config") && len(providedConf) == 0 {
  129. providedConf = currentCtx.String("config")
  130. }
  131. if currentCtx.IsSet("work-path") && len(providedWorkPath) == 0 {
  132. providedWorkPath = currentCtx.String("work-path")
  133. }
  134. currentCtx = currentCtx.Parent()
  135. }
  136. setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath)
  137. setAppHelpTemplates()
  138. if ctx.IsSet("version") {
  139. cli.ShowVersion(ctx)
  140. os.Exit(0)
  141. }
  142. return nil
  143. }
  144. func setAppHelpTemplates() {
  145. cli.AppHelpTemplate = adjustHelpTemplate(originalAppHelpTemplate)
  146. cli.CommandHelpTemplate = adjustHelpTemplate(originalCommandHelpTemplate)
  147. cli.SubcommandHelpTemplate = adjustHelpTemplate(originalSubcommandHelpTemplate)
  148. }
  149. func adjustHelpTemplate(originalTemplate string) string {
  150. overridden := ""
  151. if _, ok := os.LookupEnv("GITEA_CUSTOM"); ok {
  152. overridden = "(GITEA_CUSTOM)"
  153. }
  154. return fmt.Sprintf(`%s
  155. DEFAULT CONFIGURATION:
  156. CustomPath: %s %s
  157. CustomConf: %s
  158. AppPath: %s
  159. AppWorkPath: %s
  160. `, originalTemplate, setting.CustomPath, overridden, setting.CustomConf, setting.AppPath, setting.AppWorkPath)
  161. }
  162. func formatBuiltWith() string {
  163. version := runtime.Version()
  164. if len(MakeVersion) > 0 {
  165. version = MakeVersion + ", " + runtime.Version()
  166. }
  167. if len(Tags) == 0 {
  168. return " built with " + version
  169. }
  170. return " built with " + version + " : " + strings.ReplaceAll(Tags, " ", ", ")
  171. }