From ab0cf15c7b23fca1c2b4aafd3f2077da6f94e54a Mon Sep 17 00:00:00 2001 From: Daan Sprenkels Date: Fri, 8 Nov 2019 22:17:05 +0100 Subject: [PATCH] Move flag handling into cmd/ --- cmd/rushlink/main.go | 5 ++++- db/db.go | 18 ++++-------------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/cmd/rushlink/main.go b/cmd/rushlink/main.go index e3d49a0..2af0166 100644 --- a/cmd/rushlink/main.go +++ b/cmd/rushlink/main.go @@ -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() diff --git a/db/db.go b/db/db.go index 24506b3..2cf4ca1 100644 --- a/db/db.go +++ b/db/db.go @@ -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)