mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2026-04-02 20:05:23 +00:00
Refactor setLock to accept key value pairs
This allows SetCron and Lock to use the same function setLockValue. It also removes the need to call getLock explicitly in tests by returning the lock object.
This commit is contained in:
@@ -14,6 +14,10 @@ var lock *viper.Viper
|
|||||||
var file string
|
var file string
|
||||||
var once sync.Once
|
var once sync.Once
|
||||||
|
|
||||||
|
const (
|
||||||
|
RUNNING = "running"
|
||||||
|
)
|
||||||
|
|
||||||
func getLock() *viper.Viper {
|
func getLock() *viper.Viper {
|
||||||
if lock == nil {
|
if lock == nil {
|
||||||
|
|
||||||
@@ -37,36 +41,38 @@ func getLock() *viper.Viper {
|
|||||||
return lock
|
return lock
|
||||||
}
|
}
|
||||||
|
|
||||||
func setLock(locked bool) error {
|
func setLockValue(key string, value interface{}) (*viper.Viper, error) {
|
||||||
lock := getLock()
|
lock := getLock()
|
||||||
if locked {
|
|
||||||
running := lock.GetBool("running")
|
if key == RUNNING {
|
||||||
if running {
|
value := value.(bool)
|
||||||
|
if value && lock.GetBool(key) {
|
||||||
colors.Error.Println("an instance is already running. exiting")
|
colors.Error.Println("an instance is already running. exiting")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lock.Set("running", locked)
|
|
||||||
|
lock.Set(key, value)
|
||||||
if err := lock.WriteConfigAs(file); err != nil {
|
if err := lock.WriteConfigAs(file); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
return nil
|
return lock, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCron(location string) int64 {
|
func GetCron(location string) int64 {
|
||||||
lock := getLock()
|
return getLock().GetInt64("cron." + location)
|
||||||
return lock.GetInt64("cron." + location)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetCron(location string, value int64) {
|
func SetCron(location string, value int64) {
|
||||||
lock.Set("cron."+location, value)
|
setLockValue("cron."+location, value)
|
||||||
lock.WriteConfigAs(file)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Lock() error {
|
func Lock() error {
|
||||||
return setLock(true)
|
_, err := setLockValue(RUNNING, true)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func Unlock() error {
|
func Unlock() error {
|
||||||
return setLock(false)
|
_, err := setLockValue(RUNNING, false)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ func TestLock(t *testing.T) {
|
|||||||
setup(t)
|
setup(t)
|
||||||
|
|
||||||
t.Run("getLock", func(t *testing.T) {
|
t.Run("getLock", func(t *testing.T) {
|
||||||
result := getLock().GetBool("running")
|
result := getLock().GetBool(RUNNING)
|
||||||
|
|
||||||
if result {
|
if result {
|
||||||
t.Errorf("got %v, want %v", result, false)
|
t.Errorf("got %v, want %v", result, false)
|
||||||
@@ -39,24 +39,24 @@ func TestLock(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("lock", func(t *testing.T) {
|
t.Run("lock", func(t *testing.T) {
|
||||||
err := Lock()
|
lock, err := setLockValue(RUNNING, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
result := getLock().GetBool("running")
|
result := lock.GetBool(RUNNING)
|
||||||
if !result {
|
if !result {
|
||||||
t.Errorf("got %v, want %v", result, true)
|
t.Errorf("got %v, want %v", result, true)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("unlock", func(t *testing.T) {
|
t.Run("unlock", func(t *testing.T) {
|
||||||
err := Unlock()
|
lock, err := setLockValue(RUNNING, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
result := getLock().GetBool("running")
|
result := lock.GetBool(RUNNING)
|
||||||
if result {
|
if result {
|
||||||
t.Errorf("got %v, want %v", result, false)
|
t.Errorf("got %v, want %v", result, false)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user