From 0fdf9c77aebfb9129e21d612fa5917edb0570ffa Mon Sep 17 00:00:00 2001 From: Boris Bera Date: Sun, 10 Nov 2024 13:45:24 -0500 Subject: [PATCH] feat(config): allow specifying lockfile --- internal/config.go | 1 + internal/lock.go | 34 ++++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/internal/config.go b/internal/config.go index 89e7c82..f3c63b0 100644 --- a/internal/config.go +++ b/internal/config.go @@ -23,6 +23,7 @@ type Options map[string]OptionMap type Config struct { Version string `mapstructure:"version" yaml:"version"` + Lockfile string `mapstructure:"lockfile,omitempty" yaml:"lockfile,omitempty"` Extras interface{} `mapstructure:"extras" yaml:"extras"` Locations map[string]Location `mapstructure:"locations" yaml:"locations"` Backends map[string]Backend `mapstructure:"backends" yaml:"backends"` diff --git a/internal/lock.go b/internal/lock.go index 6226f12..a128bdf 100644 --- a/internal/lock.go +++ b/internal/lock.go @@ -3,6 +3,7 @@ package internal import ( "os" "path" + "path/filepath" "sync" "github.com/cupcakearmy/autorestic/internal/colors" @@ -18,20 +19,33 @@ 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. +// getLockfilePath returns the path to the lockfile. The path for the lockfile +// can be sources from multiple places If flags.LOCKFILE_PATH is set, its value +// is used; if the config has the `lockfile` option 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) + abs, err := filepath.Abs(flags.LOCKFILE_PATH) + if err != nil { + return flags.LOCKFILE_PATH + } + return abs + } + + if lockfile := GetConfig().Lockfile; lockfile != "" { + abs, err := filepath.Abs(lockfile) + if err != nil { + return lockfile } - return path.Join(path.Dir(p), ".autorestic.lock.yml") + return abs + } + + 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 {