install_windows.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/drakkan/sftpgo/service"
  5. "github.com/drakkan/sftpgo/utils"
  6. "github.com/spf13/cobra"
  7. )
  8. var (
  9. installCmd = &cobra.Command{
  10. Use: "install",
  11. Short: "Install SFTPGo as Windows Service",
  12. Long: `To install the SFTPGo Windows Service with the default values for the command line flags simply use:
  13. sftpgo service install
  14. Please take a look at the usage below to customize the startup options`,
  15. Run: func(cmd *cobra.Command, args []string) {
  16. s := service.Service{
  17. ConfigDir: utils.CleanDirInput(configDir),
  18. ConfigFile: configFile,
  19. LogFilePath: logFilePath,
  20. LogMaxSize: logMaxSize,
  21. LogMaxBackups: logMaxBackups,
  22. LogMaxAge: logMaxAge,
  23. LogCompress: logCompress,
  24. LogVerbose: logVerbose,
  25. Shutdown: make(chan bool),
  26. }
  27. winService := service.WindowsService{
  28. Service: s,
  29. }
  30. serviceArgs := []string{"service", "start"}
  31. customFlags := getCustomServeFlags()
  32. if len(customFlags) > 0 {
  33. serviceArgs = append(serviceArgs, customFlags...)
  34. }
  35. err := winService.Install(serviceArgs...)
  36. if err != nil {
  37. fmt.Printf("Error installing service: %v\r\n", err)
  38. } else {
  39. fmt.Printf("Service installed!\r\n")
  40. }
  41. },
  42. }
  43. )
  44. func init() {
  45. serviceCmd.AddCommand(installCmd)
  46. addServeFlags(installCmd)
  47. }