start_windows.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/spf13/cobra"
  7. "github.com/drakkan/sftpgo/v2/service"
  8. "github.com/drakkan/sftpgo/v2/utils"
  9. )
  10. var (
  11. startCmd = &cobra.Command{
  12. Use: "start",
  13. Short: "Start SFTPGo Windows Service",
  14. Run: func(cmd *cobra.Command, args []string) {
  15. configDir = utils.CleanDirInput(configDir)
  16. if !filepath.IsAbs(logFilePath) && utils.IsFileInputValid(logFilePath) {
  17. logFilePath = filepath.Join(configDir, logFilePath)
  18. }
  19. s := service.Service{
  20. ConfigDir: configDir,
  21. ConfigFile: configFile,
  22. LogFilePath: logFilePath,
  23. LogMaxSize: logMaxSize,
  24. LogMaxBackups: logMaxBackups,
  25. LogMaxAge: logMaxAge,
  26. LogCompress: logCompress,
  27. LogVerbose: logVerbose,
  28. Shutdown: make(chan bool),
  29. }
  30. winService := service.WindowsService{
  31. Service: s,
  32. }
  33. err := winService.RunService()
  34. if err != nil {
  35. fmt.Printf("Error starting service: %v\r\n", err)
  36. os.Exit(1)
  37. } else {
  38. fmt.Printf("Service started!\r\n")
  39. }
  40. },
  41. }
  42. )
  43. func init() {
  44. serviceCmd.AddCommand(startCmd)
  45. addServeFlags(startCmd)
  46. }