Move flag handling into cmd/

This commit is contained in:
Daan Sprenkels 2019-11-08 22:17:05 +01:00
parent 853945a138
commit ab0cf15c7b
2 changed files with 8 additions and 15 deletions

View File

@ -10,9 +10,12 @@ import (
) )
func main() { func main() {
var databasePath, fileStorePath string
flag.StringVar(&databasePath, "database", "", "Location of the database file")
flag.Parse() flag.Parse()
if err := db.Open(); err != nil { if err := db.Open(databasePath); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
defer db.Close() defer db.Close()

View File

@ -1,7 +1,6 @@
package db package db
import ( import (
"flag"
"fmt" "fmt"
"log" "log"
"time" "time"
@ -12,7 +11,6 @@ import (
"gitea.hashru.nl/dsprenkels/rushlink/gobmarsh" "gitea.hashru.nl/dsprenkels/rushlink/gobmarsh"
) )
var path = flag.String("database", "", "Location of the database file")
var DB *bolt.DB var DB *bolt.DB
// The current database version // The current database version
@ -33,15 +31,15 @@ const BUCKET_PASTES = "pastes"
const KEY_MIGRATE_VERSION = "migrate_version" const KEY_MIGRATE_VERSION = "migrate_version"
// Open the bolt database // Open the bolt database
func Open() error { func Open(path string) error {
if *path == "" { if path == "" {
return errors.New("database not set") return errors.New("database not set")
} }
var err error var err error
DB, err = bolt.Open(*path, 0666, &bolt.Options{Timeout: 1 * time.Second}) DB, err = bolt.Open(path, 0666, &bolt.Options{Timeout: 1 * time.Second})
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to open database at '%v'", *path) return errors.Wrapf(err, "failed to open database at '%v'", path)
} }
return DB.Update(migrateDatabase) return DB.Update(migrateDatabase)
} }
@ -54,14 +52,6 @@ func Close() error {
return DB.Close() return DB.Close()
} }
// Get the database path (as was set by flags)
func Path() string {
if path == nil {
return ""
}
return *path
}
// Initialize and migrate the database to the current version // Initialize and migrate the database to the current version
func migrateDatabase(tx *bolt.Tx) error { func migrateDatabase(tx *bolt.Tx) error {
dbVersion, err := dbVersion(tx) dbVersion, err := dbVersion(tx)