From 1da62f29303543f177e903a70a71858926608ef5 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Fri, 14 Jul 2023 12:03:40 +0200 Subject: [PATCH] [CLI] implement forgejo-cli (squash) support initDB (cherry picked from commit 5c31ae602a45f1d9a90b86bece5393bc9faddf25) --- cmd/forgejo/forgejo.go | 31 ++++++++++++++++++++++----- tests/integration/cmd_forgejo_test.go | 2 +- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/cmd/forgejo/forgejo.go b/cmd/forgejo/forgejo.go index f9cbe38a4d..c9a9386349 100644 --- a/cmd/forgejo/forgejo.go +++ b/cmd/forgejo/forgejo.go @@ -11,7 +11,10 @@ import ( "os/signal" "syscall" + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/private" + "code.gitea.io/gitea/modules/setting" "github.com/urfave/cli" ) @@ -19,7 +22,7 @@ import ( type key int const ( - noInstallSignalsKey key = iota + 1 + noInitKey key = iota + 1 noExitKey stdoutKey stderrKey @@ -37,12 +40,12 @@ func CmdForgejo(ctx context.Context) cli.Command { } } -func ContextSetNoInstallSignals(ctx context.Context, value bool) context.Context { - return context.WithValue(ctx, noInstallSignalsKey, value) +func ContextSetNoInit(ctx context.Context, value bool) context.Context { + return context.WithValue(ctx, noInitKey, value) } -func ContextGetNoInstallSignals(ctx context.Context) bool { - value, ok := ctx.Value(noInstallSignalsKey).(bool) +func ContextGetNoInit(ctx context.Context) bool { + value, ok := ctx.Value(noInitKey).(bool) return ok && value } @@ -91,6 +94,24 @@ func ContextGetStdin(ctx context.Context) io.Reader { return value } +// copied from ../cmd.go +func initDB(ctx context.Context) error { + setting.MustInstalled() + setting.LoadDBSetting() + setting.InitSQLLoggersForCli(log.INFO) + + if setting.Database.Type == "" { + log.Fatal(`Database settings are missing from the configuration file: %q. +Ensure you are running in the correct environment or set the correct configuration file with -c. +If this is the intended configuration file complete the [database] section.`, setting.CustomConf) + } + if err := db.InitEngine(ctx); err != nil { + return fmt.Errorf("unable to initialize the database using the configuration in %q. Error: %w", setting.CustomConf, err) + } + return nil +} + +// copied from ../cmd.go func installSignals(ctx context.Context) (context.Context, context.CancelFunc) { ctx, cancel := context.WithCancel(ctx) go func() { diff --git a/tests/integration/cmd_forgejo_test.go b/tests/integration/cmd_forgejo_test.go index 079e1361fb..b98d197ca4 100644 --- a/tests/integration/cmd_forgejo_test.go +++ b/tests/integration/cmd_forgejo_test.go @@ -24,7 +24,7 @@ func cmdForgejoCaptureOutput(t *testing.T, args []string, stdin ...string) (stri assert.NoError(t, set.Parse(args)) cliContext := cli.NewContext(&cli.App{Writer: w, ErrWriter: w}, set, nil) ctx := context.Background() - ctx = forgejo.ContextSetNoInstallSignals(ctx, true) + ctx = forgejo.ContextSetNoInit(ctx, true) ctx = forgejo.ContextSetNoExit(ctx, true) ctx = forgejo.ContextSetStdout(ctx, w) ctx = forgejo.ContextSetStderr(ctx, w)