chore: move lockfile code to internal module

In a future commit, I'll need the lockfile code to access the config
file. This solves an import cycle.
This commit is contained in:
Boris Bera
2024-11-10 13:37:59 -05:00
parent ccca7c850f
commit 8de8d0070e
13 changed files with 23 additions and 33 deletions

View File

@@ -10,7 +10,6 @@ import (
"github.com/cupcakearmy/autorestic/internal/colors"
"github.com/cupcakearmy/autorestic/internal/flags"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/joho/godotenv"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
@@ -40,7 +39,7 @@ func exitConfig(err error, msg string) {
if msg != "" {
colors.Error.Println(msg)
}
lock.Unlock()
Unlock()
os.Exit(1)
}

View File

@@ -11,7 +11,6 @@ import (
"github.com/cupcakearmy/autorestic/internal/colors"
"github.com/cupcakearmy/autorestic/internal/flags"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/cupcakearmy/autorestic/internal/metadata"
"github.com/robfig/cron"
)
@@ -442,11 +441,11 @@ func (l Location) RunCron() error {
if err != nil {
return err
}
last := time.Unix(lock.GetCron(l.name), 0)
last := time.Unix(GetCron(l.name), 0)
next := schedule.Next(last)
now := time.Now()
if now.After(next) {
lock.SetCron(l.name, now.Unix())
SetCron(l.name, now.Unix())
errs := l.Backup(true, "")
if len(errs) > 0 {
return fmt.Errorf("Failed to backup location \"%s\":\n%w", l.name, errors.Join(errs...))

View File

@@ -1,4 +1,4 @@
package lock
package internal
import (
"os"
@@ -12,7 +12,7 @@ import (
var lock *viper.Viper
var file string
var once sync.Once
var lockOnce sync.Once
const (
RUNNING = "running"
@@ -36,7 +36,7 @@ func getLockfilePath() string {
func getLock() *viper.Viper {
if lock == nil {
once.Do(func() {
lockOnce.Do(func() {
lock = viper.New()
lock.SetDefault("running", false)
file = getLockfilePath()

View File

@@ -1,4 +1,4 @@
package lock
package internal
import (
"log"