From 94a5fbfc51361242f08948835af7752343691fa4 Mon Sep 17 00:00:00 2001 From: Melora Hugues Date: Mon, 18 Mar 2024 16:40:28 +0100 Subject: [PATCH] Feat #45: Add CLI commands to manage the DB --- polyculeconnect/cmd/db/connect.go | 65 +++++++++++++++++++++++++++++++ polyculeconnect/cmd/db/db.go | 27 +++++++++++++ polyculeconnect/cmd/db/destroy.go | 60 ++++++++++++++++++++++++++++ polyculeconnect/main.go | 1 + 4 files changed, 153 insertions(+) create mode 100644 polyculeconnect/cmd/db/connect.go create mode 100644 polyculeconnect/cmd/db/db.go create mode 100644 polyculeconnect/cmd/db/destroy.go diff --git a/polyculeconnect/cmd/db/connect.go b/polyculeconnect/cmd/db/connect.go new file mode 100644 index 0000000..6960bd4 --- /dev/null +++ b/polyculeconnect/cmd/db/connect.go @@ -0,0 +1,65 @@ +package db + +import ( + "errors" + "fmt" + "os/exec" + "syscall" + + "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils" + "git.faercol.me/faercol/polyculeconnect/polyculeconnect/config" + "github.com/spf13/cobra" +) + +// connectCmd represents the db connect command +var connectCmd = &cobra.Command{ + Use: "connect", + Short: "Connect to the database", + Long: `Connect to the database.`, + Run: func(cmd *cobra.Command, args []string) { + conf := utils.InitConfig("") + if err := connectToDB(conf); err != nil { + utils.Failf("Failed to connect to DB: %s", err.Error()) + } + }, +} + +func connectSQLite(conf *config.StorageConfig) error { + path, err := exec.LookPath("sqlite3") + if err != nil { + if errors.Is(err, exec.ErrNotFound) { + return errors.New("sqlite3 not installed") + } + return fmt.Errorf("failed to find sqlite3 executable: %w", err) + } + + if err := syscall.Exec(path, []string{path, conf.File}, nil); err != nil { + return fmt.Errorf("failed to run sqlite3 command: %w", err) + } + return nil +} + +func connectToDB(conf *config.AppConfig) error { + switch conf.StorageType { + case string(config.Memory): + return errors.New("no DB associated with memory storage") + case string(config.SQLite): + return connectSQLite(conf.StorageConfig) + default: + return fmt.Errorf("unsupported storage type %q", conf.StorageType) + } +} + +func init() { + dbCmd.AddCommand(connectCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // dbCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // dbCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/polyculeconnect/cmd/db/db.go b/polyculeconnect/cmd/db/db.go new file mode 100644 index 0000000..0df72fc --- /dev/null +++ b/polyculeconnect/cmd/db/db.go @@ -0,0 +1,27 @@ +package db + +import ( + "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd" + "github.com/spf13/cobra" +) + +// dbCmd represents the db command +var dbCmd = &cobra.Command{ + Use: "db", + Short: "Manage the database", + Long: `Manage the database.`, +} + +func init() { + cmd.RootCmd.AddCommand(dbCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // dbCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // dbCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/polyculeconnect/cmd/db/destroy.go b/polyculeconnect/cmd/db/destroy.go new file mode 100644 index 0000000..1fef2e9 --- /dev/null +++ b/polyculeconnect/cmd/db/destroy.go @@ -0,0 +1,60 @@ +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) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // dbCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // dbCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/polyculeconnect/main.go b/polyculeconnect/main.go index 2cf4d4d..1012ff6 100644 --- a/polyculeconnect/main.go +++ b/polyculeconnect/main.go @@ -4,6 +4,7 @@ import ( "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd" _ "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/app" _ "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/backend" + _ "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/db" _ "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/serve" )