The Wayback Machine - https://web.archive.org/web/20221218034411/https://github.com/go-gitea/gitea/blame/main/main.go
Skip to content
Permalink
Newer
Older
100644 198 lines (170 sloc) 5.19 KB
1
// Copyright 2014 The Gogs Authors. All rights reserved.
2
// Copyright 2016 The Gitea Authors. All rights reserved.
3
// SPDX-License-Identifier: MIT
5
// Gitea (git with a cup of tea) is a painless self-hosted Git Service.
6
package main // import "code.gitea.io/gitea"
Feb 12, 2014
7
8
import (
10
"os"
14
16
"code.gitea.io/gitea/modules/log"
17
"code.gitea.io/gitea/modules/setting"
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
25
"github.com/urfave/cli"
Feb 12, 2014
26
)
27
28
var (
29
// Version holds the current Gitea version
30
Version = "development"
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 = ""
Feb 12, 2014
41
func init() {
42
setting.AppVer = Version
43
setting.AppBuiltWith = formatBuiltWith()
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
Feb 12, 2014
50
}
51
Feb 12, 2014
52
func main() {
53
app := cli.NewApp()
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.`
58
app.Version = Version + formatBuiltWith()
59
app.Commands = []cli.Command{
60
cmd.CmdWeb,
61
cmd.CmdServ,
62
cmd.CmdHook,
63
cmd.CmdDump,
65
cmd.CmdAdmin,
66
cmd.CmdGenerate,
67
cmd.CmdMigrate,
68
cmd.CmdKeys,
73
cmd.CmdMigrateStorage,
74
cmd.CmdDocs,
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
81
setting.SetCustomPathAndConf("", "", "")
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...)
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
115
err := app.Run(os.Args)
116
if err != nil {
117
log.Fatal("Failed to run app with %s: %v", os.Args, err)
118
}
Feb 12, 2014
119
}
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
132
var providedWorkPath string
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 {
173
overridden := ""
174
if _, ok := os.LookupEnv("GITEA_CUSTOM"); ok {
175
overridden = "(GITEA_CUSTOM)"
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)
188
func formatBuiltWith() string {
189
version := runtime.Version()
190
if len(MakeVersion) > 0 {
191
version = MakeVersion + ", " + runtime.Version()
192
}
193
if len(Tags) == 0 {
194
return " built with " + version
197
return " built with " + version + " : " + strings.ReplaceAll(Tags, " ", ", ")