mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-25 17:56:21 +00:00
16279d7922
(cherry picked from commit2555e315f7
) (cherry picked from commit51b9c9092e
) [CLI] implement forgejo-cli (squash) support initDB (cherry picked from commit5c31ae602a
) (cherry picked from commitbbf76489a7
) Conflicts: because ofd0dbe52e76
upgrade to https://pkg.go.dev/github.com/urfave/cli/v2 (cherry picked from commitb6c1bcc008
) [CLI] implement forgejo-cli actions (cherry picked from commit08be2b226e
) (cherry picked from commitb6cfa88c6e
) (cherry picked from commit59704200de
) [CLI] implement forgejo-cli actions generate-secret (cherry picked from commit6f7905c8ec
) (cherry picked from commite085d6d273
) [CLI] implement forgejo-cli actions generate-secret (squash) NoInit (cherry picked from commit962c944eb2
) [CLI] implement forgejo-cli actions register (cherry picked from commit2f95143000
) (cherry picked from commit42f2f8731e
) [CLI] implement forgejo-cli actions register (squash) no private Do not go through the private API, directly modify the database (cherry picked from commit1ba7c0d39d
) [CLI] implement forgejo-cli actions (cherry picked from commit6f7905c8ec
) (cherry picked from commite085d6d273
) [CLI] implement forgejo-cli actions generate-secret (squash) NoInit (cherry picked from commit962c944eb2
) (cherry picked from commit4c121ef022
) Conflicts: cmd/forgejo/actions.go tests/integration/cmd_forgejo_actions_test.go (cherry picked from commit36997a48e3
) [CLI] implement forgejo-cli actions (squash) restore --version Refs: https://codeberg.org/forgejo/forgejo/issues/1134 (cherry picked from commit9739eb52d8
) [CI] implement forgejo-cli (squash) the actions subcommand needs config (cherry picked from commit def638475122a26082ab3835842c84cd03839154) Conflicts: cmd/main.go https://codeberg.org/forgejo/forgejo/pulls/1209 (cherry picked from commita1758a3910
) (cherry picked from commit935fa650c7
) (cherry picked from commitcd21026bc9
) (cherry picked from commit1700b8973a
) (cherry picked from commit1def42a379
) (cherry picked from commit839d97521d
) (cherry picked from commitfd8c13be6b
) (cherry picked from commit588e5d552f
) (cherry picked from commit151a726620
) (cherry picked from commita93f3689a8
) (cherry picked from commit1e7bd54b28
) (cherry picked from commit7f6015382f
) (cherry picked from commit31afac5daf
) (cherry picked from commit6df8a604a5
) (cherry picked from commit912492f603
)
148 lines
3.2 KiB
Go
148 lines
3.2 KiB
Go
// Copyright The Forgejo Authors.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package forgejo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"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/v2"
|
|
)
|
|
|
|
type key int
|
|
|
|
const (
|
|
noInitKey key = iota + 1
|
|
noExitKey
|
|
stdoutKey
|
|
stderrKey
|
|
stdinKey
|
|
)
|
|
|
|
func CmdForgejo(ctx context.Context) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "forgejo-cli",
|
|
Usage: "Forgejo CLI",
|
|
Flags: []cli.Flag{},
|
|
Subcommands: []*cli.Command{
|
|
CmdActions(ctx),
|
|
},
|
|
}
|
|
}
|
|
|
|
func ContextSetNoInit(ctx context.Context, value bool) context.Context {
|
|
return context.WithValue(ctx, noInitKey, value)
|
|
}
|
|
|
|
func ContextGetNoInit(ctx context.Context) bool {
|
|
value, ok := ctx.Value(noInitKey).(bool)
|
|
return ok && value
|
|
}
|
|
|
|
func ContextSetNoExit(ctx context.Context, value bool) context.Context {
|
|
return context.WithValue(ctx, noExitKey, value)
|
|
}
|
|
|
|
func ContextGetNoExit(ctx context.Context) bool {
|
|
value, ok := ctx.Value(noExitKey).(bool)
|
|
return ok && value
|
|
}
|
|
|
|
func ContextSetStderr(ctx context.Context, value io.Writer) context.Context {
|
|
return context.WithValue(ctx, stderrKey, value)
|
|
}
|
|
|
|
func ContextGetStderr(ctx context.Context) io.Writer {
|
|
value, ok := ctx.Value(stderrKey).(io.Writer)
|
|
if !ok {
|
|
return os.Stderr
|
|
}
|
|
return value
|
|
}
|
|
|
|
func ContextSetStdout(ctx context.Context, value io.Writer) context.Context {
|
|
return context.WithValue(ctx, stdoutKey, value)
|
|
}
|
|
|
|
func ContextGetStdout(ctx context.Context) io.Writer {
|
|
value, ok := ctx.Value(stderrKey).(io.Writer)
|
|
if !ok {
|
|
return os.Stdout
|
|
}
|
|
return value
|
|
}
|
|
|
|
func ContextSetStdin(ctx context.Context, value io.Reader) context.Context {
|
|
return context.WithValue(ctx, stdinKey, value)
|
|
}
|
|
|
|
func ContextGetStdin(ctx context.Context) io.Reader {
|
|
value, ok := ctx.Value(stdinKey).(io.Reader)
|
|
if !ok {
|
|
return os.Stdin
|
|
}
|
|
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() {
|
|
// install notify
|
|
signalChannel := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(
|
|
signalChannel,
|
|
syscall.SIGINT,
|
|
syscall.SIGTERM,
|
|
)
|
|
select {
|
|
case <-signalChannel:
|
|
case <-ctx.Done():
|
|
}
|
|
cancel()
|
|
signal.Reset()
|
|
}()
|
|
|
|
return ctx, cancel
|
|
}
|
|
|
|
func handleCliResponseExtra(ctx context.Context, extra private.ResponseExtra) error {
|
|
if false && extra.UserMsg != "" {
|
|
if _, err := fmt.Fprintf(ContextGetStdout(ctx), "%s", extra.UserMsg); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
if ContextGetNoExit(ctx) {
|
|
return extra.Error
|
|
}
|
|
return cli.Exit(extra.Error, 1)
|
|
}
|