[CLI] implement forgejo-cli (squash) support initDB

(cherry picked from commit 5c31ae602a45f1d9a90b86bece5393bc9faddf25)
(cherry picked from commit 1da62f29303543f177e903a70a71858926608ef5)
This commit is contained in:
Earl Warren 2023-07-14 12:03:40 +02:00
parent e868b286c9
commit fe410f108a
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
2 changed files with 27 additions and 6 deletions

View file

@ -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() {

View file

@ -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)