start_windows.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (C) 2019-2022 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. "fmt"
  17. "os"
  18. "path/filepath"
  19. "github.com/spf13/cobra"
  20. "github.com/drakkan/sftpgo/v2/service"
  21. "github.com/drakkan/sftpgo/v2/util"
  22. )
  23. var (
  24. startCmd = &cobra.Command{
  25. Use: "start",
  26. Short: "Start the SFTPGo Windows Service",
  27. Run: func(cmd *cobra.Command, args []string) {
  28. configDir = util.CleanDirInput(configDir)
  29. if !filepath.IsAbs(logFilePath) && util.IsFileInputValid(logFilePath) {
  30. logFilePath = filepath.Join(configDir, logFilePath)
  31. }
  32. s := service.Service{
  33. ConfigDir: configDir,
  34. ConfigFile: configFile,
  35. LogFilePath: logFilePath,
  36. LogMaxSize: logMaxSize,
  37. LogMaxBackups: logMaxBackups,
  38. LogMaxAge: logMaxAge,
  39. LogCompress: logCompress,
  40. LogVerbose: logVerbose,
  41. LogUTCTime: logUTCTime,
  42. Shutdown: make(chan bool),
  43. }
  44. winService := service.WindowsService{
  45. Service: s,
  46. }
  47. err := winService.RunService()
  48. if err != nil {
  49. fmt.Printf("Error starting service: %v\r\n", err)
  50. os.Exit(1)
  51. } else {
  52. fmt.Printf("Service started!\r\n")
  53. }
  54. },
  55. }
  56. )
  57. func init() {
  58. serviceCmd.AddCommand(startCmd)
  59. addServeFlags(startCmd)
  60. }