Log error and exit if the db file could not be opened within 1s

This commit is contained in:
Andrew Slotin 2017-10-13 18:57:28 +07:00
parent e903cbcea6
commit 4eadc39e27

View File

@ -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 <filename(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