Pass db open timeout via command-line flag

This commit is contained in:
Andrew Slotin 2017-10-13 19:01:25 +07:00
parent 4eadc39e27
commit 4ab47545f7

View File

@ -23,9 +23,15 @@ var currentFilename string
const DefaultDBOpenTimeout = time.Second const DefaultDBOpenTimeout = time.Second
var args struct {
DBOpenTimeout time.Duration
}
func init() { func init() {
flag.DurationVar(&args.DBOpenTimeout, "timeout", DefaultDBOpenTimeout, "DB file open timeout")
flag.Usage = func() { flag.Usage = func() {
fmt.Fprintf(os.Stdout, "Usage: %s <filename(s)>\n", ProgramName) fmt.Fprintf(os.Stdout, "Usage: %s [OPTIONS] <filename(s)>\nOptions:\n", ProgramName)
flag.PrintDefaults()
} }
} }
@ -50,7 +56,7 @@ 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, &bolt.Options{Timeout: DefaultDBOpenTimeout}) db, err = bolt.Open(databaseFile, 0600, &bolt.Options{Timeout: args.DBOpenTimeout})
if err == bolt.ErrTimeout { if err == bolt.ErrTimeout {
termbox.Close() termbox.Close()
fmt.Printf("File %s is locked. Make sure it's not used by another app and try again\n", databaseFile) fmt.Printf("File %s is locked. Make sure it's not used by another app and try again\n", databaseFile)