aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorzeripath2019-04-29 19:08:21 +0100
committertechknowlogick2019-04-29 14:08:21 -0400
commit8d0d7bc28d517acc9ca98f0468d7ff2a4fdaf139 (patch)
tree0a8817abd2512ca2ebccfd4e5dc08814933aad2b /main.go
parentccf4783980effa871abaecd70d0f983c5062a187 (diff)
Make CustomPath, CustomConf and AppWorkPath configurable at build (#6631)
Diffstat (limited to 'main.go')
-rw-r--r--main.go103
1 files changed, 103 insertions, 0 deletions
diff --git a/main.go b/main.go
index 989ae1c58..102450f90 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@
package main // import "code.gitea.io/gitea"
import (
+ "fmt"
"os"
"runtime"
"strings"
@@ -30,11 +31,20 @@ var (
Tags = ""
// MakeVersion holds the current Make version if built with make
MakeVersion = ""
+
+ originalAppHelpTemplate = ""
+ originalCommandHelpTemplate = ""
+ originalSubcommandHelpTemplate = ""
)
func init() {
setting.AppVer = Version
setting.AppBuiltWith = formatBuiltWith(Tags)
+
+ // Grab the original help templates
+ originalAppHelpTemplate = cli.AppHelpTemplate
+ originalCommandHelpTemplate = cli.CommandHelpTemplate
+ originalSubcommandHelpTemplate = cli.SubcommandHelpTemplate
}
func main() {
@@ -55,14 +65,107 @@ arguments - which can alternatively be run by running the subcommand web.`
cmd.CmdMigrate,
cmd.CmdKeys,
}
+ // Now adjust these commands to add our global configuration options
+
+ // First calculate the default paths and set the AppHelpTemplates in this context
+ setting.SetCustomPathAndConf("", "")
+ 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,
+ }
+
+ // Set the default to be equivalent to cmdWeb and add the default flags
app.Flags = append(app.Flags, cmd.CmdWeb.Flags...)
+ app.Flags = append(app.Flags, defaultFlags...)
app.Action = cmd.CmdWeb.Action
+
+ // 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)
+ }
+
err := app.Run(os.Args)
if err != nil {
log.Fatal("Failed to run app with %s: %v", os.Args, err)
}
}
+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
+
+ currentCtx := ctx
+ for {
+ if len(providedCustom) != 0 && len(providedConf) != 0 {
+ 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")
+ }
+ currentCtx = currentCtx.Parent()
+
+ }
+ setting.SetCustomPathAndConf(providedCustom, providedConf)
+
+ 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 {
+ overrided := ""
+ if _, ok := os.LookupEnv("GITEA_CUSTOM"); ok {
+ overrided = "(GITEA_CUSTOM)"
+ }
+
+ return fmt.Sprintf(`%s
+DEFAULT CONFIGURATION:
+ CustomPath: %s %s
+ CustomConf: %s
+ AppPath: %s
+ AppWorkPath: %s
+
+`, originalTemplate, setting.CustomPath, overrided, setting.CustomConf, setting.AppPath, setting.AppWorkPath)
+}
+
func formatBuiltWith(makeTags string) string {
var version = runtime.Version()
if len(MakeVersion) > 0 {