diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-07 11:11:27 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-07 11:11:27 +0800 |
commit | 69b61d437357235700306f28d22d21acc39bb2b5 (patch) | |
tree | 33af1d4d797b428556a323837fcc3eda38a54479 /cmd | |
parent | c9110eb5e4657e018695f084f37e1b423939e9e0 (diff) | |
download | gitea-69b61d437357235700306f28d22d21acc39bb2b5.tar.gz gitea-69b61d437357235700306f28d22d21acc39bb2b5.zip |
Fix bug on admin subcommand (#17533)
* Fix bug on admin subcommand
* Add signals for all initDB
Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/admin.go | 55 | ||||
-rw-r--r-- | cmd/admin_auth_ldap.go | 23 | ||||
-rw-r--r-- | cmd/admin_auth_ldap_test.go | 9 | ||||
-rw-r--r-- | cmd/cmd.go | 9 | ||||
-rw-r--r-- | cmd/convert.go | 5 | ||||
-rw-r--r-- | cmd/doctor.go | 10 | ||||
-rw-r--r-- | cmd/dump.go | 5 | ||||
-rw-r--r-- | cmd/dump_repo.go | 5 | ||||
-rw-r--r-- | cmd/migrate.go | 5 | ||||
-rw-r--r-- | cmd/migrate_storage.go | 5 |
10 files changed, 99 insertions, 32 deletions
diff --git a/cmd/admin.go b/cmd/admin.go index 099083ae91..64106f5060 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -339,7 +339,10 @@ func runChangePassword(c *cli.Context) error { return err } - if err := initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := initDB(ctx); err != nil { return err } if !pwd.IsComplexEnough(c.String("password")) { @@ -393,7 +396,10 @@ func runCreateUser(c *cli.Context) error { fmt.Fprintf(os.Stderr, "--name flag is deprecated. Use --username instead.\n") } - if err := initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := initDB(ctx); err != nil { return err } @@ -456,7 +462,10 @@ func runCreateUser(c *cli.Context) error { } func runListUsers(c *cli.Context) error { - if err := initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := initDB(ctx); err != nil { return err } @@ -493,7 +502,10 @@ func runDeleteUser(c *cli.Context) error { return fmt.Errorf("You must provide the id, username or email of a user to delete") } - if err := initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := initDB(ctx); err != nil { return err } @@ -525,7 +537,10 @@ func runDeleteUser(c *cli.Context) error { } func runRepoSyncReleases(_ *cli.Context) error { - if err := initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := initDB(ctx); err != nil { return err } @@ -591,14 +606,20 @@ func getReleaseCount(id int64) (int64, error) { } func runRegenerateHooks(_ *cli.Context) error { - if err := initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := initDB(ctx); err != nil { return err } return repo_module.SyncRepositoryHooks(graceful.GetManager().ShutdownContext()) } func runRegenerateKeys(_ *cli.Context) error { - if err := initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := initDB(ctx); err != nil { return err } return models.RewriteAllPublicKeys() @@ -628,7 +649,10 @@ func parseOAuth2Config(c *cli.Context) *oauth2.Source { } func runAddOauth(c *cli.Context) error { - if err := initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := initDB(ctx); err != nil { return err } @@ -645,7 +669,10 @@ func runUpdateOauth(c *cli.Context) error { return fmt.Errorf("--id flag is missing") } - if err := initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := initDB(ctx); err != nil { return err } @@ -712,7 +739,10 @@ func runUpdateOauth(c *cli.Context) error { } func runListAuth(c *cli.Context) error { - if err := initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := initDB(ctx); err != nil { return err } @@ -748,7 +778,10 @@ func runDeleteAuth(c *cli.Context) error { return fmt.Errorf("--id flag is missing") } - if err := initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := initDB(ctx); err != nil { return err } diff --git a/cmd/admin_auth_ldap.go b/cmd/admin_auth_ldap.go index 517904957f..950d515e39 100644 --- a/cmd/admin_auth_ldap.go +++ b/cmd/admin_auth_ldap.go @@ -5,6 +5,7 @@ package cmd import ( + "context" "fmt" "strings" @@ -16,7 +17,7 @@ import ( type ( authService struct { - initDB func() error + initDB func(ctx context.Context) error createLoginSource func(loginSource *login.Source) error updateLoginSource func(loginSource *login.Source) error getLoginSourceByID func(id int64) (*login.Source, error) @@ -299,7 +300,10 @@ func (a *authService) addLdapBindDn(c *cli.Context) error { return err } - if err := a.initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := a.initDB(ctx); err != nil { return err } @@ -321,7 +325,10 @@ func (a *authService) addLdapBindDn(c *cli.Context) error { // updateLdapBindDn updates a new LDAP via Bind DN authentication source. func (a *authService) updateLdapBindDn(c *cli.Context) error { - if err := a.initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := a.initDB(ctx); err != nil { return err } @@ -344,7 +351,10 @@ func (a *authService) addLdapSimpleAuth(c *cli.Context) error { return err } - if err := a.initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := a.initDB(ctx); err != nil { return err } @@ -366,7 +376,10 @@ func (a *authService) addLdapSimpleAuth(c *cli.Context) error { // updateLdapBindDn updates a new LDAP (simple auth) authentication source. func (a *authService) updateLdapSimpleAuth(c *cli.Context) error { - if err := a.initDB(); err != nil { + ctx, cancel := installSignals() + defer cancel() + + if err := a.initDB(ctx); err != nil { return err } diff --git a/cmd/admin_auth_ldap_test.go b/cmd/admin_auth_ldap_test.go index db1ba13bcd..15880639d6 100644 --- a/cmd/admin_auth_ldap_test.go +++ b/cmd/admin_auth_ldap_test.go @@ -5,6 +5,7 @@ package cmd import ( + "context" "testing" "code.gitea.io/gitea/models/login" @@ -207,7 +208,7 @@ func TestAddLdapBindDn(t *testing.T) { // Mock functions. var createdLoginSource *login.Source service := &authService{ - initDB: func() error { + initDB: func(context.Context) error { return nil }, createLoginSource: func(loginSource *login.Source) error { @@ -438,7 +439,7 @@ func TestAddLdapSimpleAuth(t *testing.T) { // Mock functions. var createdLoginSource *login.Source service := &authService{ - initDB: func() error { + initDB: func(context.Context) error { return nil }, createLoginSource: func(loginSource *login.Source) error { @@ -863,7 +864,7 @@ func TestUpdateLdapBindDn(t *testing.T) { // Mock functions. var updatedLoginSource *login.Source service := &authService{ - initDB: func() error { + initDB: func(context.Context) error { return nil }, createLoginSource: func(loginSource *login.Source) error { @@ -1227,7 +1228,7 @@ func TestUpdateLdapSimpleAuth(t *testing.T) { // Mock functions. var updatedLoginSource *login.Source service := &authService{ - initDB: func() error { + initDB: func(context.Context) error { return nil }, createLoginSource: func(loginSource *login.Source) error { diff --git a/cmd/cmd.go b/cmd/cmd.go index ea025cd98e..b89dd5d8b8 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -56,16 +56,15 @@ func confirm() (bool, error) { } } -func initDB() error { - return initDBDisableConsole(false) +func initDB(ctx context.Context) error { + return initDBDisableConsole(ctx, false) } -func initDBDisableConsole(disableConsole bool) error { +func initDBDisableConsole(ctx context.Context, disableConsole bool) error { setting.NewContext() setting.InitDBConfig() - setting.NewXORMLogService(disableConsole) - if err := db.InitEngine(); err != nil { + if err := db.InitEngine(ctx); err != nil { return fmt.Errorf("models.SetEngine: %v", err) } return nil diff --git a/cmd/convert.go b/cmd/convert.go index 20e4045c34..0b2e240b32 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -23,7 +23,10 @@ var CmdConvert = cli.Command{ } func runConvert(ctx *cli.Context) error { - if err := initDB(); err != nil { + stdCtx, cancel := installSignals() + defer cancel() + + if err := initDB(stdCtx); err != nil { return err } diff --git a/cmd/doctor.go b/cmd/doctor.go index 19b26f09c6..498b859e41 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -96,7 +96,10 @@ func runRecreateTable(ctx *cli.Context) error { setting.Cfg.Section("log").Key("XORM").SetValue(",") setting.NewXORMLogService(!ctx.Bool("debug")) - if err := db.InitEngine(); err != nil { + stdCtx, cancel := installSignals() + defer cancel() + + if err := db.InitEngine(stdCtx); 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 @@ -128,6 +131,9 @@ func runDoctor(ctx *cli.Context) error { log.DelNamedLogger("console") log.DelNamedLogger(log.DEFAULT) + stdCtx, cancel := installSignals() + defer cancel() + // Now setup our own logFile := ctx.String("log-file") if !ctx.IsSet("log-file") { @@ -210,5 +216,5 @@ func runDoctor(ctx *cli.Context) error { logger := log.GetLogger("doctorouter") defer logger.Close() - return doctor.RunChecks(logger, ctx.Bool("fix"), checks) + return doctor.RunChecks(stdCtx, logger, ctx.Bool("fix"), checks) } diff --git a/cmd/dump.go b/cmd/dump.go index 70ed6c2b55..efb9397208 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -173,7 +173,10 @@ func runDump(ctx *cli.Context) error { } setting.NewServices() // cannot access session settings otherwise - err := db.InitEngine() + stdCtx, cancel := installSignals() + defer cancel() + + err := db.InitEngine(stdCtx) if err != nil { return err } diff --git a/cmd/dump_repo.go b/cmd/dump_repo.go index 3ea82f6d1a..6274b4d865 100644 --- a/cmd/dump_repo.go +++ b/cmd/dump_repo.go @@ -76,7 +76,10 @@ wiki, issues, labels, releases, release_assets, milestones, pull_requests, comme } func runDumpRepository(ctx *cli.Context) error { - if err := initDB(); err != nil { + stdCtx, cancel := installSignals() + defer cancel() + + if err := initDB(stdCtx); err != nil { return err } diff --git a/cmd/migrate.go b/cmd/migrate.go index 7bb87d8400..054772d9ec 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -24,7 +24,10 @@ var CmdMigrate = cli.Command{ } func runMigrate(ctx *cli.Context) error { - if err := initDB(); err != nil { + stdCtx, cancel := installSignals() + defer cancel() + + if err := initDB(stdCtx); err != nil { return err } diff --git a/cmd/migrate_storage.go b/cmd/migrate_storage.go index 7c7629490e..1829ad00ba 100644 --- a/cmd/migrate_storage.go +++ b/cmd/migrate_storage.go @@ -107,7 +107,10 @@ func migrateRepoAvatars(dstStorage storage.ObjectStorage) error { } func runMigrateStorage(ctx *cli.Context) error { - if err := initDB(); err != nil { + stdCtx, cancel := installSignals() + defer cancel() + + if err := initDB(stdCtx); err != nil { return err } |