2014-02-19 23:34:31 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2016-12-21 17:43:17 +05:30
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2014-02-19 15:20:53 +05:30
|
|
|
|
2016-11-11 19:26:35 +05:30
|
|
|
// Gitea (git with a cup of tea) is a painless self-hosted Git Service.
|
|
|
|
package main // import "code.gitea.io/gitea"
|
2014-02-12 23:19:46 +05:30
|
|
|
|
|
|
|
import (
|
2019-04-29 23:38:21 +05:30
|
|
|
"fmt"
|
2014-02-19 15:20:53 +05:30
|
|
|
"os"
|
2019-01-24 20:52:51 +05:30
|
|
|
"runtime"
|
2017-02-28 06:10:02 +05:30
|
|
|
"strings"
|
2020-11-18 04:14:52 +05:30
|
|
|
"time"
|
2016-12-01 05:26:15 +05:30
|
|
|
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/cmd"
|
2017-01-04 18:46:03 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2018-11-01 19:11:07 +05:30
|
|
|
|
2017-09-21 10:50:14 +05:30
|
|
|
// register supported doc types
|
2022-06-09 03:16:39 +05:30
|
|
|
_ "code.gitea.io/gitea/modules/markup/console"
|
2018-07-21 02:38:15 +05:30
|
|
|
_ "code.gitea.io/gitea/modules/markup/csv"
|
2017-09-21 10:50:14 +05:30
|
|
|
_ "code.gitea.io/gitea/modules/markup/markdown"
|
|
|
|
_ "code.gitea.io/gitea/modules/markup/orgmode"
|
|
|
|
|
2016-11-04 18:14:23 +05:30
|
|
|
"github.com/urfave/cli"
|
2014-02-12 23:19:46 +05:30
|
|
|
)
|
|
|
|
|
2019-04-02 21:40:11 +05:30
|
|
|
var (
|
|
|
|
// Version holds the current Gitea version
|
2020-01-25 18:51:22 +05:30
|
|
|
Version = "development"
|
2019-04-02 21:40:11 +05:30
|
|
|
// Tags holds the build tags used
|
|
|
|
Tags = ""
|
|
|
|
// MakeVersion holds the current Make version if built with make
|
|
|
|
MakeVersion = ""
|
2019-04-29 23:38:21 +05:30
|
|
|
|
|
|
|
originalAppHelpTemplate = ""
|
|
|
|
originalCommandHelpTemplate = ""
|
|
|
|
originalSubcommandHelpTemplate = ""
|
2019-04-02 21:40:11 +05:30
|
|
|
)
|
2017-02-28 06:10:02 +05:30
|
|
|
|
2014-02-13 01:24:09 +05:30
|
|
|
func init() {
|
2016-11-04 17:02:04 +05:30
|
|
|
setting.AppVer = Version
|
2019-06-13 01:11:28 +05:30
|
|
|
setting.AppBuiltWith = formatBuiltWith()
|
2020-11-18 04:14:52 +05:30
|
|
|
setting.AppStartTime = time.Now().UTC()
|
2019-04-29 23:38:21 +05:30
|
|
|
|
|
|
|
// Grab the original help templates
|
|
|
|
originalAppHelpTemplate = cli.AppHelpTemplate
|
|
|
|
originalCommandHelpTemplate = cli.CommandHelpTemplate
|
|
|
|
originalSubcommandHelpTemplate = cli.SubcommandHelpTemplate
|
2014-02-13 01:24:09 +05:30
|
|
|
}
|
|
|
|
|
2014-02-12 23:19:46 +05:30
|
|
|
func main() {
|
2014-02-19 15:20:53 +05:30
|
|
|
app := cli.NewApp()
|
2016-11-11 19:26:35 +05:30
|
|
|
app.Name = "Gitea"
|
|
|
|
app.Usage = "A painless self-hosted Git service"
|
2018-01-10 10:28:08 +05:30
|
|
|
app.Description = `By default, gitea will start serving using the webserver with no
|
|
|
|
arguments - which can alternatively be run by running the subcommand web.`
|
2019-06-13 01:11:28 +05:30
|
|
|
app.Version = Version + formatBuiltWith()
|
2014-02-19 15:20:53 +05:30
|
|
|
app.Commands = []cli.Command{
|
2014-05-02 06:51:46 +05:30
|
|
|
cmd.CmdWeb,
|
|
|
|
cmd.CmdServ,
|
2017-02-23 09:10:44 +05:30
|
|
|
cmd.CmdHook,
|
2014-06-11 04:41:53 +05:30
|
|
|
cmd.CmdDump,
|
2014-09-23 03:00:58 +05:30
|
|
|
cmd.CmdCert,
|
2016-08-14 04:41:52 +05:30
|
|
|
cmd.CmdAdmin,
|
2018-02-18 23:44:37 +05:30
|
|
|
cmd.CmdGenerate,
|
2018-10-31 08:44:42 +05:30
|
|
|
cmd.CmdMigrate,
|
2018-11-01 19:11:07 +05:30
|
|
|
cmd.CmdKeys,
|
2019-06-08 19:23:45 +05:30
|
|
|
cmd.CmdConvert,
|
2020-01-11 19:54:57 +05:30
|
|
|
cmd.CmdDoctor,
|
2020-01-29 06:31:06 +05:30
|
|
|
cmd.CmdManager,
|
2020-02-02 07:47:44 +05:30
|
|
|
cmd.Cmdembedded,
|
2020-08-18 09:53:45 +05:30
|
|
|
cmd.CmdMigrateStorage,
|
2020-11-07 06:02:57 +05:30
|
|
|
cmd.CmdDocs,
|
2020-12-27 09:04:19 +05:30
|
|
|
cmd.CmdDumpRepository,
|
|
|
|
cmd.CmdRestoreRepository,
|
2014-02-19 15:20:53 +05:30
|
|
|
}
|
2019-04-29 23:38:21 +05:30
|
|
|
// Now adjust these commands to add our global configuration options
|
|
|
|
|
|
|
|
// First calculate the default paths and set the AppHelpTemplates in this context
|
2019-05-14 20:50:35 +05:30
|
|
|
setting.SetCustomPathAndConf("", "", "")
|
2019-04-29 23:38:21 +05:30
|
|
|
setAppHelpTemplates()
|
|
|
|
|
|
|
|
// default configuration flags
|
|
|
|
defaultFlags := []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "custom-path, C",
|
|
|
|
Value: setting.CustomPath,
|
|
|
|
Usage: "Custom path file path",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: setting.CustomConf,
|
|
|
|
Usage: "Custom configuration file path",
|
|
|
|
},
|
|
|
|
cli.VersionFlag,
|
2019-05-14 20:50:35 +05:30
|
|
|
cli.StringFlag{
|
|
|
|
Name: "work-path, w",
|
|
|
|
Value: setting.AppWorkPath,
|
|
|
|
Usage: "Set the gitea working path",
|
|
|
|
},
|
2019-04-29 23:38:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Set the default to be equivalent to cmdWeb and add the default flags
|
2018-11-01 06:06:41 +05:30
|
|
|
app.Flags = append(app.Flags, cmd.CmdWeb.Flags...)
|
2019-04-29 23:38:21 +05:30
|
|
|
app.Flags = append(app.Flags, defaultFlags...)
|
2018-01-10 10:28:08 +05:30
|
|
|
app.Action = cmd.CmdWeb.Action
|
2019-04-29 23:38:21 +05:30
|
|
|
|
|
|
|
// Add functions to set these paths and these flags to the commands
|
|
|
|
app.Before = establishCustomPath
|
|
|
|
for i := range app.Commands {
|
|
|
|
setFlagsAndBeforeOnSubcommands(&app.Commands[i], defaultFlags, establishCustomPath)
|
|
|
|
}
|
|
|
|
|
2016-12-01 05:26:15 +05:30
|
|
|
err := app.Run(os.Args)
|
|
|
|
if err != nil {
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Fatal("Failed to run app with %s: %v", os.Args, err)
|
2016-12-01 05:26:15 +05:30
|
|
|
}
|
2014-02-12 23:19:46 +05:30
|
|
|
}
|
2017-02-28 06:10:02 +05:30
|
|
|
|
2019-04-29 23:38:21 +05:30
|
|
|
func setFlagsAndBeforeOnSubcommands(command *cli.Command, defaultFlags []cli.Flag, before cli.BeforeFunc) {
|
|
|
|
command.Flags = append(command.Flags, defaultFlags...)
|
|
|
|
command.Before = establishCustomPath
|
|
|
|
for i := range command.Subcommands {
|
|
|
|
setFlagsAndBeforeOnSubcommands(&command.Subcommands[i], defaultFlags, before)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func establishCustomPath(ctx *cli.Context) error {
|
|
|
|
var providedCustom string
|
|
|
|
var providedConf string
|
2019-05-14 20:50:35 +05:30
|
|
|
var providedWorkPath string
|
2019-04-29 23:38:21 +05:30
|
|
|
|
|
|
|
currentCtx := ctx
|
|
|
|
for {
|
2019-05-14 20:50:35 +05:30
|
|
|
if len(providedCustom) != 0 && len(providedConf) != 0 && len(providedWorkPath) != 0 {
|
2019-04-29 23:38:21 +05:30
|
|
|
break
|
|
|
|
}
|
|
|
|
if currentCtx == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if currentCtx.IsSet("custom-path") && len(providedCustom) == 0 {
|
|
|
|
providedCustom = currentCtx.String("custom-path")
|
|
|
|
}
|
|
|
|
if currentCtx.IsSet("config") && len(providedConf) == 0 {
|
|
|
|
providedConf = currentCtx.String("config")
|
|
|
|
}
|
2019-05-14 20:50:35 +05:30
|
|
|
if currentCtx.IsSet("work-path") && len(providedWorkPath) == 0 {
|
|
|
|
providedWorkPath = currentCtx.String("work-path")
|
|
|
|
}
|
2019-04-29 23:38:21 +05:30
|
|
|
currentCtx = currentCtx.Parent()
|
|
|
|
|
|
|
|
}
|
2019-05-14 20:50:35 +05:30
|
|
|
setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath)
|
2019-04-29 23:38:21 +05:30
|
|
|
|
|
|
|
setAppHelpTemplates()
|
|
|
|
|
|
|
|
if ctx.IsSet("version") {
|
|
|
|
cli.ShowVersion(ctx)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setAppHelpTemplates() {
|
|
|
|
cli.AppHelpTemplate = adjustHelpTemplate(originalAppHelpTemplate)
|
|
|
|
cli.CommandHelpTemplate = adjustHelpTemplate(originalCommandHelpTemplate)
|
|
|
|
cli.SubcommandHelpTemplate = adjustHelpTemplate(originalSubcommandHelpTemplate)
|
|
|
|
}
|
|
|
|
|
|
|
|
func adjustHelpTemplate(originalTemplate string) string {
|
2022-08-07 06:24:26 +05:30
|
|
|
overridden := ""
|
2019-04-29 23:38:21 +05:30
|
|
|
if _, ok := os.LookupEnv("GITEA_CUSTOM"); ok {
|
2022-08-07 06:24:26 +05:30
|
|
|
overridden = "(GITEA_CUSTOM)"
|
2019-04-29 23:38:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(`%s
|
|
|
|
DEFAULT CONFIGURATION:
|
|
|
|
CustomPath: %s %s
|
|
|
|
CustomConf: %s
|
|
|
|
AppPath: %s
|
|
|
|
AppWorkPath: %s
|
|
|
|
|
2022-08-07 06:24:26 +05:30
|
|
|
`, originalTemplate, setting.CustomPath, overridden, setting.CustomConf, setting.AppPath, setting.AppWorkPath)
|
2019-04-29 23:38:21 +05:30
|
|
|
}
|
|
|
|
|
2019-06-13 01:11:28 +05:30
|
|
|
func formatBuiltWith() string {
|
2022-01-20 23:16:10 +05:30
|
|
|
version := runtime.Version()
|
2019-04-02 21:40:11 +05:30
|
|
|
if len(MakeVersion) > 0 {
|
|
|
|
version = MakeVersion + ", " + runtime.Version()
|
|
|
|
}
|
2017-02-28 06:10:02 +05:30
|
|
|
if len(Tags) == 0 {
|
2019-04-02 21:40:11 +05:30
|
|
|
return " built with " + version
|
2017-02-28 06:10:02 +05:30
|
|
|
}
|
|
|
|
|
2020-10-12 01:57:20 +05:30
|
|
|
return " built with " + version + " : " + strings.ReplaceAll(Tags, " ", ", ")
|
2017-02-28 06:10:02 +05:30
|
|
|
}
|