From f6aa45fd6c0724ce7415edaebce21993f70568a9 Mon Sep 17 00:00:00 2001 From: Boris Bera Date: Tue, 2 Jul 2024 21:12:26 -0400 Subject: [PATCH] feat(lockfile): allow changing the lockfile path --- cmd/root.go | 1 + internal/flags/flags.go | 11 ++++++----- internal/lock/lock.go | 24 +++++++++++++++++------- internal/lock/lock_test.go | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 3da9e25..df69ab4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -42,6 +42,7 @@ func init() { rootCmd.PersistentFlags().BoolVarP(&flags.VERBOSE, "verbose", "v", false, "verbose mode") rootCmd.PersistentFlags().StringVar(&flags.RESTIC_BIN, "restic-bin", "restic", "specify custom restic binary") rootCmd.PersistentFlags().StringVar(&flags.DOCKER_IMAGE, "docker-image", "cupcakearmy/autorestic:"+internal.VERSION, "specify a custom docker image") + rootCmd.PersistentFlags().StringVar(&flags.LOCKFILE_PATH, "lockfile-path", "", "specify a custom path for the lockfile (defaults to .autorestic.lock.yml next to the loaded autorestic config file)") cobra.OnInitialize(initConfig) } diff --git a/internal/flags/flags.go b/internal/flags/flags.go index e03cc25..801aed7 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -1,9 +1,10 @@ package flags var ( - CI bool = false - VERBOSE bool = false - CRON_LEAN bool = false - RESTIC_BIN string - DOCKER_IMAGE string + CI bool = false + VERBOSE bool = false + CRON_LEAN bool = false + RESTIC_BIN string + DOCKER_IMAGE string + LOCKFILE_PATH string ) diff --git a/internal/lock/lock.go b/internal/lock/lock.go index 4e9fd52..caae87c 100644 --- a/internal/lock/lock.go +++ b/internal/lock/lock.go @@ -18,18 +18,28 @@ const ( RUNNING = "running" ) +// getLockfilePath returns the path to the lockfile. If flags.LOCKFILE_PATH is +// set, its value is used, otherwise the path is generated relative to the +// config file. +func getLockfilePath() string { + if flags.LOCKFILE_PATH != "" { + return flags.LOCKFILE_PATH + } else { + p := viper.ConfigFileUsed() + if p == "" { + colors.Error.Println("cannot lock before reading config location") + os.Exit(1) + } + return path.Join(path.Dir(p), ".autorestic.lock.yml") + } +} + func getLock() *viper.Viper { if lock == nil { - once.Do(func() { lock = viper.New() lock.SetDefault("running", false) - p := viper.ConfigFileUsed() - if p == "" { - colors.Error.Println("cannot lock before reading config location") - os.Exit(1) - } - file = path.Join(path.Dir(p), ".autorestic.lock.yml") + file = getLockfilePath() if !flags.CRON_LEAN { colors.Faint.Println("Using lock:\t", file) } diff --git a/internal/lock/lock_test.go b/internal/lock/lock_test.go index 2b9d9f7..7546d9d 100644 --- a/internal/lock/lock_test.go +++ b/internal/lock/lock_test.go @@ -7,6 +7,7 @@ import ( "strconv" "testing" + "github.com/cupcakearmy/autorestic/internal/flags" "github.com/spf13/viper" ) @@ -28,6 +29,37 @@ func setup(t *testing.T) { }) } +func TestGetLockfilePath(t *testing.T) { + t.Run("when flags.LOCKFILE_PATH is set", func(t *testing.T) { + flags.LOCKFILE_PATH = "/path/to/my/autorestic.lock.yml" + defer func() { flags.LOCKFILE_PATH = "" }() + + p := getLockfilePath() + + if p != "/path/to/my/autorestic.lock.yml" { + t.Errorf("got %v, want %v", p, "/path/to/my/autorestic.lock.yml") + } + }) + + t.Run("when flags.LOCKFILE_PATH is set", func(t *testing.T) { + d, err := os.MkdirTemp("", testDirectory) + if err != nil { + log.Fatalf("error creating temp dir: %v", err) + return + } + viper.SetConfigFile(d + "/.autorestic.yml") + defer viper.Reset() + + flags.LOCKFILE_PATH = "" + + p := getLockfilePath() + + if p != d+"/.autorestic.lock.yml" { + t.Errorf("got %v, want %v", p, d+"/.autorestic.lock.yml") + } + }) +} + func TestLock(t *testing.T) { setup(t)