Set experimental option ExistDir

This commit is contained in:
Sergey Romanov 2015-11-21 22:57:59 +05:00
parent 94c595be43
commit 9be99d20fb
2 changed files with 17 additions and 1 deletions

View File

@ -35,8 +35,11 @@ type (
} }
) )
// Options uses for specification of working golang-scribble
type Options struct { type Options struct {
Logger Logger
// ExistDir not creates new directory before start
ExistDir bool
} }
// New creates a new scribble database at the desired directory location, and // New creates a new scribble database at the desired directory location, and
@ -53,7 +56,8 @@ func New(dir string, opt *Options) (driver *Driver, err error) {
// ensure the database location doesn't already exist (we don't want to overwrite // ensure the database location doesn't already exist (we don't want to overwrite
// any existing files/database) // any existing files/database)
if _, err := os.Stat(dir); err == nil { _, err = os.Stat(dir)
if err == nil && !options.ExistDir {
fmt.Printf("Unable to create database, '%s' already exists. Please specify a different location.\n", dir) fmt.Printf("Unable to create database, '%s' already exists. Please specify a different location.\n", dir)
os.Exit(1) os.Exit(1)
} }
@ -72,6 +76,10 @@ func New(dir string, opt *Options) (driver *Driver, err error) {
options.Logger.Info("Creating scribble database at '%v'...\n", dir) options.Logger.Info("Creating scribble database at '%v'...\n", dir)
//
if options.ExistDir && err == nil {
return driver, nil
}
// create database // create database
return driver, os.MkdirAll(dir, 0755) return driver, os.MkdirAll(dir, 0755)
} }

View File

@ -48,6 +48,14 @@ func TestNew(t *testing.T) {
} }
} }
// Checking opening exist dir
func TestNewExist(t *testing.T) {
var err error
if db, err = New(database, &Options{ExistDir: true}); err != nil {
panic(err)
}
}
// //
func TestWriteAndRead(t *testing.T) { func TestWriteAndRead(t *testing.T) {