Log error and exit if the db file could not be opened within 1s
This commit is contained in:
parent
e903cbcea6
commit
4eadc39e27
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user