resetprovider.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. "bufio"
  17. "os"
  18. "strings"
  19. "github.com/rs/zerolog"
  20. "github.com/spf13/cobra"
  21. "github.com/spf13/viper"
  22. "github.com/drakkan/sftpgo/v2/internal/config"
  23. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  24. "github.com/drakkan/sftpgo/v2/internal/logger"
  25. "github.com/drakkan/sftpgo/v2/internal/util"
  26. )
  27. var (
  28. resetProviderForce bool
  29. resetProviderCmd = &cobra.Command{
  30. Use: "resetprovider",
  31. Short: "Reset the configured provider, any data will be lost",
  32. Long: `This command reads the data provider connection details from the specified
  33. configuration file and resets the provider by deleting all data and schemas.
  34. This command is not supported for the memory provider.
  35. Please take a look at the usage below to customize the options.`,
  36. Run: func(_ *cobra.Command, _ []string) {
  37. logger.DisableLogger()
  38. logger.EnableConsoleLogger(zerolog.DebugLevel)
  39. configDir = util.CleanDirInput(configDir)
  40. err := config.LoadConfig(configDir, configFile)
  41. if err != nil {
  42. logger.WarnToConsole("Unable to initialize data provider, config load error: %v", err)
  43. os.Exit(1)
  44. }
  45. kmsConfig := config.GetKMSConfig()
  46. err = kmsConfig.Initialize()
  47. if err != nil {
  48. logger.ErrorToConsole("unable to initialize KMS: %v", err)
  49. os.Exit(1)
  50. }
  51. providerConf := config.GetProviderConf()
  52. if !resetProviderForce {
  53. logger.WarnToConsole("You are about to delete all the SFTPGo data for provider %q, config file: %q",
  54. providerConf.Driver, viper.ConfigFileUsed())
  55. logger.WarnToConsole("Are you sure? (Y/n)")
  56. reader := bufio.NewReader(os.Stdin)
  57. answer, err := reader.ReadString('\n')
  58. if err != nil {
  59. logger.ErrorToConsole("unable to read your answer: %v", err)
  60. os.Exit(1)
  61. }
  62. if strings.ToUpper(strings.TrimSpace(answer)) != "Y" {
  63. logger.InfoToConsole("command aborted")
  64. os.Exit(1)
  65. }
  66. }
  67. logger.InfoToConsole("Resetting provider: %q, config file: %q", providerConf.Driver, viper.ConfigFileUsed())
  68. err = dataprovider.ResetDatabase(providerConf, configDir)
  69. if err != nil {
  70. logger.WarnToConsole("Error resetting provider: %v", err)
  71. os.Exit(1)
  72. }
  73. logger.InfoToConsole("Tha data provider was successfully reset")
  74. },
  75. }
  76. )
  77. func init() {
  78. addConfigFlags(resetProviderCmd)
  79. resetProviderCmd.Flags().BoolVar(&resetProviderForce, "force", false, `reset the provider without asking for confirmation`)
  80. rootCmd.AddCommand(resetProviderCmd)
  81. }