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" "flag"
"fmt" "fmt"
"os" "os"
"time"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
@ -20,6 +21,8 @@ var memBolt *BoltDB
var currentFilename string var currentFilename string
const DefaultDBOpenTimeout = time.Second
func init() { func init() {
flag.Usage = func() { flag.Usage = func() {
fmt.Fprintf(os.Stdout, "Usage: %s <filename(s)>\n", ProgramName) fmt.Fprintf(os.Stdout, "Usage: %s <filename(s)>\n", ProgramName)
@ -47,8 +50,12 @@ func main() {
databaseFiles := flag.Args() databaseFiles := flag.Args()
for _, databaseFile := range databaseFiles { for _, databaseFile := range databaseFiles {
currentFilename = databaseFile currentFilename = databaseFile
db, err = bolt.Open(databaseFile, 0600, nil) db, err = bolt.Open(databaseFile, 0600, &bolt.Options{Timeout: DefaultDBOpenTimeout})
if err != nil { 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 { if len(databaseFiles) > 1 {
mainLoop(nil, style) mainLoop(nil, style)
continue continue