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

(cherry picked from commit 5c31ae602a)
This commit is contained in:
Earl Warren 2023-07-14 12:03:40 +02:00
parent 0983b62ca0
commit 1da62f2930
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" "os/signal"
"syscall" "syscall"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/private"
"code.gitea.io/gitea/modules/setting"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -19,7 +22,7 @@ import (
type key int type key int
const ( const (
noInstallSignalsKey key = iota + 1 noInitKey key = iota + 1
noExitKey noExitKey
stdoutKey stdoutKey
stderrKey stderrKey
@ -37,12 +40,12 @@ func CmdForgejo(ctx context.Context) cli.Command {
} }
} }
func ContextSetNoInstallSignals(ctx context.Context, value bool) context.Context { func ContextSetNoInit(ctx context.Context, value bool) context.Context {
return context.WithValue(ctx, noInstallSignalsKey, value) return context.WithValue(ctx, noInitKey, value)
} }
func ContextGetNoInstallSignals(ctx context.Context) bool { func ContextGetNoInit(ctx context.Context) bool {
value, ok := ctx.Value(noInstallSignalsKey).(bool) value, ok := ctx.Value(noInitKey).(bool)
return ok && value return ok && value
} }
@ -91,6 +94,24 @@ func ContextGetStdin(ctx context.Context) io.Reader {
return value 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) { func installSignals(ctx context.Context) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
go func() { go func() {

View file

@ -24,7 +24,7 @@ func cmdForgejoCaptureOutput(t *testing.T, args []string, stdin ...string) (stri
assert.NoError(t, set.Parse(args)) assert.NoError(t, set.Parse(args))
cliContext := cli.NewContext(&cli.App{Writer: w, ErrWriter: w}, set, nil) cliContext := cli.NewContext(&cli.App{Writer: w, ErrWriter: w}, set, nil)
ctx := context.Background() ctx := context.Background()
ctx = forgejo.ContextSetNoInstallSignals(ctx, true) ctx = forgejo.ContextSetNoInit(ctx, true)
ctx = forgejo.ContextSetNoExit(ctx, true) ctx = forgejo.ContextSetNoExit(ctx, true)
ctx = forgejo.ContextSetStdout(ctx, w) ctx = forgejo.ContextSetStdout(ctx, w)
ctx = forgejo.ContextSetStderr(ctx, w) ctx = forgejo.ContextSetStderr(ctx, w)