Permalink
gitea/main.go
Newer
100644
198 lines (170 sloc)
5.19 KB
2
// Copyright 2016 The Gitea Authors. All rights reserved.
5
// Gitea (git with a cup of tea) is a painless self-hosted Git Service.
6
package main // import "code.gitea.io/gitea"
15
"code.gitea.io/gitea/cmd"
17
"code.gitea.io/gitea/modules/setting"
19
// register supported doc types
20
_ "code.gitea.io/gitea/modules/markup/console"
21
_ "code.gitea.io/gitea/modules/markup/csv"
22
_ "code.gitea.io/gitea/modules/markup/markdown"
23
_ "code.gitea.io/gitea/modules/markup/orgmode"
24
28
var (
29
// Version holds the current Gitea version
31
// Tags holds the build tags used
32
Tags = ""
33
// MakeVersion holds the current Make version if built with make
34
MakeVersion = ""
35
36
originalAppHelpTemplate = ""
37
originalCommandHelpTemplate = ""
38
originalSubcommandHelpTemplate = ""
44
setting.AppStartTime = time.Now().UTC()
45
46
// Grab the original help templates
47
originalAppHelpTemplate = cli.AppHelpTemplate
48
originalCommandHelpTemplate = cli.CommandHelpTemplate
49
originalSubcommandHelpTemplate = cli.SubcommandHelpTemplate
54
app.Name = "Gitea"
55
app.Usage = "A painless self-hosted Git service"
56
app.Description = `By default, gitea will start serving using the webserver with no
57
arguments - which can alternatively be run by running the subcommand web.`
70
cmd.CmdDoctor,
75
cmd.CmdDumpRepository,
76
cmd.CmdRestoreRepository,
78
// Now adjust these commands to add our global configuration options
79
80
// First calculate the default paths and set the AppHelpTemplates in this context
82
setAppHelpTemplates()
83
84
// default configuration flags
85
defaultFlags := []cli.Flag{
86
cli.StringFlag{
87
Name: "custom-path, C",
88
Value: setting.CustomPath,
89
Usage: "Custom path file path",
90
},
91
cli.StringFlag{
92
Name: "config, c",
93
Value: setting.CustomConf,
94
Usage: "Custom configuration file path",
95
},
96
cli.VersionFlag,
97
cli.StringFlag{
98
Name: "work-path, w",
99
Value: setting.AppWorkPath,
100
Usage: "Set the gitea working path",
101
},
102
}
103
104
// Set the default to be equivalent to cmdWeb and add the default flags
105
app.Flags = append(app.Flags, cmd.CmdWeb.Flags...)
106
app.Flags = append(app.Flags, defaultFlags...)
107
app.Action = cmd.CmdWeb.Action
108
109
// Add functions to set these paths and these flags to the commands
110
app.Before = establishCustomPath
111
for i := range app.Commands {
112
setFlagsAndBeforeOnSubcommands(&app.Commands[i], defaultFlags, establishCustomPath)
113
}
114
117
log.Fatal("Failed to run app with %s: %v", os.Args, err)
121
func setFlagsAndBeforeOnSubcommands(command *cli.Command, defaultFlags []cli.Flag, before cli.BeforeFunc) {
122
command.Flags = append(command.Flags, defaultFlags...)
123
command.Before = establishCustomPath
124
for i := range command.Subcommands {
125
setFlagsAndBeforeOnSubcommands(&command.Subcommands[i], defaultFlags, before)
126
}
127
}
128
129
func establishCustomPath(ctx *cli.Context) error {
130
var providedCustom string
131
var providedConf string
133
134
currentCtx := ctx
135
for {
136
if len(providedCustom) != 0 && len(providedConf) != 0 && len(providedWorkPath) != 0 {
137
break
138
}
139
if currentCtx == nil {
140
break
141
}
142
if currentCtx.IsSet("custom-path") && len(providedCustom) == 0 {
143
providedCustom = currentCtx.String("custom-path")
144
}
145
if currentCtx.IsSet("config") && len(providedConf) == 0 {
146
providedConf = currentCtx.String("config")
147
}
148
if currentCtx.IsSet("work-path") && len(providedWorkPath) == 0 {
149
providedWorkPath = currentCtx.String("work-path")
150
}
151
currentCtx = currentCtx.Parent()
152
153
}
154
setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath)
155
156
setAppHelpTemplates()
157
158
if ctx.IsSet("version") {
159
cli.ShowVersion(ctx)
160
os.Exit(0)
161
}
162
163
return nil
164
}
165
166
func setAppHelpTemplates() {
167
cli.AppHelpTemplate = adjustHelpTemplate(originalAppHelpTemplate)
168
cli.CommandHelpTemplate = adjustHelpTemplate(originalCommandHelpTemplate)
169
cli.SubcommandHelpTemplate = adjustHelpTemplate(originalSubcommandHelpTemplate)
170
}
171
172
func adjustHelpTemplate(originalTemplate string) string {
174
if _, ok := os.LookupEnv("GITEA_CUSTOM"); ok {
176
}
177
178
return fmt.Sprintf(`%s
179
DEFAULT CONFIGURATION:
180
CustomPath: %s %s
181
CustomConf: %s
182
AppPath: %s
183
AppWorkPath: %s
184
185
`, originalTemplate, setting.CustomPath, overridden, setting.CustomConf, setting.AppPath, setting.AppWorkPath)
190
if len(MakeVersion) > 0 {
191
version = MakeVersion + ", " + runtime.Version()
192
}
197
return " built with " + version + " : " + strings.ReplaceAll(Tags, " ", ", ")