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

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