mirror of
1
Fork 0

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

(cherry picked from commit 5c31ae602a)
(cherry picked from commit 1da62f2930)
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" "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)