diff --git a/README.md b/README.md index ab46c8e..140deb2 100644 --- a/README.md +++ b/README.md @@ -36,3 +36,9 @@ Just provide a BoltDB filename to be opened as the first argument on the command ```sh boltbrowser ``` + +To see all options that are available, run: + +``` +boltbrowser --help +``` diff --git a/boltbrowser.go b/boltbrowser.go index f2ddbb8..2a21b22 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,9 +21,17 @@ var memBolt *BoltDB var currentFilename string +const DefaultDBOpenTimeout = time.Second + +var args struct { + DBOpenTimeout time.Duration +} + func init() { + flag.DurationVar(&args.DBOpenTimeout, "timeout", DefaultDBOpenTimeout, "DB file open timeout") flag.Usage = func() { - fmt.Fprintf(os.Stdout, "Usage: %s \n", ProgramName) + fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] \nOptions:\n", ProgramName) + flag.PrintDefaults() } } @@ -47,8 +56,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: args.DBOpenTimeout}) + 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