serve.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (C) 2019 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package cmd
  15. import (
  16. "os"
  17. "path/filepath"
  18. "strconv"
  19. "strings"
  20. "github.com/spf13/cobra"
  21. "github.com/subosito/gotenv"
  22. "github.com/drakkan/sftpgo/v2/internal/service"
  23. "github.com/drakkan/sftpgo/v2/internal/util"
  24. )
  25. const (
  26. envFileMaxSize = 1048576
  27. )
  28. var (
  29. serveCmd = &cobra.Command{
  30. Use: "serve",
  31. Short: "Start the SFTPGo service",
  32. Long: `To start the SFTPGo with the default values for the command line flags simply
  33. use:
  34. $ sftpgo serve
  35. Please take a look at the usage below to customize the startup options`,
  36. Run: func(_ *cobra.Command, _ []string) {
  37. configDir := util.CleanDirInput(configDir)
  38. checkServeParamsFromEnvFiles(configDir)
  39. service.SetGraceTime(graceTime)
  40. service := service.Service{
  41. ConfigDir: configDir,
  42. ConfigFile: configFile,
  43. LogFilePath: logFilePath,
  44. LogMaxSize: logMaxSize,
  45. LogMaxBackups: logMaxBackups,
  46. LogMaxAge: logMaxAge,
  47. LogCompress: logCompress,
  48. LogLevel: logLevel,
  49. LogUTCTime: logUTCTime,
  50. LoadDataFrom: loadDataFrom,
  51. LoadDataMode: loadDataMode,
  52. LoadDataQuotaScan: loadDataQuotaScan,
  53. LoadDataClean: loadDataClean,
  54. Shutdown: make(chan bool),
  55. }
  56. if err := service.Start(disableAWSInstallationCode); err == nil {
  57. service.Wait()
  58. if service.Error == nil {
  59. os.Exit(0)
  60. }
  61. }
  62. os.Exit(1)
  63. },
  64. }
  65. )
  66. func setIntFromEnv(receiver *int, val string) {
  67. converted, err := strconv.Atoi(val)
  68. if err == nil {
  69. *receiver = converted
  70. }
  71. }
  72. func setBoolFromEnv(receiver *bool, val string) {
  73. converted, err := strconv.ParseBool(strings.TrimSpace(val))
  74. if err == nil {
  75. *receiver = converted
  76. }
  77. }
  78. func checkServeParamsFromEnvFiles(configDir string) { //nolint:gocyclo
  79. // The logger is not yet initialized here, we have no way to report errors.
  80. envd := filepath.Join(configDir, "env.d")
  81. entries, err := os.ReadDir(envd)
  82. if err != nil {
  83. return
  84. }
  85. for _, entry := range entries {
  86. info, err := entry.Info()
  87. if err == nil && info.Mode().IsRegular() {
  88. envFile := filepath.Join(envd, entry.Name())
  89. if info.Size() > envFileMaxSize {
  90. continue
  91. }
  92. envVars, err := gotenv.Read(envFile)
  93. if err != nil {
  94. return
  95. }
  96. for k, v := range envVars {
  97. if _, isSet := os.LookupEnv(k); isSet {
  98. continue
  99. }
  100. switch k {
  101. case "SFTPGO_LOG_FILE_PATH":
  102. logFilePath = v
  103. case "SFTPGO_LOG_MAX_SIZE":
  104. setIntFromEnv(&logMaxSize, v)
  105. case "SFTPGO_LOG_MAX_BACKUPS":
  106. setIntFromEnv(&logMaxBackups, v)
  107. case "SFTPGO_LOG_MAX_AGE":
  108. setIntFromEnv(&logMaxAge, v)
  109. case "SFTPGO_LOG_COMPRESS":
  110. setBoolFromEnv(&logCompress, v)
  111. case "SFTPGO_LOG_LEVEL":
  112. logLevel = v
  113. case "SFTPGO_LOG_UTC_TIME":
  114. setBoolFromEnv(&logUTCTime, v)
  115. case "SFTPGO_CONFIG_FILE":
  116. configFile = v
  117. case "SFTPGO_LOADDATA_FROM":
  118. loadDataFrom = v
  119. case "SFTPGO_LOADDATA_MODE":
  120. setIntFromEnv(&loadDataMode, v)
  121. case "SFTPGO_LOADDATA_CLEAN":
  122. setBoolFromEnv(&loadDataClean, v)
  123. case "SFTPGO_LOADDATA_QUOTA_SCAN":
  124. setIntFromEnv(&loadDataQuotaScan, v)
  125. case "SFTPGO_GRACE_TIME":
  126. setIntFromEnv(&graceTime, v)
  127. }
  128. }
  129. }
  130. }
  131. }
  132. func init() {
  133. rootCmd.AddCommand(serveCmd)
  134. addServeFlags(serveCmd)
  135. addAWSContainerFlags(serveCmd)
  136. }