From 4eadc39e277b41d71c961f677a177be398d132d5 Mon Sep 17 00:00:00 2001 From: Andrew Slotin Date: Fri, 13 Oct 2017 18:57:28 +0700 Subject: [PATCH] Log error and exit if the db file could not be opened within 1s --- boltbrowser.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/boltbrowser.go b/boltbrowser.go index f2ddbb8..4c38de6 100644 --- a/boltbrowser.go +++ b/boltbrowser.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "os" + "time" "github.com/boltdb/bolt" "github.com/nsf/termbox-go" @@ -20,6 +21,8 @@ var memBolt *BoltDB var currentFilename string +const DefaultDBOpenTimeout = time.Second + func init() { flag.Usage = func() { fmt.Fprintf(os.Stdout, "Usage: %s \n", ProgramName) @@ -47,8 +50,12 @@ func main() { databaseFiles := flag.Args() for _, databaseFile := range databaseFiles { currentFilename = databaseFile - db, err = bolt.Open(databaseFile, 0600, nil) - if err != nil { + db, err = bolt.Open(databaseFile, 0600, &bolt.Options{Timeout: DefaultDBOpenTimeout}) + if err == bolt.ErrTimeout { + termbox.Close() + fmt.Printf("File %s is locked. Make sure it's not used by another app and try again\n", databaseFile) + os.Exit(1) + } else if err != nil { if len(databaseFiles) > 1 { mainLoop(nil, style) continue