update vendor/

This commit is contained in:
Ettore Di Giacinto
2019-11-25 20:32:33 +01:00
parent 08897b5105
commit a674c6515c
10 changed files with 246 additions and 0 deletions

21
vendor/github.com/marcsauter/single/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018 Marc Sauter
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

29
vendor/github.com/marcsauter/single/README.md generated vendored Normal file
View File

@@ -0,0 +1,29 @@
# single
`single` provides a mechanism to ensure, that only one instance of a program is running.
package main
import (
"log"
"time"
"github.com/marcsauter/single"
)
func main() {
s := single.New("your-app-name")
if err := s.CheckLock(); err != nil && err == single.ErrAlreadyRunning {
log.Fatal("another instance of the app is already running, exiting")
} else if err != nil {
// Another error occurred, might be worth handling it as well
log.Fatalf("failed to acquire exclusive app lock: %v", err)
}
defer s.TryUnlock()
log.Println("working")
time.Sleep(60 * time.Second)
log.Println("finished")
}
The package currently supports `linux`, `solaris` and `windows`.

41
vendor/github.com/marcsauter/single/single.go generated vendored Normal file
View File

@@ -0,0 +1,41 @@
// package single provides a mechanism to ensure, that only one instance of a program is running
package single
import (
"errors"
"log"
"os"
)
var (
// ErrAlreadyRunning -- the instance is already running
ErrAlreadyRunning = errors.New("the program is already running")
// Lockfile -- the lock file to check
Lockfile string
)
// Single represents the name and the open file descriptor
type Single struct {
name string
file *os.File
}
// New creates a Single instance
func New(name string) *Single {
return &Single{name: name}
}
// Lock tries to obtain an exclude lock on a lockfile and exits the program if an error occurs
func (s *Single) Lock() {
if err := s.CheckLock(); err != nil {
log.Fatal(err)
}
}
// Unlock releases the lock, closes and removes the lockfile. All errors will be reported directly.
func (s *Single) Unlock() {
if err := s.TryUnlock(); err != nil {
log.Print(err)
}
}

16
vendor/github.com/marcsauter/single/single_bsd.go generated vendored Normal file
View File

@@ -0,0 +1,16 @@
// +build freebsd openbsd netbsd dragonfly
package single
import (
"fmt"
"path/filepath"
)
// Filename returns an absolute filename, appropriate for the operating system
func (s *Single) Filename() string {
if len(Lockfile) > 0 {
return Lockfile
}
return filepath.Join("/var/run", fmt.Sprintf("%s.lock", s.name))
}

15
vendor/github.com/marcsauter/single/single_darwin.go generated vendored Normal file
View File

@@ -0,0 +1,15 @@
package single
import (
"fmt"
"os"
"path/filepath"
)
// Filename returns an absolute filename, appropriate for the operating system
func (s *Single) Filename() string {
if len(Lockfile) > 0 {
return Lockfile
}
return filepath.Join(os.TempDir(), fmt.Sprintf("%s.lock", s.name))
}

14
vendor/github.com/marcsauter/single/single_linux.go generated vendored Normal file
View File

@@ -0,0 +1,14 @@
package single
import (
"fmt"
"path/filepath"
)
// Filename returns an absolute filename, appropriate for the operating system
func (s *Single) Filename() string {
if len(Lockfile) > 0 {
return Lockfile
}
return filepath.Join("/var/lock", fmt.Sprintf("%s.lock", s.name))
}

14
vendor/github.com/marcsauter/single/single_solaris.go generated vendored Normal file
View File

@@ -0,0 +1,14 @@
package single
import (
"fmt"
"path/filepath"
)
// Filename returns an absolute filename, appropriate for the operating system
func (s *Single) Filename() string {
if len(Lockfile) > 0 {
return Lockfile
}
return filepath.Join("/var/run", fmt.Sprintf("%s.lock", s.name))
}

50
vendor/github.com/marcsauter/single/single_unix.go generated vendored Normal file
View File

@@ -0,0 +1,50 @@
// +build linux solaris darwin freebsd openbsd netbsd dragonfly
package single
import (
"fmt"
"os"
"syscall"
)
// CheckLock tries to obtain an exclude lock on a lockfile and returns an error if one occurs
func (s *Single) CheckLock() error {
// open/create lock file
f, err := os.OpenFile(s.Filename(), os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return err
}
s.file = f
// set the lock type to F_WRLCK, therefor the file has to be opened writable
flock := syscall.Flock_t{
Type: syscall.F_WRLCK,
Pid: int32(os.Getpid()),
}
// try to obtain an exclusive lock - FcntlFlock seems to be the portable *ix way
if err := syscall.FcntlFlock(s.file.Fd(), syscall.F_SETLK, &flock); err != nil {
return ErrAlreadyRunning
}
return nil
}
// TryUnlock unlocks, closes and removes the lockfile
func (s *Single) TryUnlock() error {
// set the lock type to F_UNLCK
flock := syscall.Flock_t{
Type: syscall.F_UNLCK,
Pid: int32(os.Getpid()),
}
if err := syscall.FcntlFlock(s.file.Fd(), syscall.F_SETLK, &flock); err != nil {
return fmt.Errorf("failed to unlock the lock file: %v", err)
}
if err := s.file.Close(); err != nil {
return fmt.Errorf("failed to close the lock file: %v", err)
}
if err := os.Remove(s.Filename()); err != nil {
return fmt.Errorf("failed to remove the lock file: %v", err)
}
return nil
}

44
vendor/github.com/marcsauter/single/single_windows.go generated vendored Normal file
View File

@@ -0,0 +1,44 @@
// +build windows
package single
import (
"fmt"
"os"
"path/filepath"
)
// Filename returns an absolute filename, appropriate for the operating system
func (s *Single) Filename() string {
if len(Lockfile) > 0 {
return Lockfile
}
return filepath.Join(os.TempDir(), fmt.Sprintf("%s.lock", s.name))
}
// CheckLock tries to obtain an exclude lock on a lockfile and returns an error if one occurs
func (s *Single) CheckLock() error {
if err := os.Remove(s.Filename()); err != nil && !os.IsNotExist(err) {
return ErrAlreadyRunning
}
file, err := os.OpenFile(s.Filename(), os.O_EXCL|os.O_CREATE, 0600)
if err != nil {
return err
}
s.file = file
return nil
}
// TryUnlock closes and removes the lockfile
func (s *Single) TryUnlock() error {
if err := s.file.Close(); err != nil {
return fmt.Errorf("failed to close the lock file: %v", err)
}
if err := os.Remove(s.Filename()); err != nil {
return fmt.Errorf("failed to remove the lock file: %v", err)
}
return nil
}