Skip to content

Fix bug on admin subcommand #17533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 44 additions & 11 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
23 changes: 18 additions & 5 deletions cmd/admin_auth_ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package cmd

import (
"context"
"fmt"
"strings"

Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
9 changes: 5 additions & 4 deletions cmd/admin_auth_ldap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package cmd

import (
"context"
"testing"

"code.gitea.io/gitea/models/login"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 8 additions & 2 deletions cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
}
5 changes: 4 additions & 1 deletion cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/dump_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/migrate_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading