forked from gruntwork-io/terragrunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlockfile.go
40 lines (34 loc) · 761 Bytes
/
lockfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package util
import (
"os"
"github.com/gofrs/flock"
"github.com/gruntwork-io/go-commons/errors"
)
type Lockfile struct {
*flock.Flock
}
func NewLockfile(filename string) *Lockfile {
return &Lockfile{
flock.New(filename),
}
}
func (lockfile *Lockfile) Unlock() error {
if lockfile.Flock == nil {
return nil
}
if err := lockfile.Flock.Unlock(); err != nil {
return errors.WithStackTrace(err)
}
if err := os.Remove(lockfile.Path()); err != nil {
return errors.WithStackTrace(err)
}
return nil
}
func (lockfile *Lockfile) TryLock() error {
if locked, err := lockfile.Flock.TryLock(); err != nil {
return errors.WithStackTrace(err)
} else if !locked {
return errors.Errorf("unable to lock file %s", lockfile.Path())
}
return nil
}