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. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. // Package cmd provides subcommands to the gitea binary - such as "web" or
  5. // "admin".
  6. package cmd
  7. import (
  8. "context"
  9. "errors"
  10. "fmt"
  11. "os"
  12. "os/signal"
  13. "strings"
  14. "syscall"
  15. "code.gitea.io/gitea/models/db"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/modules/util"
  19. "github.com/urfave/cli"
  20. )
  21. // argsSet checks that all the required arguments are set. args is a list of
  22. // arguments that must be set in the passed Context.
  23. func argsSet(c *cli.Context, args ...string) error {
  24. for _, a := range args {
  25. if !c.IsSet(a) {
  26. return errors.New(a + " is not set")
  27. }
  28. if util.IsEmptyString(a) {
  29. return errors.New(a + " is required")
  30. }
  31. }
  32. return nil
  33. }
  34. // confirm waits for user input which confirms an action
  35. func confirm() (bool, error) {
  36. var response string
  37. _, err := fmt.Scanln(&response)
  38. if err != nil {
  39. return false, err
  40. }
  41. switch strings.ToLower(response) {
  42. case "y", "yes":
  43. return true, nil
  44. case "n", "no":
  45. return false, nil
  46. default:
  47. return false, errors.New(response + " isn't a correct confirmation string")
  48. }
  49. }
  50. func initDB(ctx context.Context) error {
  51. setting.LoadFromExisting()
  52. setting.InitDBConfig()
  53. setting.NewXORMLogService(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 initialise the database using the configuration in %q. Error: %v", 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. }