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() {
var databasePath, fileStorePath string
flag.StringVar(&databasePath, "database", "", "Location of the database file")
flag.Parse()
if err := db.Open(); err != nil {
if err := db.Open(databasePath); err != nil {
log.Fatalln(err)
}
defer db.Close()

View File

@ -1,7 +1,6 @@
package db
import (
"flag"
"fmt"
"log"
"time"
@ -12,7 +11,6 @@ import (
"gitea.hashru.nl/dsprenkels/rushlink/gobmarsh"
)
var path = flag.String("database", "", "Location of the database file")
var DB *bolt.DB
// The current database version
@ -33,15 +31,15 @@ const BUCKET_PASTES = "pastes"
const KEY_MIGRATE_VERSION = "migrate_version"
// Open the bolt database
func Open() error {
if *path == "" {
func Open(path string) error {
if path == "" {
return errors.New("database not set")
}
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 {
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)
}
@ -54,14 +52,6 @@ func Close() error {
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
func migrateDatabase(tx *bolt.Tx) error {
dbVersion, err := dbVersion(tx)