github.com/tilt-dev/wat@v0.0.2-0.20180626175338-9349b638e250/os/temp/persistent.go (about) 1 package temp 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/windmilleng/wat/errors" 9 ) 10 11 // An implementation of Dir more suitable 12 // for directories that need to persist. 13 type PersistentDir struct { 14 dir string 15 } 16 17 func NewPersistentDir(path string) (*PersistentDir, error) { 18 _, err := os.Stat(path) 19 if err == nil || !os.IsNotExist(err) { 20 return nil, fmt.Errorf("NewPersistentDir: dir already exists: %s", path) 21 } 22 23 err = os.Mkdir(path, 0777) 24 if err != nil { 25 return nil, errors.Propagatef(err, "NewPersistentDir failed to create %s", path) 26 } 27 28 return &PersistentDir{dir: path}, nil 29 } 30 31 func (d *PersistentDir) NewDir(name string) (Dir, error) { 32 path := filepath.Join(d.dir, name) 33 return NewPersistentDir(path) 34 } 35 36 func (d *PersistentDir) TearDown() error { 37 return os.RemoveAll(d.dir) 38 } 39 40 func (d *PersistentDir) Path() string { 41 return d.dir 42 }