Compare commits

...

3 Commits

Author SHA1 Message Date
Boris Bera
59b2393127
chore(lockfile) change flag to --lockfile
This is more consistent with the `lockfile` config option
2024-12-01 14:53:25 -05:00
Boris Bera
de0862a3dd
doc(lock): add lockfile doc with customization explanation 2024-12-01 14:46:56 -05:00
Boris Bera
c9a1474297
chore(lock): test setting lockfile path from config 2024-12-01 14:26:15 -05:00
7 changed files with 90 additions and 49 deletions

View File

@ -41,7 +41,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&flags.VERBOSE, "verbose", "v", false, "verbose mode") 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.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.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)") 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)")
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
} }

View File

@ -3,6 +3,7 @@
"quick": "Quick Start", "quick": "Quick Start",
"installation": "Installation", "installation": "Installation",
"config": "Configuration", "config": "Configuration",
"lockfile": "Lockfile",
"location": "Locations", "location": "Locations",
"backend": "Backend", "backend": "Backend",
"cli": "CLI", "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 autorestic --restic-bin /some/path/to/my/custom/restic/binary
``` ```
## `--lockfile-path` ## `--lockfile`
Specify the path for the lockfile used by `autorestic`. If omitted, this will default to `.autorestic.lock.yml` next to the loaded config file. 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.
```bash ```bash
autorestic --lockfile-path /path/to/my/.autorestic.lock.yml autorestic --lockfile /path/to/my/.autorestic.lock.yml
``` ```

14
docs/pages/lockfile.md Normal file
View File

@ -0,0 +1,14 @@
# 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 CRON_LEAN bool = false
RESTIC_BIN string RESTIC_BIN string
DOCKER_IMAGE string DOCKER_IMAGE string
LOCKFILE_PATH string LOCKFILE string
) )

View File

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

View File

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