You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmd.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. // Package cmd provides subcommands to the gitea binary - such as "web" or
  4. // "admin".
  5. package cmd
  6. import (
  7. "context"
  8. "errors"
  9. "fmt"
  10. "os"
  11. "os/signal"
  12. "strings"
  13. "syscall"
  14. "code.gitea.io/gitea/models/db"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. "code.gitea.io/gitea/modules/util"
  18. "github.com/urfave/cli"
  19. )
  20. // argsSet checks that all the required arguments are set. args is a list of
  21. // arguments that must be set in the passed Context.
  22. func argsSet(c *cli.Context, args ...string) error {
  23. for _, a := range args {
  24. if !c.IsSet(a) {
  25. return errors.New(a + " is not set")
  26. }
  27. if util.IsEmptyString(c.String(a)) {
  28. return errors.New(a + " is required")
  29. }
  30. }
  31. return nil
  32. }
  33. // confirm waits for user input which confirms an action
  34. func confirm() (bool, error) {
  35. var response string
  36. _, err := fmt.Scanln(&response)
  37. if err != nil {
  38. return false, err
  39. }
  40. switch strings.ToLower(response) {
  41. case "y", "yes":
  42. return true, nil
  43. case "n", "no":
  44. return false, nil
  45. default:
  46. return false, errors.New(response + " isn't a correct confirmation string")
  47. }
  48. }
  49. func initDB(ctx context.Context) error {
  50. setting.LoadFromExisting()
  51. setting.InitDBConfig()
  52. setting.NewXORMLogService(false)
  53. if setting.Database.Type == "" {
  54. log.Fatal(`Database settings are missing from the configuration file: %q.
  55. Ensure you are running in the correct environment or set the correct configuration file with -c.
  56. If this is the intended configuration file complete the [database] section.`, setting.CustomConf)
  57. }
  58. if err := db.InitEngine(ctx); err != nil {
  59. return fmt.Errorf("unable to initialize the database using the configuration in %q. Error: %w", setting.CustomConf, err)
  60. }
  61. return nil
  62. }
  63. func installSignals() (context.Context, context.CancelFunc) {
  64. ctx, cancel := context.WithCancel(context.Background())
  65. go func() {
  66. // install notify
  67. signalChannel := make(chan os.Signal, 1)
  68. signal.Notify(
  69. signalChannel,
  70. syscall.SIGINT,
  71. syscall.SIGTERM,
  72. )
  73. select {
  74. case <-signalChannel:
  75. case <-ctx.Done():
  76. }
  77. cancel()
  78. signal.Reset()
  79. }()
  80. return ctx, cancel
  81. }