polycule-connect/polyculeconnect/cmd/db/destroy.go
Melora Hugues 3ca1a9a51c
Some checks failed
/ go-test (push) Failing after 51s
/ docker-build-only (push) Successful in 1m41s
Chore: remove generated cobra comments and improve help messages
2024-08-10 16:27:49 +02:00

50 lines
1.2 KiB
Go

package db
import (
"errors"
"fmt"
"os"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/config"
"github.com/spf13/cobra"
)
// destroyCmd represents the db destroy command
var destroyCmd = &cobra.Command{
Use: "destroy",
Short: "Completely delete the current database",
Long: `Delete the current database.`,
Run: func(cmd *cobra.Command, args []string) {
conf := utils.InitConfig("")
if err := deleteDB(conf); err != nil {
utils.Failf("Failed to connect to DB: %s", err.Error())
}
fmt.Println("DB deleted")
},
}
func deleteSqliteDB(path string) error {
if err := os.Remove(path); err != nil {
if errors.Is(err, os.ErrNotExist) { // if the file has already been deleted we don't want to fail here
return nil
}
return fmt.Errorf("failed to delete SQLite file: %w", err)
}
return nil
}
func deleteDB(conf *config.AppConfig) error {
switch conf.StorageType {
case string(config.Memory):
return errors.New("no DB to delete in memory mode")
case string(config.SQLite):
return deleteSqliteDB(conf.StorageConfig.File)
default:
return fmt.Errorf("unsupported storage type %q", conf.StorageType)
}
}
func init() {
dbCmd.AddCommand(destroyCmd)
}