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 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/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(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. return initDBDisableConsole(ctx, false)
  51. }
  52. func initDBDisableConsole(ctx context.Context, disableConsole bool) error {
  53. setting.NewContext()
  54. setting.InitDBConfig()
  55. setting.NewXORMLogService(disableConsole)
  56. if err := db.InitEngine(ctx); err != nil {
  57. return fmt.Errorf("models.SetEngine: %v", err)
  58. }
  59. return nil
  60. }
  61. func installSignals() (context.Context, context.CancelFunc) {
  62. ctx, cancel := context.WithCancel(context.Background())
  63. go func() {
  64. // install notify
  65. signalChannel := make(chan os.Signal, 1)
  66. signal.Notify(
  67. signalChannel,
  68. syscall.SIGINT,
  69. syscall.SIGTERM,
  70. )
  71. select {
  72. case <-signalChannel:
  73. case <-ctx.Done():
  74. }
  75. cancel()
  76. signal.Reset()
  77. }()
  78. return ctx, cancel
  79. }