diff --git a/cmd/root.go b/cmd/root.go
index ab8236d..5397fa5 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -17,7 +17,6 @@ along with this program. If not, see .
package cmd
import (
- "database/sql"
"fmt"
"freed/internal/database"
"os"
@@ -37,7 +36,6 @@ type Config struct {
type AppContext struct {
Config Config
- DB *sql.DB
}
var (
@@ -54,32 +52,19 @@ Where f[r]eed is different to other feed aggregators or bookmarking services is
The application also includes a set of recommended "Small Web" and topic-specific curated feeds. These are also available through the configuration.
`,
- PersistentPreRun: func(cmd *cobra.Command, args []string) {
- db, err := database.Connect(appContext.Config.DB.Path)
-
- if err != nil {
- fmt.Printf("Could not connect to the database: %s", err)
- }
-
- appContext.DB = db
- },
- PersistentPostRun: func(cmd *cobra.Command, args []string) {
- if appContext.DB != nil {
- appContext.DB.Close()
- }
- },
}
)
func Execute() {
err := rootCmd.Execute()
+
if err != nil {
os.Exit(1)
}
}
func init() {
- cobra.OnInitialize(initConfig)
+ cobra.OnInitialize(initConfig, initDB)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default: ~/.config/freed/config.toml)")
}
@@ -115,3 +100,11 @@ func initConfig() {
os.Exit(1)
}
}
+
+func initDB() {
+ err := database.Open(appContext.Config.DB.Path)
+
+ if err != nil {
+ fmt.Printf("Could not connect to the database: %s", err)
+ }
+}
diff --git a/internal/database/database.go b/internal/database/database.go
index 25f2740..1b8a1aa 100644
--- a/internal/database/database.go
+++ b/internal/database/database.go
@@ -8,15 +8,18 @@ import (
migrate "github.com/rubenv/sql-migrate"
)
+var db *sql.DB
+
//go:embed migrations/*
var dbMigrations embed.FS
-func Connect(path string) (*sql.DB, error) {
+func Open(path string) error {
+ var err error
dbOptions := "?_fk=on&_journal=WAL&sync=normal"
- db, err := sql.Open("sqlite3", path+dbOptions)
+ db, err = sql.Open("sqlite3", path+dbOptions)
if err != nil {
- return nil, err
+ return err
}
migrations := migrate.EmbedFileSystemMigrationSource{
@@ -24,9 +27,13 @@ func Connect(path string) (*sql.DB, error) {
Root: "migrations",
}
- if _, err := migrate.Exec(db, "sqlite3", migrations, migrate.Up); err != nil {
- return nil, err
+ if _, migrateErr := migrate.Exec(db, "sqlite3", migrations, migrate.Up); migrateErr != nil {
+ return migrateErr
}
- return db, nil
+ if pingErr := db.Ping(); pingErr != nil {
+ return pingErr
+ }
+
+ return nil
}
diff --git a/internal/database/feed.go b/internal/database/feed.go
new file mode 100644
index 0000000..48c74a2
--- /dev/null
+++ b/internal/database/feed.go
@@ -0,0 +1,29 @@
+package database
+
+type FeedType string
+
+const (
+ RSS FeedType = "RSS"
+)
+
+type Feed struct {
+ Name string
+ Url string
+ FeedType FeedType
+}
+
+func (f Feed) Insert() (int64, error) {
+ result, err := db.Exec("INSERT INTO feed (name, url, type) VALUES (?,?,?)", f.Name, f.Url, f.FeedType)
+
+ if err != nil {
+ return 0, err
+ }
+
+ id, err := result.LastInsertId()
+
+ if err != nil {
+ return 0, err
+ }
+
+ return id, nil
+}
diff --git a/main.go b/main.go
index 01c938c..9396a14 100644
--- a/main.go
+++ b/main.go
@@ -16,7 +16,9 @@ along with this program. If not, see .
*/
package main
-import "freed/cmd"
+import (
+ "freed/cmd"
+)
func main() {
cmd.Execute()