2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2021-11-15 17:40:31 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/config"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
2021-11-15 17:40:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
resetProviderForce bool
|
|
|
|
resetProviderCmd = &cobra.Command{
|
|
|
|
Use: "resetprovider",
|
|
|
|
Short: "Reset the configured provider, any data will be lost",
|
|
|
|
Long: `This command reads the data provider connection details from the specified
|
|
|
|
configuration file and resets the provider by deleting all data and schemas.
|
|
|
|
This command is not supported for the memory provider.
|
|
|
|
|
|
|
|
Please take a look at the usage below to customize the options.`,
|
2022-07-19 21:28:33 +00:00
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
2021-11-15 17:40:31 +00:00
|
|
|
logger.DisableLogger()
|
|
|
|
logger.EnableConsoleLogger(zerolog.DebugLevel)
|
|
|
|
configDir = util.CleanDirInput(configDir)
|
|
|
|
err := config.LoadConfig(configDir, configFile)
|
|
|
|
if err != nil {
|
2023-01-16 17:54:42 +00:00
|
|
|
logger.WarnToConsole("Unable to load configuration: %v", err)
|
2021-11-15 17:40:31 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
kmsConfig := config.GetKMSConfig()
|
|
|
|
err = kmsConfig.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("unable to initialize KMS: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
if !resetProviderForce {
|
2022-11-16 18:04:50 +00:00
|
|
|
logger.WarnToConsole("You are about to delete all the SFTPGo data for provider %q, config file: %q",
|
2021-11-15 17:40:31 +00:00
|
|
|
providerConf.Driver, viper.ConfigFileUsed())
|
|
|
|
logger.WarnToConsole("Are you sure? (Y/n)")
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
answer, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("unable to read your answer: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if strings.ToUpper(strings.TrimSpace(answer)) != "Y" {
|
|
|
|
logger.InfoToConsole("command aborted")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2022-11-16 18:04:50 +00:00
|
|
|
logger.InfoToConsole("Resetting provider: %q, config file: %q", providerConf.Driver, viper.ConfigFileUsed())
|
2021-11-15 17:40:31 +00:00
|
|
|
err = dataprovider.ResetDatabase(providerConf, configDir)
|
|
|
|
if err != nil {
|
|
|
|
logger.WarnToConsole("Error resetting provider: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
logger.InfoToConsole("Tha data provider was successfully reset")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
addConfigFlags(resetProviderCmd)
|
|
|
|
resetProviderCmd.Flags().BoolVar(&resetProviderForce, "force", false, `reset the provider without asking for confirmation`)
|
|
|
|
|
|
|
|
rootCmd.AddCommand(resetProviderCmd)
|
|
|
|
}
|