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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.InitProviderFromExistingFile()
  51. setting.LoadCommonSettings()
  52. setting.LoadDBSetting()
  53. setting.InitSQLLog(false)
  54. if setting.Database.Type == "" {
  55. log.Fatal(`Database settings are missing from the configuration file: %q.
  56. Ensure you are running in the correct environment or set the correct configuration file with -c.
  57. If this is the intended configuration file complete the [database] section.`, setting.CustomConf)
  58. }
  59. if err := db.InitEngine(ctx); err != nil {
  60. return fmt.Errorf("unable to initialize the database using the configuration in %q. Error: %w", setting.CustomConf, err)
  61. }
  62. return nil
  63. }
  64. func installSignals() (context.Context, context.CancelFunc) {
  65. ctx, cancel := context.WithCancel(context.Background())
  66. go func() {
  67. // install notify
  68. signalChannel := make(chan os.Signal, 1)
  69. signal.Notify(
  70. signalChannel,
  71. syscall.SIGINT,
  72. syscall.SIGTERM,
  73. )
  74. select {
  75. case <-signalChannel:
  76. case <-ctx.Done():
  77. }
  78. cancel()
  79. signal.Reset()
  80. }()
  81. return ctx, cancel
  82. }