2020-02-08 13:44:25 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/drakkan/sftpgo/config"
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
2020-03-04 08:27:14 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-02-08 13:44:25 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
initProviderCmd = &cobra.Command{
|
|
|
|
Use: "initprovider",
|
|
|
|
Short: "Initializes the configured data provider",
|
|
|
|
Long: `This command reads the data provider connection details from the specified configuration file and creates the initial structure.
|
|
|
|
|
|
|
|
Some data providers such as bolt and memory does not require an initialization.
|
|
|
|
|
|
|
|
For SQLite provider the database file will be auto created if missing.
|
|
|
|
|
|
|
|
For PostgreSQL and MySQL providers you need to create the configured database, this command will create the required tables.
|
|
|
|
|
|
|
|
To initialize the data provider from the configuration directory simply use:
|
|
|
|
|
|
|
|
sftpgo initprovider
|
|
|
|
|
|
|
|
Please take a look at the usage below to customize the options.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
logger.DisableLogger()
|
|
|
|
logger.EnableConsoleLogger(zerolog.DebugLevel)
|
2020-03-04 08:27:14 +00:00
|
|
|
configDir = utils.CleanDirInput(configDir)
|
2020-04-30 12:23:55 +00:00
|
|
|
err := config.LoadConfig(configDir, configFile)
|
|
|
|
if err != nil {
|
|
|
|
logger.WarnToConsole("Unable to initialize data provider, config load error: %v", err)
|
|
|
|
}
|
2020-02-08 13:44:25 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
logger.DebugToConsole("Initializing provider: %#v config file: %#v", providerConf.Driver, viper.ConfigFileUsed())
|
2020-04-30 12:23:55 +00:00
|
|
|
err = dataprovider.InitializeDatabase(providerConf, configDir)
|
2020-02-08 13:44:25 +00:00
|
|
|
if err == nil {
|
|
|
|
logger.DebugToConsole("Data provider successfully initialized")
|
|
|
|
} else {
|
|
|
|
logger.WarnToConsole("Unable to initialize data provider: %v", err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(initProviderCmd)
|
|
|
|
addConfigFlags(initProviderCmd)
|
|
|
|
}
|