0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-07-18 04:22:51 +02:00

install signals in main

This commit is contained in:
TheFox0x7 2025-05-23 22:28:00 +02:00
parent 69cde4d79a
commit 4ae57e83b3
No known key found for this signature in database
GPG Key ID: 6CA33903484AF7C2
28 changed files with 67 additions and 205 deletions

View File

@ -39,10 +39,7 @@ var (
}
)
func runGenerateActionsRunnerToken(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runGenerateActionsRunnerToken(ctx context.Context, c *cli.Command) error {
setting.MustInstalled()
scope := c.String("scope")

View File

@ -93,10 +93,7 @@ var (
}
)
func runRepoSyncReleases(_ context.Context, _ *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runRepoSyncReleases(ctx context.Context, _ *cli.Command) error {
if err := initDB(ctx); err != nil {
return err
}

View File

@ -57,10 +57,7 @@ var (
}
)
func runListAuth(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runListAuth(ctx context.Context, c *cli.Command) error {
if err := initDB(ctx); err != nil {
return err
}
@ -91,14 +88,11 @@ func runListAuth(_ context.Context, c *cli.Command) error {
return nil
}
func runDeleteAuth(_ context.Context, c *cli.Command) error {
func runDeleteAuth(ctx context.Context, c *cli.Command) error {
if !c.IsSet("id") {
return errors.New("--id flag is missing")
}
ctx, cancel := installSignals()
defer cancel()
if err := initDB(ctx); err != nil {
return err
}

View File

@ -328,10 +328,7 @@ func (a *authService) getAuthSource(ctx context.Context, c *cli.Command, authTyp
}
// addLdapBindDn adds a new LDAP via Bind DN authentication source.
func (a *authService) addLdapBindDn(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func (a *authService) addLdapBindDn(ctx context.Context, c *cli.Command) error {
if err := a.initDB(ctx); err != nil {
return err
}
@ -353,10 +350,7 @@ func (a *authService) addLdapBindDn(_ context.Context, c *cli.Command) error {
}
// updateLdapBindDn updates a new LDAP via Bind DN authentication source.
func (a *authService) updateLdapBindDn(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func (a *authService) updateLdapBindDn(ctx context.Context, c *cli.Command) error {
if err := a.initDB(ctx); err != nil {
return err
}
@ -375,10 +369,7 @@ func (a *authService) updateLdapBindDn(_ context.Context, c *cli.Command) error
}
// addLdapSimpleAuth adds a new LDAP (simple auth) authentication source.
func (a *authService) addLdapSimpleAuth(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func (a *authService) addLdapSimpleAuth(ctx context.Context, c *cli.Command) error {
if err := a.initDB(ctx); err != nil {
return err
}
@ -400,10 +391,7 @@ func (a *authService) addLdapSimpleAuth(_ context.Context, c *cli.Command) error
}
// updateLdapSimpleAuth updates a new LDAP (simple auth) authentication source.
func (a *authService) updateLdapSimpleAuth(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func (a *authService) updateLdapSimpleAuth(ctx context.Context, c *cli.Command) error {
if err := a.initDB(ctx); err != nil {
return err
}

View File

@ -176,10 +176,7 @@ func parseOAuth2Config(c *cli.Command) *oauth2.Source {
}
}
func (a *authService) runAddOauth(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func (a *authService) runAddOauth(ctx context.Context, c *cli.Command) error {
if err := a.initDB(ctx); err != nil {
return err
}
@ -201,14 +198,11 @@ func (a *authService) runAddOauth(_ context.Context, c *cli.Command) error {
})
}
func (a *authService) runUpdateOauth(_ context.Context, c *cli.Command) error {
func (a *authService) runUpdateOauth(ctx context.Context, c *cli.Command) error {
if !c.IsSet("id") {
return errors.New("--id flag is missing")
}
ctx, cancel := installSignals()
defer cancel()
if err := a.initDB(ctx); err != nil {
return err
}

View File

@ -129,10 +129,7 @@ func parseSMTPConfig(c *cli.Command, conf *smtp.Source) error {
return nil
}
func (a *authService) runAddSMTP(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func (a *authService) runAddSMTP(ctx context.Context, c *cli.Command) error {
if err := a.initDB(ctx); err != nil {
return err
}
@ -171,14 +168,11 @@ func (a *authService) runAddSMTP(_ context.Context, c *cli.Command) error {
})
}
func (a *authService) runUpdateSMTP(_ context.Context, c *cli.Command) error {
func (a *authService) runUpdateSMTP(ctx context.Context, c *cli.Command) error {
if !c.IsSet("id") {
return errors.New("--id flag is missing")
}
ctx, cancel := installSignals()
defer cancel()
if err := a.initDB(ctx); err != nil {
return err
}

View File

@ -27,20 +27,14 @@ var (
}
)
func runRegenerateHooks(_ context.Context, _ *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runRegenerateHooks(ctx context.Context, _ *cli.Command) error {
if err := initDB(ctx); err != nil {
return err
}
return repo_service.SyncRepositoryHooks(graceful.GetManager().ShutdownContext())
}
func runRegenerateKeys(_ context.Context, _ *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runRegenerateKeys(ctx context.Context, _ *cli.Command) error {
if err := initDB(ctx); err != nil {
return err
}

View File

@ -42,14 +42,11 @@ var microcmdUserChangePassword = &cli.Command{
},
}
func runChangePassword(_ context.Context, c *cli.Command) error {
func runChangePassword(ctx context.Context, c *cli.Command) error {
if err := argsSet(c, "username", "password"); err != nil {
return err
}
ctx, cancel := installSignals()
defer cancel()
if err := initDB(ctx); err != nil {
return err
}

View File

@ -140,9 +140,6 @@ func runCreateUser(ctx context.Context, c *cli.Command) error {
if !setting.IsInTesting {
// FIXME: need to refactor the "installSignals/initDB" related code later
// it doesn't make sense to call it in (almost) every command action function
var cancel context.CancelFunc
ctx, cancel = installSignals()
defer cancel()
if err := initDB(ctx); err != nil {
return err
}

View File

@ -42,14 +42,11 @@ var microcmdUserDelete = &cli.Command{
Action: runDeleteUser,
}
func runDeleteUser(_ context.Context, c *cli.Command) error {
func runDeleteUser(ctx context.Context, c *cli.Command) error {
if !c.IsSet("id") && !c.IsSet("username") && !c.IsSet("email") {
return errors.New("You must provide the id, username or email of a user to delete")
}
ctx, cancel := installSignals()
defer cancel()
if err := initDB(ctx); err != nil {
return err
}

View File

@ -42,14 +42,11 @@ var microcmdUserGenerateAccessToken = &cli.Command{
Action: runGenerateAccessToken,
}
func runGenerateAccessToken(_ context.Context, c *cli.Command) error {
func runGenerateAccessToken(ctx context.Context, c *cli.Command) error {
if !c.IsSet("username") {
return errors.New("you must provide a username to generate a token for")
}
ctx, cancel := installSignals()
defer cancel()
if err := initDB(ctx); err != nil {
return err
}

View File

@ -26,10 +26,7 @@ var microcmdUserList = &cli.Command{
},
}
func runListUsers(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runListUsers(ctx context.Context, c *cli.Command) error {
if err := initDB(ctx); err != nil {
return err
}

View File

@ -35,10 +35,7 @@ var microcmdUserMustChangePassword = &cli.Command{
},
}
func runMustChangePassword(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runMustChangePassword(ctx context.Context, c *cli.Command) error {
if c.NArg() == 0 && !c.IsSet("all") {
return errors.New("either usernames or --all must be provided")
}

View File

@ -93,10 +93,7 @@ You should back-up your database before doing this and ensure that your database
Action: runRecreateTable,
}
func runRecreateTable(_ context.Context, cmd *cli.Command) error {
stdCtx, cancel := installSignals()
defer cancel()
func runRecreateTable(ctx context.Context, cmd *cli.Command) error {
// Redirect the default golog to here
golog.SetFlags(0)
golog.SetPrefix("")
@ -113,7 +110,7 @@ func runRecreateTable(_ context.Context, cmd *cli.Command) error {
}
setting.Database.LogSQL = debug
if err := db.InitEngine(stdCtx); err != nil {
if err := db.InitEngine(ctx); err != nil {
fmt.Println(err)
fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.")
return nil
@ -131,7 +128,7 @@ func runRecreateTable(_ context.Context, cmd *cli.Command) error {
}
recreateTables := migrate_base.RecreateTables(beans...)
return db.InitEngineWithMigration(stdCtx, func(ctx context.Context, x *xorm.Engine) error {
return db.InitEngineWithMigration(ctx, func(ctx context.Context, x *xorm.Engine) error {
if err := migrations.EnsureUpToDate(ctx, x); err != nil {
return err
}
@ -161,10 +158,7 @@ func setupDoctorDefaultLogger(cmd *cli.Command, colorize bool) {
}
}
func runDoctorCheck(_ context.Context, cmd *cli.Command) error {
stdCtx, cancel := installSignals()
defer cancel()
func runDoctorCheck(ctx context.Context, cmd *cli.Command) error {
colorize := log.CanColorStdout
if cmd.IsSet("color") {
colorize = cmd.Bool("color")
@ -217,5 +211,5 @@ func runDoctorCheck(_ context.Context, cmd *cli.Command) error {
}
}
}
return doctor.RunChecks(stdCtx, colorize, cmd.Bool("fix"), checks)
return doctor.RunChecks(ctx, colorize, cmd.Bool("fix"), checks)
}

View File

@ -22,11 +22,8 @@ var cmdDoctorConvert = &cli.Command{
Action: runDoctorConvert,
}
func runDoctorConvert(_ context.Context, cmd *cli.Command) error {
stdCtx, cancel := installSignals()
defer cancel()
if err := initDB(stdCtx); err != nil {
func runDoctorConvert(ctx context.Context, cmd *cli.Command) error {
if err := initDB(ctx); err != nil {
return err
}

View File

@ -102,7 +102,7 @@ func fatal(format string, args ...any) {
log.Fatal(format, args...)
}
func runDump(_ context.Context, cmd *cli.Command) error {
func runDump(ctx context.Context, cmd *cli.Command) error {
setting.MustInstalled()
quite := cmd.Bool("quiet")
@ -137,10 +137,7 @@ func runDump(_ context.Context, cmd *cli.Command) error {
setting.DisableLoggerInit()
setting.LoadSettings() // cannot access session settings otherwise
stdCtx, cancel := installSignals()
defer cancel()
err := db.InitEngine(stdCtx)
err := db.InitEngine(ctx)
if err != nil {
return err
}

View File

@ -79,11 +79,8 @@ wiki, issues, labels, releases, release_assets, milestones, pull_requests, comme
},
}
func runDumpRepository(_ context.Context, cmd *cli.Command) error {
stdCtx, cancel := installSignals()
defer cancel()
if err := initDB(stdCtx); err != nil {
func runDumpRepository(ctx context.Context, cmd *cli.Command) error {
if err := initDB(ctx); err != nil {
return err
}

View File

@ -161,12 +161,10 @@ func (n *nilWriter) WriteString(s string) (int, error) {
return len(s), nil
}
func runHookPreReceive(_ context.Context, c *cli.Command) error {
func runHookPreReceive(ctx context.Context, c *cli.Command) error {
if isInternal, _ := strconv.ParseBool(os.Getenv(repo_module.EnvIsInternal)); isInternal {
return nil
}
ctx, cancel := installSignals()
defer cancel()
setup(ctx, c.Bool("debug"))
@ -309,10 +307,7 @@ func runHookUpdate(_ context.Context, c *cli.Command) error {
return nil
}
func runHookPostReceive(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runHookPostReceive(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))
// First of all run update-server-info no matter what
@ -496,10 +491,7 @@ func pushOptions() map[string]string {
return opts
}
func runHookProcReceive(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runHookProcReceive(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {

View File

@ -50,7 +50,7 @@ var CmdKeys = &cli.Command{
},
}
func runKeys(_ context.Context, c *cli.Command) error {
func runKeys(ctx context.Context, c *cli.Command) error {
if !c.IsSet("username") {
return errors.New("No username provided")
}
@ -69,9 +69,6 @@ func runKeys(_ context.Context, c *cli.Command) error {
return errors.New("No key type and content provided")
}
ctx, cancel := installSignals()
defer cancel()
setup(ctx, c.Bool("debug"))
authorizedString, extra := private.AuthorizedPublicKeyByContent(ctx, content)

View File

@ -13,10 +13,7 @@ import (
"github.com/urfave/cli/v3"
)
func runSendMail(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runSendMail(ctx context.Context, c *cli.Command) error {
setting.MustInstalled()
if err := argsSet(c, "title"); err != nil {

View File

@ -173,7 +173,9 @@ func NewMainApp(appVer AppVersion) *cli.Command {
}
func RunMainApp(app *cli.Command, args ...string) error {
err := app.Run(context.Background(), args)
ctx, cancel := installSignals()
defer cancel()
err := app.Run(ctx, args)
if err == nil {
return nil
}

View File

@ -109,46 +109,31 @@ var (
}
)
func runShutdown(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runShutdown(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))
extra := private.Shutdown(ctx)
return handleCliResponseExtra(extra)
}
func runRestart(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runRestart(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))
extra := private.Restart(ctx)
return handleCliResponseExtra(extra)
}
func runReloadTemplates(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runReloadTemplates(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))
extra := private.ReloadTemplates(ctx)
return handleCliResponseExtra(extra)
}
func runFlushQueues(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runFlushQueues(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))
extra := private.FlushQueues(ctx, c.Duration("timeout"), c.Bool("non-blocking"))
return handleCliResponseExtra(extra)
}
func runProcesses(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runProcesses(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))
extra := private.Processes(ctx, os.Stdout, c.Bool("flat"), c.Bool("no-system"), c.Bool("stacktraces"), c.Bool("json"), c.String("cancel"))
return handleCliResponseExtra(extra)

View File

@ -196,10 +196,7 @@ var (
}
)
func runRemoveLogger(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runRemoveLogger(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))
logger := c.String("logger")
if len(logger) == 0 {
@ -211,10 +208,7 @@ func runRemoveLogger(_ context.Context, c *cli.Command) error {
return handleCliResponseExtra(extra)
}
func runAddConnLogger(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runAddConnLogger(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))
vals := map[string]any{}
mode := "conn"
@ -238,13 +232,10 @@ func runAddConnLogger(_ context.Context, c *cli.Command) error {
if c.IsSet("reconnect-on-message") {
vals["reconnectOnMsg"] = c.Bool("reconnect-on-message")
}
return commonAddLogger(c, mode, vals)
return commonAddLogger(ctx, c, mode, vals)
}
func runAddFileLogger(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runAddFileLogger(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))
vals := map[string]any{}
mode := "file"
@ -271,10 +262,10 @@ func runAddFileLogger(_ context.Context, c *cli.Command) error {
if c.IsSet("compression-level") {
vals["compressionLevel"] = c.Int("compression-level")
}
return commonAddLogger(c, mode, vals)
return commonAddLogger(ctx, c, mode, vals)
}
func commonAddLogger(c *cli.Command, mode string, vals map[string]any) error {
func commonAddLogger(ctx context.Context, c *cli.Command, mode string, vals map[string]any) error {
if len(c.String("level")) > 0 {
vals["level"] = log.LevelFromString(c.String("level")).String()
}
@ -301,46 +292,33 @@ func commonAddLogger(c *cli.Command, mode string, vals map[string]any) error {
if c.IsSet("writer") {
writer = c.String("writer")
}
ctx, cancel := installSignals()
defer cancel()
extra := private.AddLogger(ctx, logger, writer, mode, vals)
return handleCliResponseExtra(extra)
}
func runPauseLogging(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runPauseLogging(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))
userMsg := private.PauseLogging(ctx)
_, _ = fmt.Fprintln(os.Stdout, userMsg)
return nil
}
func runResumeLogging(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runResumeLogging(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))
userMsg := private.ResumeLogging(ctx)
_, _ = fmt.Fprintln(os.Stdout, userMsg)
return nil
}
func runReleaseReopenLogging(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runReleaseReopenLogging(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))
userMsg := private.ReleaseReopenLogging(ctx)
_, _ = fmt.Fprintln(os.Stdout, userMsg)
return nil
}
func runSetLogSQL(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runSetLogSQL(ctx context.Context, c *cli.Command) error {
setup(ctx, c.Bool("debug"))
extra := private.SetLogSQL(ctx, !c.Bool("off"))

View File

@ -22,11 +22,8 @@ var CmdMigrate = &cli.Command{
Action: runMigrate,
}
func runMigrate(_ context.Context, c *cli.Command) error {
stdCtx, cancel := installSignals()
defer cancel()
if err := initDB(stdCtx); err != nil {
func runMigrate(ctx context.Context, c *cli.Command) error {
if err := initDB(ctx); err != nil {
return err
}

View File

@ -213,11 +213,8 @@ func migrateActionsArtifacts(ctx context.Context, dstStorage storage.ObjectStora
})
}
func runMigrateStorage(_ context.Context, cmd *cli.Command) error {
stdCtx, cancel := installSignals()
defer cancel()
if err := initDB(stdCtx); err != nil {
func runMigrateStorage(ctx context.Context, cmd *cli.Command) error {
if err := initDB(ctx); err != nil {
return err
}
@ -248,13 +245,13 @@ func runMigrateStorage(_ context.Context, cmd *cli.Command) error {
return nil
}
dstStorage, err = storage.NewLocalStorage(
stdCtx,
ctx,
&setting.Storage{
Path: p,
})
case string(setting.MinioStorageType):
dstStorage, err = storage.NewMinioStorage(
stdCtx,
ctx,
&setting.Storage{
MinioConfig: setting.MinioStorageConfig{
Endpoint: cmd.String("minio-endpoint"),
@ -271,7 +268,7 @@ func runMigrateStorage(_ context.Context, cmd *cli.Command) error {
})
case string(setting.AzureBlobStorageType):
dstStorage, err = storage.NewAzureBlobStorage(
stdCtx,
ctx,
&setting.Storage{
AzureBlobConfig: setting.AzureBlobStorageConfig{
Endpoint: cmd.String("azureblob-endpoint"),
@ -301,7 +298,7 @@ func runMigrateStorage(_ context.Context, cmd *cli.Command) error {
tp := strings.ToLower(cmd.String("type"))
if m, ok := migratedMethods[tp]; ok {
if err := m(stdCtx, dstStorage); err != nil {
if err := m(ctx, dstStorage); err != nil {
return err
}
log.Info("%s files have successfully been copied to the new storage.", tp)

View File

@ -49,10 +49,7 @@ wiki, issues, labels, releases, release_assets, milestones, pull_requests, comme
},
}
func runRestoreRepository(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runRestoreRepository(ctx context.Context, c *cli.Command) error {
setting.MustInstalled()
var units []string
if s := c.String("units"); s != "" {

View File

@ -152,10 +152,7 @@ func getLFSAuthToken(ctx context.Context, lfsVerb string, results *private.ServC
return "Bearer " + tokenString, nil
}
func runServ(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runServ(ctx context.Context, c *cli.Command) error {
// FIXME: This needs to internationalised
setup(ctx, c.Bool("debug"))

View File

@ -105,16 +105,14 @@ OPTIONS:
`
app.Action = runBackport
if err := app.Run(context.Background(), os.Args); err != nil {
ctx, cancel := installSignals()
defer cancel()
if err := app.Run(ctx, os.Args); err != nil {
fmt.Fprintf(os.Stderr, "Unable to backport: %v\n", err)
}
}
func runBackport(_ context.Context, c *cli.Command) error {
ctx, cancel := installSignals()
defer cancel()
func runBackport(ctx context.Context, c *cli.Command) error {
continuing := c.Bool("continue")
var pr string