install_windows.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "strconv"
  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. installCmd = &cobra.Command{
  25. Use: "install",
  26. Short: "Install SFTPGo as Windows Service",
  27. Long: `To install the SFTPGo Windows Service with the default values for the command
  28. line flags simply use:
  29. sftpgo service install
  30. Please take a look at the usage below to customize the startup options`,
  31. Run: func(_ *cobra.Command, _ []string) {
  32. s := service.Service{
  33. ConfigDir: util.CleanDirInput(configDir),
  34. ConfigFile: configFile,
  35. LogFilePath: logFilePath,
  36. LogMaxSize: logMaxSize,
  37. LogMaxBackups: logMaxBackups,
  38. LogMaxAge: logMaxAge,
  39. LogCompress: logCompress,
  40. LogLevel: logLevel,
  41. LogUTCTime: logUTCTime,
  42. Shutdown: make(chan bool),
  43. }
  44. winService := service.WindowsService{
  45. Service: s,
  46. }
  47. serviceArgs := []string{"service", "start"}
  48. customFlags := getCustomServeFlags()
  49. if len(customFlags) > 0 {
  50. serviceArgs = append(serviceArgs, customFlags...)
  51. }
  52. err := winService.Install(serviceArgs...)
  53. if err != nil {
  54. fmt.Printf("Error installing service: %v\r\n", err)
  55. os.Exit(1)
  56. } else {
  57. fmt.Printf("Service installed!\r\n")
  58. }
  59. },
  60. }
  61. )
  62. func init() {
  63. serviceCmd.AddCommand(installCmd)
  64. addServeFlags(installCmd)
  65. }
  66. func getCustomServeFlags() []string {
  67. result := []string{}
  68. if configDir != defaultConfigDir {
  69. configDir = util.CleanDirInput(configDir)
  70. result = append(result, "--"+configDirFlag)
  71. result = append(result, configDir)
  72. }
  73. if configFile != defaultConfigFile {
  74. result = append(result, "--"+configFileFlag)
  75. result = append(result, configFile)
  76. }
  77. if logFilePath != defaultLogFile {
  78. result = append(result, "--"+logFilePathFlag)
  79. result = append(result, logFilePath)
  80. }
  81. if logMaxSize != defaultLogMaxSize {
  82. result = append(result, "--"+logMaxSizeFlag)
  83. result = append(result, strconv.Itoa(logMaxSize))
  84. }
  85. if logMaxBackups != defaultLogMaxBackup {
  86. result = append(result, "--"+logMaxBackupFlag)
  87. result = append(result, strconv.Itoa(logMaxBackups))
  88. }
  89. if logMaxAge != defaultLogMaxAge {
  90. result = append(result, "--"+logMaxAgeFlag)
  91. result = append(result, strconv.Itoa(logMaxAge))
  92. }
  93. if logLevel != defaultLogLevel {
  94. result = append(result, "--"+logLevelFlag)
  95. result = append(result, logLevel)
  96. }
  97. if logUTCTime != defaultLogUTCTime {
  98. result = append(result, "--"+logUTCTimeFlag+"=true")
  99. }
  100. if logCompress != defaultLogCompress {
  101. result = append(result, "--"+logCompressFlag+"=true")
  102. }
  103. if graceTime != defaultGraceTime {
  104. result = append(result, "--"+graceTimeFlag)
  105. result = append(result, strconv.Itoa(graceTime))
  106. }
  107. return result
  108. }