Merge pull request #6 from dennisschoepf/refactor/database
Moves database initialization to OnInitialize
This commit is contained in:
commit
9bc607472f
4 changed files with 55 additions and 24 deletions
27
cmd/root.go
27
cmd/root.go
|
|
@ -17,7 +17,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
29
internal/database/feed.go
Normal file
29
internal/database/feed.go
Normal file
|
|
@ -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
|
||||
}
|
||||
4
main.go
4
main.go
|
|
@ -16,7 +16,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/
|
||||
package main
|
||||
|
||||
import "freed/cmd"
|
||||
import (
|
||||
"freed/cmd"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cmd.Execute()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue