-
Notifications
You must be signed in to change notification settings - Fork 8
/
single.go
48 lines (39 loc) · 1.03 KB
/
single.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
41
42
43
44
45
46
47
48
// Package single provides a mechanism to ensure, that only one instance of a program is running
package single
import (
"errors"
"fmt"
"os"
"path/filepath"
)
var (
// ErrAlreadyRunning another instance of is already running
ErrAlreadyRunning = errors.New("another instance is already running")
)
// Single represents the name and the open file descriptor
type Single struct {
name string
path string
file *os.File
}
// New creates a Single instance where name is the basename of the lock file (<name>.lock)
// if no path is given (WithLockPath option) the lock will be created in an operating specific path as <name>.lock
func New(name string, opts ...Option) (*Single, error) {
if name == "" {
return nil, fmt.Errorf("name cannot be empty")
}
s := &Single{
name: name,
}
for _, o := range opts {
o(s)
}
if s.path == "" {
s.path = os.TempDir()
}
return s, nil
}
// Lockfile returns the full path of the lock file
func (s *Single) Lockfile() string {
return filepath.Join(s.path, fmt.Sprintf("%s.lock", s.name))
}