Compare commits

..

1 Commits

Author SHA1 Message Date
Boris Bera
db6fbd598b
Merge 0fdf9c77ae into 48fa20b482 2024-11-15 13:43:01 -05:00
7 changed files with 47 additions and 88 deletions

View File

@ -41,7 +41,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, "lockfile", "", "specify a custom path for the lockfile (defaults to .autorestic.lock.yml next to the loaded autorestic config file)")
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)
}

View File

@ -3,7 +3,6 @@
"quick": "Quick Start",
"installation": "Installation",
"config": "Configuration",
"lockfile": "Lockfile",
"location": "Locations",
"backend": "Backend",
"cli": "CLI",

View File

@ -35,10 +35,10 @@ With `--restic-bin` you can specify to run a specific restic binary. This can be
autorestic --restic-bin /some/path/to/my/custom/restic/binary
```
## `--lockfile`
## `--lockfile-path`
Specify the path for the [lockfile](../lockfile.md) used by `autorestic`. If omitted, this will default to `.autorestic.lock.yml` next to the loaded config file.
Specify the path for the lockfile used by `autorestic`. If omitted, this will default to `.autorestic.lock.yml` next to the loaded config file.
```bash
autorestic --lockfile /path/to/my/.autorestic.lock.yml
autorestic --lockfile-path /path/to/my/.autorestic.lock.yml
```

View File

@ -1,14 +0,0 @@
# Lockfile
Under the hood, `autorestic` uses a lockfile to ensure that only one instance is running and to keep track of when [cronjobs](./location/cron.md) were last run.
By default, the lockfile is stored next to your [configuration file](./config.md) as `.autorestic.lock.yml`. In other words, if your config file is located at `/some/path/.autorestic.yml`, then the lockfile will be located at `/some/path/.autorestic.lock.yml`.
## Customization
The path to the lockfile can be customized if need be. This can be done is a few ways:
1. Using the `--lockfile ...` command line flag
1. Setting `lockfile: ...` in the configuration file
Note that `autorestic` will check for a customized lockfile path in the order listed above. This means that if you specify a lockfile path in multiple places, the method that's higher in the list will win.

View File

@ -6,5 +6,5 @@ var (
CRON_LEAN bool = false
RESTIC_BIN string
DOCKER_IMAGE string
LOCKFILE string
LOCKFILE_PATH string
)

View File

@ -20,14 +20,14 @@ const (
)
// getLockfilePath returns the path to the lockfile. The path for the lockfile
// can be sources from multiple places If flags.LOCKFILE is set, its value is
// used; if the config has the `lockfile` option set, its value is used;
// 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 != "" {
abs, err := filepath.Abs(flags.LOCKFILE)
if flags.LOCKFILE_PATH != "" {
abs, err := filepath.Abs(flags.LOCKFILE_PATH)
if err != nil {
return flags.LOCKFILE
return flags.LOCKFILE_PATH
}
return abs
}

View File

@ -1,88 +1,62 @@
package internal
import (
"log"
"os"
"os/exec"
"path"
"strconv"
"sync"
"testing"
"github.com/cupcakearmy/autorestic/internal/flags"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
var testDirectory = "autorestic_test_tmp"
// All tests must share the same lock file as it is only initialized once
func setup(t *testing.T) {
t.Helper()
cleanup := func() {
flags.LOCKFILE = ""
config = nil
once = sync.Once{}
viper.Reset()
d, err := os.MkdirTemp("", testDirectory)
if err != nil {
log.Fatalf("error creating temp dir: %v", err)
return
}
cleanup()
d := t.TempDir()
// set config file location
viper.SetConfigFile(d + "/.autorestic.yml")
viper.Set("version", 2)
viper.WriteConfig()
t.Cleanup(cleanup)
t.Cleanup(func() {
os.RemoveAll(d)
viper.Reset()
})
}
func TestGetLockfilePath(t *testing.T) {
t.Run("user specified", func(t *testing.T) {
testCases := []struct {
name string
flag string
config string
expected string
}{
{
name: "flag and config",
flag: "/flag.lock.yml",
config: "/config.lock.yml",
expected: "/flag.lock.yml",
},
{
name: "flag only",
flag: "/flag.lock.yml",
config: "",
expected: "/flag.lock.yml",
},
{
name: "config only",
flag: "",
config: "/config.lock.yml",
expected: "/config.lock.yml",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
setup(t)
flags.LOCKFILE = testCase.flag
if testCase.config != "" {
viper.Set("lockfile", testCase.config)
err := viper.WriteConfig()
assert.NoError(t, err)
}
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()
assert.Equal(t, testCase.expected, p)
})
if p != "/path/to/my/autorestic.lock.yml" {
t.Errorf("got %v, want %v", p, "/path/to/my/autorestic.lock.yml")
}
})
t.Run("default", func(t *testing.T) {
setup(t)
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()
configPath := viper.ConfigFileUsed()
expectedLockfile := path.Join(path.Dir(configPath), ".autorestic.lock.yml")
flags.LOCKFILE_PATH = ""
p := getLockfilePath()
assert.Equal(t, expectedLockfile, p)
if p != d+"/.autorestic.lock.yml" {
t.Errorf("got %v, want %v", p, d+"/.autorestic.lock.yml")
}
})
}