start_windows.go 1.8 KB

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