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

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